• How to invoke Java methods using reflection

    In the Java reflection API, a method of a class or interface is represented by the java.lang.reflect.Method class. An object of this class can reflect either a class method or an instance method. The Method class allows not only retrieving information about a given method, such as its name, its parameter list, its return type, the exceptions it can throw, etc., but also invoking the method on a given object.

    A Method object representing a method in a given class is retrieved from the corresponding java.lang.Class object, by calling either getMethods(), which returns an array of objects representing the public methods of the class, or getMethod(String name, Class<?>... parameterTypes), which takes as arguments the method name and the types of its parameters (in the order in which they are defined) and returns the object representing the requested method. Once this object has been retrieved, the method it represents can be invoked by calling invoke(Object obj, Object... args), which takes as arguments the object on which to invoke the method and the list of arguments (if any) to be passed to the method invocation, and returns the return value from the invocation.

    The simple example below shoes how to call a method using reflection.

  • How to retrieve field values using Java reflection

    Values of public member fields in a Java object can be obtained (and are usually obtained) with the notation<objectName>.<fieldName>. But Java offers another possibility to do that: using the reflection API it is possible to retrieve ajava.lang.reflect.Field object representing a given field and to call specific methods of the Field class to obtain field values. While this process is clearly not necessary in the majority of cases where programmers know a priori the definition of classes they are using in a given program, if such definition is not known (or is only partially known) until when the program is executed the reflection API represents a valuable tool for dealing with unknown classes.

    Differently from the usual case where a class definition is available at build time, the fields of a given class accessed through the reflection API can have an arbitrary type, thus the Field class provides a number of methods to retrieve field values of different types; all of these methods take as their unique argument the object from which a field value has to be retrieved. Below are listed the methods that allow retrieving each Java primitive type:

  • How to retrieve the constructors of a Java class

    In object-oriented programming, a class constructor is a method called every time an object of a given class is created. In Java, a class can have more than one constructor, and different constructors differ in the list of parameters they accept. Thejava.lang.Class<T> class, which contains the definition of an arbitraryT class, offers various methods to inspect a class; one of these methods isgetConstructors(), which returns an array ofjava.lang.reflect.Constructor objects representing each of the public constructors of a given class.

    The constructor class allows retrieving information about a given constructor method, such as its parameters, the type of exceptions it can throw, etc. In the following example we demonstrate how to obtain the name of a constructor for thejava.lang.String class, by using the getName() method of the Constructor class: