Alex Rivera | Logout

How can I determine whether a Java class is abstract by reflection

Asked 2009-07-02T06:58:32.600
200

I am interating through classes in a Jar file and wish to find those which are not abstract. I can solve this by instantiating the classes and trapping InstantiationException but that has a performance hit as some classes have heavy startup. I can't find anything obviously like isAbstract() in the Class.java docs.

Edit
Report

1 Answer

350

It'll have abstract as one of its modifiers when you call getModifiers() on the class object.

This link should help.

 Modifier.isAbstract( someClass.getModifiers() );

Also:

http://java.sun.com/javase/6/docs/api/java/lang/reflect/Modifier.html

http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getModifiers()

answered 2009-07-02T07:01:16.680

Your Answer