• How to find the superclass name by using the getSuperclass() method

    In Java, class hierarchies follow the single inheritance model, i.e. a class can have at most one superclass. The only class that does not have any superclass isjava.lang.Object, while all other classes, including user-defined classes, derive (directly or indirectly) from Object. Thejava.lang.Class class provides a method, called getSuperclass(), to retrieve the direct superclass (i.e. the next class encountered when walking up the hierarchy) of a given class; this method returns a Class object representing the superclass of the Class object on which the method is called; if the method is called on the Object class,null is returned.

  • 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 Java interfaces by using the getInterfaces() method

    In Java, an interface is a set of functionalities that an object of a given class is expected to implement. A class is said to implement an interface when it contains all the methods defined in the interface. An interface can extend another interface, thus there are hierarchies of interfaces similarly to class hierarchies. Differently from the single inheritance model characterizing relationships between classes, a class can implement more than one interface, and an interface can extend more than one interface.

    Thejava.lang.Class class provides a method to retrieve the interfaces implemented in a given class. This method is calledgetInterfaces() and returns an array (possibly with zero length) of Class objects representing the different interfaces implemented by a given class. It is worth noting that Class objects can refer not only to Java classes, but also to interfaces. If a Class object represents an interface, calling getInterfaces() on it returns an array with all the interfaces extended by this interface.

  • How to retrieve the component type of an array

    In Java, every array is an object, regardless of whether array elements are objects or primitive types. As a consequence, every array has a corresponding java.lang.Class object, which can be retrieved by calling the getClass() method of java.lang.Object. From the Class object associated to an array, we can retrieve the type of array elements via the getComponentType()method, which returns the Class object representing the element type. Class objects can represent not only classes and interfaces, but also primitive types, so the getComponentType() method works for any array object; if the object on which this method is called does not represent an array, then the method returns null.

    In the following example, we first declare two arrays, and then retrieve their component types and print them on the standard output.

  • 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.