Reflections: Obj Instanceof Dog Dog D Dog Obj D
Reflections: Obj Instanceof Dog Dog D Dog Obj D
Class<?> c = Class.forName("classpath.and.classname");
Object dog = c.newInstance();
Method m = c.getDeclaredMethod("bark", new Class<?>[0]);
m.invoke(dog);
In Java, reflection is more about introspection, because you can not change
structure of an object. There are some APIs to change accessibilities of
methods and fields, but not structures.
Reflection enables us to:
Reflection Classes:
Using Java Reflection you can inspect Java classes at runtime. Inspecting
classes is often the first thing you do when using Reflection. From the classes
you can obtain information about
Class Name
Package Info
Super class
Implemented Interfaces
Constructors
Methods
Fields
Annotations
plus a lot more information related to Java classes. For a full list you should
consult the JavaDoc for java.lang.Class. This text will briefly touch upon all
accessing of the above mentioned information. Some of the topics will also
be examined in greater detail in separate texts. For instance, this text will
show you how to obtain all methods or a specific method, but a separate text
will show you how to invoke that method, how to find the method matching a
given set of arguments if more than one method exists with the same name,
what exceptions are thrown from method invocation via reflection, how to
spot a getter/setter etc. The purpose of this text is primarily to introduce
the Class object and the information you can obtain from it.
Superclass
From the Class object you can access the superclass of the class. Here is
how:
Class superclass = aClass.getSuperclass();
The superclass class object is a Class object like any other, so you can
continue doing class reflection on that too.
Implemented Interfaces
It is possible to get a list of the interfaces implemented by a given class.
Here is how:
Class aClass = ... //obtain Class object. See prev. section
Class[] interfaces = aClass.getInterfaces();
A class can implement many interfaces. Therefore an array of Class is
returned. Interfaces are also represented byClass objects in Java Reflection.
NOTE: Only the interfaces specifically declared implemented by a given class
is returned. If a superclass of the class implements an interface, but the class
doesn't specifically state that it also implements that interface, that interface
will not be returned in the array. Even if the class in practice implements that
interface, because the superclass does.
To get a complete list of the interfaces implemented by a given class you will
have to consult both the class and its superclasses recursively.
Constructors
You can access the constructors of a class like this:
Constructor[] constructors = aClass.getConstructors();
Constructors are covered in more detail in the text on Constructors.
Methods
You can access the methods of a class like this:
Method[] method = aClass.getMethods();
Methods are covered in more detail in the text on Methods.
Constructor Parameters
You can read what parameters a given constructor takes like this:
Constructor constructor = ... // obtain constructor - see above
Class[] parameterTypes = constructor.getParameterTypes();
Method Parameters and Return Types
You can read what parameters a given method takes like this:
Method method = ... // obtain method - see above
Class[] parameterTypes = method.getParameterTypes();
You can access the return type of a method like this:
Method method = ... // obtain method - see above