The Modifier#isAbstract Method In the example above, we first obtain the instance of the class we want to test. Once we have the class reference, we can call the Modifier#isAbstract method. As we'd expect, it returns true if the class is abstract, and otherwise, it returns false.
In order to reflect a Java class, we first need to create an object of Class . And, using the object we can call various methods to get information about methods, fields, and constructors present in a class. class Dog {...} // create object of Class // to reflect the Dog class Class a = Class. forName("Dog");
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.
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()
Class myClass = myJar.load("classname");
bool test = Modifier.isAbstract(myClass.getModifiers());
public static boolean isInstantiable(Class<?> clz) {
if(clz.isPrimitive() || Modifier.isAbstract( clz.getModifiers()) ||clz.isInterface() || clz.isArray() || String.class.getName().equals(clz.getName()) || Integer.class.getName().equals(clz.getName())){
return false;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With