The java.lang.reflect Package
Contents:
java.lang.reflect.Array (JDK 1.1)
java.lang.reflect.Constructor (JDK 1.1)
java.lang.reflect.Field (JDK 1.1)
java.lang.reflect.InvocationTargetException (JDK 1.1)
java.lang.reflect.Member (JDK 1.1)
java.lang.reflect.Method (JDK 1.1)
java.lang.reflect.Modifier (JDK 1.1)
The java.lang.reflect package contains the classes and interfaces that, along with java.lang.Class, comprise the Java Reflection API. This package is new in Java 1.1. Figure 26.1 shows the class hierarchy.
The Constructor, Field, and Method classes represent constructors, fields, and methods of a class. Because these types all represent members of a class, they each implement the Member interface, which defines a simple set of methods that can be invoked for any class member. These classes allow information about the class members to be obtained, and allow methods and constructors to be invoked and fields to be queried and set.
Class member modifiers are represented as integers that specify a number of bit flags. The Modifier class defines static methods that are useful in interpreting the meanings of these flags. The Array class defines static methods for creating arrays and reading and writing array elements.
See Reflection for examples of using these classes.
Figure 26.1: The java.lang.reflect package
java.lang.reflect.Array (JDK 1.1)
This class contains methods that allow you to set and query the values of array elements, to determine the length of an array, and to create new instances of arrays. Note that the Array class is used for manipulating array values, not array types; Java data types, including array types, are represented by java.lang.Class. Since the Array class represents a Java value, unlike the Field, Method, and Constructor classes which represent class members, the Array class is significantly different (despite some surface similarities) from those other classes in this package. Most notably, all the methods of Array are static and apply to all array values, rather than applying only to a specific field, method, or constructor.
The get() method returns the value of the specified element of the specified array as an Object. If the array elements are of a primitive type, the value is converted to a wrapper object before being returned. You can also use getInteger() and related methods to query array elements and return them as specific primitive types. The set() method and its primitive type variants perform the opposite operation. Also, the getLength() method returns the length of the array.
The newInstance() methods create new arrays. One version of this method is passed the number of elements in the array and the type of those elements. The other version of this method creates multidimensional arrays. Besides just specifying the component type of the array, it is passed an array of numbers. The length of this array specifies the number of dimensions for the array to be created, and the values of each array element specify the size of each dimension of the created array.
public final classArrayextends Object { //No Constructor//Class Methodspublic static native Objectget(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native booleangetBoolean(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native bytegetByte(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native chargetChar(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native doublegetDouble(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native floatgetFloat(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native intgetInt(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native intgetLength(Objectarray) throws IllegalArgumentException; public static native longgetLong(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native shortgetShort(Objectarray, intindex) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static ObjectnewInstance(ClasscomponentType, intlength) throws NegativeArraySizeException; public static ObjectnewInstance(ClasscomponentType, int[]dimensions) throws IllegalArgumentException, NegativeArraySizeException; public static native voidset(Objectarray, intindex, Objectvalue) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetBoolean(Objectarray, intindex, booleanz) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetByte(Objectarray, intindex, byteb) Xsthrows IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetChar(Objectarray, intindex, charc) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetDouble(Objectarray, intindex, doubled) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetFloat(Objectarray, intindex, floatf) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetInt(Objectarray, intindex, inti) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetLong(Objectarray, intindex, longl) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; public static native voidsetShort(Objectarray, intindex, shorts) throws IllegalArgumentException, ArrayIndexOutOfBoundsException; }