java.lang.reflect

  • 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 the fields of a Java class

    The Java reflection API, i.e. the set of classes defined in thejava.lang.reflect package, offers powerful tools to inspect at run-time the definition of any class used in a Java program. The usual starting point to inspect a class definition is acquiring ajava.lang.Class instance representing a given class; it is easy to obtain such an instance because every class defines implicitly a public field calledclass that contains a reference to the Class object representing this class. Among other things,java.lang.Class allows retrieving the definition of all the accessible public fields of a given class: the method to call to do so isgetFields(), which returns an array ofjava.lang.reflect.Field objects representing each of the (class or instance) fields.

    The Field class represents a particular field defined in a class or interface, and contains various methods to get or set the field value, or to get static information such as field name and type. In the following example we will retrieve the name of a field, using thegetName() method. The code first acquires the Class object corresponding to thejava.lang.String class, then retrieves the fields of the String class with a call to getFields(), and finally prints the name of the first field.

  • How to set field values using Java reflection

    In the Java reflection API, the java.lang.reflect.Field class represents a generic field in a class or interface, and allows to retrieve programmatically field information, such as name, type, or annotations. In addition, via the Field class we can get and set the value of a field in a given object.

    The most generic method to set a field value is set(Object obj, Object value): it takes as first argument the object on which the field needs to be set, and as second argument the value for the field. The above method can be used for any field type, either primitive or class type; in case of a primitive type, value must be an instance of the Java class corresponding to the type, such as java.lang.Integer, java.lang.Float, and so on. For primitive types, there also exist specific methods that can be called passing them directly primitive type values: