|Contents | Prev | Next||Java Core Reflection|
Constructor-that reflect class and interface members and constructors. These classes provide:
Classthat provide for the construction of new instances of the
Array-that provides methods to dynamically construct and access Java arrays.
Modifier-that helps decode Java language modifier information about classes and their members.
java.langpackage that support reflection. These additions are:
Short. These new classes are subclasses of the class
Number, and are similar to the class
Integer. Instances of these new classes serve as object wrappers for primitive values of type
Class, to represent the primitive Java types
double, and the keyword
void, at run-time.
Void-to hold a reference to the
Classobject representing the keyword
One category is comprised of applications that need to discover and use all of
public members of a target object based on its run-time class. These
applications require run-time access to all the
public fields, methods, and
constructors of an object. Examples in this category are services such as Java(TM)
Beans, and lightweight tools, such as object inspectors. These applications
use the instances of the classes
through the methods
getConstructors of class
The second category consists of sophisticated applications that need to
discover and use the members declared by a given class. These applications
need run-time access to the implementation of a class at the level provided by
class file. Examples in this category are development tools, such as
debuggers, interpreters, inspectors, and class browsers, and run-time services,
such as Java(TM) Object Serialization. These applications use instances of the
Constructor obtained through the methods
final. Only the Java Virtual Machine may create instances of these classes; these objects are used to manipulate the underlying objects; that is, to:
staticmethods that permit creating new arrays, and getting and setting the elements of arrays.
Memberinterface. The methods of
Memberare used to query a reflected member for basic identifying information. Identifying information consists of the class or interface that declared the member, the name of the member itself, and the Java language modifiers (such as
synchronized, and so on) for the member.
Fieldobject represents a reflected field. The underlying field may be a class variable (a
staticfield) or an instance variable (a non-
staticfield). Methods of class
Fieldare used to obtain the type of the underlying field, and to get and set the underlying field's value on objects.
Methodobject represents a reflected method. The underlying method may be an abstract method, an instance method, or a class (
Methods of class
Method are used to obtain the formal parameter types, the
return type, and the checked exception types of the underlying method. In
invoke method of class
Method is used to invoke the underlying
method on target objects. Instance and abstract method invocation uses
dynamic method resolution based on the target object's run-time class and the
reflected method's declaring class, name, and formal parameter types. (Thus, it
is permissible to invoke a reflected interface method on an object that is an
instance of a class that implements the interface.) Static method invocation
uses the underlying static method of the method's declaring class.
Constructorobject represents a reflected constructor. Methods of class
Constructorare used to obtain the formal parameter types and the checked exception types of the underlying constructor. In addition, the
newInstancemethod of class
Constructoris used to create and initialize a new instance of the class that declares the constructor, provided the class is instantiable.
Arrayclass is an uninstantiable class that exports class methods to create Java arrays with primitive or class component types. Methods of class
Arrayare also used to get and set array component values.
Modifier class is an uninstantiable class that exports class methods to
decode Java language modifiers for classes and members. The language
modifiers are encoded in an integer, and use the encoding constants defined by
The Java Virtual Machine Specification.
Classobjects that are used to represent the eight primitive Java types and
voidat run-time. (Note that these are
Classobjects, not classes.) The Core Reflection API uses these objects to identify the following:
Classobjects. They have the same names as the types that they represent. The
Classobjects may only be referenced via the following
java.lang.Boolean.TYPE java.lang.Character.TYPE java.lang.Byte.TYPE java.lang.Short.TYPE java.lang.Integer.TYPE java.lang.Long.TYPE java.lang.Float.TYPE java.lang.Double.TYPE java.lang.Void.TYPEIn particular, these
Classobjects are not accessible via the
forNamemethod of class
Classthat give reflective access to a member or a set of members of a class are the only source for instances of
Constructor. These methods first delegate security checking to the system security manager (if installed), which throws a
SecurityExceptionshould the request for reflective access be denied.
protected, default (package) access, and
privateclasses and members-will normally occur when the individual reflected members are used to operate on the underlying members of objects,that is, to get or set field values, to invoke methods, or to create and initialize new objects. Unrestricted access, which overrides standard language access control rules, may be granted to privileged code (such as debugger code)-a future version of this specification will define the interface by which this may be accomplished.
void checkMemberAccess(Class,int) throws SecurityException
Classparameter identifies the class or interface whose members need to be accessed. The
intparameter identifies the set of members to be accessed-either
If the requested access to the specified set of members of the specified class is
denied, the method should throw a
SecurityException. If the requested
access to the set is granted, the method should return.
As stated earler, standard Java language access control will be enforced when a reflected member from this set is used to operate on an underlying object, that is, when:
Fieldis used to get or set a field value
Methodis used to invoke a method
Constructoris used to create and initialize a new instance of a class
publicmembers and constructors) of any class it may link against. Application code that gains reflective access to a member or constructor may only use the reflected member or constructor with standard Java language access control.
AppletSecurityimplements the following policy:
publicmembers of all
publicclasses loaded by the same class loader as the untrusted code
public) members of all classes loaded by the same class loader as the untrusted code
CLASSPATH, is additionally granted access to all classes loaded by all class loaders.
This policy is conservative with respect to untrusted code-it is more
restrictive than the linker for the Java Virtual Machine. For example, an
untrusted class cannot, by itself, access a
protected member of a system
superclass via reflection, although it can via the linker. (However, system code
may access such members and pass them to untrusted code.)
The JDK security policy is expected to evolve with the security framework for Java.
There are two types of automatic data conversions. Wrapping conversions convert from values of primitive types to objects of class types. Unwrapping conversions convert objects of class types to values of primitive types. The rules for these conversions are defined in "Wrapping and Unwrapping Conversions."
Additionally, field access and method invocation permit widening conversions on primitive and reference types. These conversions are documented in The Java Language Specification, section 5, and are detailed in "Widening Conversions."
Array.get, or when it is returned by a method invoked via
Similarly, an object value is automatically unwrapped when supplied as a parameter in a context that requires a value of a primitive type. These contexts are:
Field.set, where the underlying field has a primitive type
Array.set, where the underlying array has a primitive element type
Constructor.newInstance, where the corresponding formal parameter of the underlying method or constructor has a primitive type
A method that is declared
void returns the special reference
null when it is
Widening conversions are performed at run-time:
java.lang.reflect. This avoids compatibility problems caused by Java's default package importation rules.