Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if a field is instanceof a type via reflection?

I want to find out via reflection if a field is an instance of some type T.

Lets say I have an object o. Now I want to know if it has any fields which are instance of T. I can get all fields with:

o.getClass().getFields(); 

I can get the type of the field with:

field.getType(); 

But now I want to know if this type or any supertype equals T. Do I have to call getSuperclass() recursively to be sure to check all supertypes?

like image 243
c0d3x Avatar asked Feb 14 '10 18:02

c0d3x


People also ask

How do you find the instance of a class using reflection?

We can use newInstance() method on the constructor object to instantiate a new instance of the class. Since we use reflection when we don't have the classes information at compile time, we can assign it to Object and then further use reflection to access it's fields and invoke it's methods.

When to use reflection in Java?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

What are possible uses of reflection?

Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object. Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.

Does Instanceof check for subclasses?

The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type.


2 Answers

You have to use isAssignableFrom.

like image 84
fastcodejava Avatar answered Sep 21 '22 06:09

fastcodejava


The rather baroquely-named Class.isAssignableFrom is what you're after. I usually end up having to read the javadoc to make sure I get it the right way around:

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. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion.

For example:

public class X {         public int i;     public static void main(String[] args) throws Exception {       Class<?> myType = Integer.class;       Object o = new X();              for (Field field : o.getClass().getFields()) {          if (field.getType().isAssignableFrom(myType)) {             System.out.println("Field " + field + " is assignable from type " + o.getClass());          }       }    } } 
like image 39
skaffman Avatar answered Sep 19 '22 06:09

skaffman