I have this class :
public class Bird extends AbstractEntity<Long> implements IHasWings {
}
And some other classes that extends AbstractEntity
but they don't implement IHasWings
, for example :
public class Cat extends AbstractEntity<Long>{
}
I pass these classes as a type argument in their DAO classe, like this :
public class BirdDAO extends AbstractDAO<Bird, Long> { ... }
and
public class CatDAO extends AbstractDAO<Cat, Long> { ... }
So all the DAO classes are extending the AbstractDAO
class.
In AbstractDAO
class I have this :
public abstract class AbstractDAO<T extends AbstractEntity<ID>, ID extends Serializable> {
public List<T> findAll(){
AbstractEntity entity = ...
if (entity instanceof IHasWings) {
IHasWings entityWithWings = (IHasWings) entity;
//Do something with entityWithWings.getWingSize();
}else{
//Do something else
}
}
}
I want to test if the passed type argument is implementing IHasWings
as you can see above, but I couldn't find a way to do it.
I tried the following :
Class<T> entity = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
if (entity instanceof IHasWings) {...}
Which threw this error : Incompatible conditional operand types Class<T> and IHasWings
How can I solve this ?