October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Java Reflection in Action, Part 2

  • May 25, 2005
  • By Dr. Ira Forman & Nate Forman
  • Send Email »
  • More Articles »
Editor's Note: This piece picks up where the article Java Reflection in Action left off.]

1.5 Representing types with class objects

The discussion of the methods from table 1 indicates that Java reflection uses instances of Class to represent types. For example, getMethod from listing 1 uses an array of Class to indicate the types of the parameters of the desired method. This seems fine for methods that take objects as parameters, but what about types not created by a class declaration?

Table 1.1 The methods defined by Class for method query

Method Description
Method getMethod ( String name,
                   Class[] parameterTypes )
Returns a Method object that represents a public method (either declared or inherited) of the target Class object with the signature specified by the second parameters
Method[] getMethods ()
Returns an array of Method objects that represent all of the public methods (either declared or inherited) supported by the target Class object
Method getDeclaredMethod (
                 String name,
                 Class[] parameterTypes )
Returns a Method object that represents a declared method of the target Class object with the signature specified by the second parameters
Method[] getDeclaredMethods ()
Returns an array of Method objects that represent all of the methods declared by the target Classobject

Listing 1 George's setObjectColor code



Click here for a larger image.

Consider listing 2, which shows a fragment of java.util.Vector. One method has an interface type as a parameter, another an array, and the third a primitive. To program effectively with reflection, you must know how to introspect on classes such as Vector that have methods with such parameters.

Listing 2 A fragment of java.util.Vector.

public class Vector ... {
public synchronized boolean       addAll( Collection c ) ...
public synchronized void          copyInto( Object[] anArray ) ...
public synchronized Object        get( int index ) ...
}

Java represents primitive, array, and interface types by introducing class objects to represent them. These class objects cannot do everything that many other class objects can. For instance, you cannot create a new instance of a primitive or interface. However, such class objects are necessary for performing introspection. Table 2 shows the methods of Class that support type representation.

Table 2 Methods defined by Class that deal with type representation

Method Description
String getName() Returns the fully qualified name of the target Class object
Class getComponentType() If the target object is a Class object for an array, returns the Class object representing the component type
boolean isArray() Returns true if and only if the target Class object represents an array
boolean isInterface() Returns true if and only if the target Class object represents an interface
boolean isPrimitive() Returns true if and only if the target Class object represents a primitive type or void.

The rest of this section explains in greater detail how Java represents primitive, interface, and array types using class objects. By the end of this section, you should know how to use methods such as getMethod to introspect on Vector.class for the methods shown in listing 2.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel