views:

41

answers:

2

I have an abstract generic class.

public abstract class FieldHandlerWithData<DataType extends Parcelable> 
    extends FieldHandler

Now I have an object c

Class<? extends FieldHandler> c = getHandlerClass(type);

and now I want to test if c inherits FieldHandlerWithData (directly or indirectly). How to determine whether c inherits FieldHandlerWithData?

c.isAssignableFrom(FieldHandlerWithData.class) - returns false.

A: 

Try the other way around instead:

if (FieldHandlerWithData.class.isAssignableFrom(c)) ...

If this condition is true, class c is either FieldHandlerWithData itself, or a subclass of it.

Péter Török
+1  A: 

It's the other way around - FieldHandlerWithData.class.isAssignableFrom(c)

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter

So this class (the one on which the method is invoked) should be the superclass/superinterface

Bozho