D.2. Using reflection

Reflection is done on a Class instance; Class is a Java entity for storing metadata about a Java platform class. If we have an object obj of any class, we can get a reference to its Class like this:

Class class = obj.getClass(); 

We can even get a Class reference for a noninstantiated class, like this:

Class class = Class.forName(”com.something.MyClass”); 

From the Class object we can find information about the methods and instance variables declared in the class, and its superclasses. For example, the call

Method[] m= class.getMethods(); 

will return an array of Method instances, each of which provides information about a method. Once we have established that an object has a given method, we make a call on that method ...

Get Applied Enterprise JavaBeans™ Technology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.