The master factory for all reflective objects, both those in
java.lang.reflect (Fields, Methods, Constructors) as well as their
delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).
The methods in this class are extremely unsafe and can cause
subversion of both the language and the verifier. For this reason,
they are all instance methods, and access to the constructor of
this factory is guarded by a security check, in similar style to
sun.misc.Unsafe .
Method from sun.reflect.ReflectionFactory Detail: |
public Constructor<T> copyConstructor(Constructor<T> arg) {
return langReflectAccess().copyConstructor(arg);
}
Makes a copy of the passed constructor. The returned
constructor is a "child" of the passed one; see the comments
in Constructor.java for details. |
public Field copyField(Field arg) {
return langReflectAccess().copyField(arg);
}
Makes a copy of the passed field. The returned field is a
"child" of the passed one; see the comments in Field.java for
details. |
public Method copyMethod(Method arg) {
return langReflectAccess().copyMethod(arg);
}
Makes a copy of the passed method. The returned method is a
"child" of the passed one; see the comments in Method.java for
details. |
public ConstructorAccessor getConstructorAccessor(Constructor c) {
return langReflectAccess().getConstructorAccessor(c);
}
Gets the ConstructorAccessor object for a
java.lang.reflect.Constructor |
public MethodAccessor getMethodAccessor(Method m) {
return langReflectAccess().getMethodAccessor(m);
}
Gets the MethodAccessor object for a java.lang.reflect.Method |
public static ReflectionFactory getReflectionFactory() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
// TO DO: security.checkReflectionFactoryAccess();
security.checkPermission(reflectionFactoryAccessPerm);
}
return soleInstance;
}
Provides the caller with the capability to instantiate reflective
objects.
First, if there is a security manager, its
checkPermission method is called with a java.lang.RuntimePermission with target
"reflectionFactoryAccess" . This may result in a
security exception.
The returned ReflectionFactory object should be
carefully guarded by the caller, since it can be used to read and
write private data and invoke private methods, as well as to load
unverified bytecodes. It must never be passed to untrusted code. |
static int inflationThreshold() {
return inflationThreshold;
}
|
public Constructor newConstructor(Class<?> declaringClass,
Class<?>[] parameterTypes,
Class<?>[] checkedExceptions,
int modifiers,
int slot,
String signature,
byte[] annotations,
byte[] parameterAnnotations) {
return langReflectAccess().newConstructor(declaringClass,
parameterTypes,
checkedExceptions,
modifiers,
slot,
signature,
annotations,
parameterAnnotations);
}
Creates a new java.lang.reflect.Constructor. Access checks as
per java.lang.reflect.AccessibleObject are not overridden. |
public ConstructorAccessor newConstructorAccessor(Constructor c) {
checkInitted();
Class< ? > declaringClass = c.getDeclaringClass();
if (Modifier.isAbstract(declaringClass.getModifiers())) {
return new InstantiationExceptionConstructorAccessorImpl(null);
}
if (declaringClass == Class.class) {
return new InstantiationExceptionConstructorAccessorImpl
("Can not instantiate java.lang.Class");
}
// Bootstrapping issue: since we use Class.newInstance() in
// the ConstructorAccessor generation process, we have to
// break the cycle here.
if (Reflection.isSubclassOf(declaringClass,
ConstructorAccessorImpl.class)) {
return new BootstrapConstructorAccessorImpl(c);
}
if (noInflation) {
return new MethodAccessorGenerator().
generateConstructor(c.getDeclaringClass(),
c.getParameterTypes(),
c.getExceptionTypes(),
c.getModifiers());
} else {
NativeConstructorAccessorImpl acc =
new NativeConstructorAccessorImpl(c);
DelegatingConstructorAccessorImpl res =
new DelegatingConstructorAccessorImpl(acc);
acc.setParent(res);
return res;
}
}
|
public Constructor newConstructorForSerialization(Class<?> classToInstantiate,
Constructor constructorToCall) {
// Fast path
if (constructorToCall.getDeclaringClass() == classToInstantiate) {
return constructorToCall;
}
ConstructorAccessor acc = new MethodAccessorGenerator().
generateSerializationConstructor(classToInstantiate,
constructorToCall.getParameterTypes(),
constructorToCall.getExceptionTypes(),
constructorToCall.getModifiers(),
constructorToCall.getDeclaringClass());
Constructor c = newConstructor(constructorToCall.getDeclaringClass(),
constructorToCall.getParameterTypes(),
constructorToCall.getExceptionTypes(),
constructorToCall.getModifiers(),
langReflectAccess().
getConstructorSlot(constructorToCall),
langReflectAccess().
getConstructorSignature(constructorToCall),
langReflectAccess().
getConstructorAnnotations(constructorToCall),
langReflectAccess().
getConstructorParameterAnnotations(constructorToCall));
setConstructorAccessor(c, acc);
return c;
}
|
public Field newField(Class<?> declaringClass,
String name,
Class<?> type,
int modifiers,
int slot,
String signature,
byte[] annotations) {
return langReflectAccess().newField(declaringClass,
name,
type,
modifiers,
slot,
signature,
annotations);
}
Creates a new java.lang.reflect.Field. Access checks as per
java.lang.reflect.AccessibleObject are not overridden. |
public FieldAccessor newFieldAccessor(Field field,
boolean override) {
checkInitted();
return UnsafeFieldAccessorFactory.newFieldAccessor(field, override);
}
Note: this routine can cause the declaring class for the field
be initialized and therefore must not be called until the
first get/set of this field. |
public Method newMethod(Class<?> declaringClass,
String name,
Class<?>[] parameterTypes,
Class<?> returnType,
Class<?>[] checkedExceptions,
int modifiers,
int slot,
String signature,
byte[] annotations,
byte[] parameterAnnotations,
byte[] annotationDefault) {
return langReflectAccess().newMethod(declaringClass,
name,
parameterTypes,
returnType,
checkedExceptions,
modifiers,
slot,
signature,
annotations,
parameterAnnotations,
annotationDefault);
}
Creates a new java.lang.reflect.Method. Access checks as per
java.lang.reflect.AccessibleObject are not overridden. |
public MethodAccessor newMethodAccessor(Method method) {
checkInitted();
if (noInflation) {
return new MethodAccessorGenerator().
generateMethod(method.getDeclaringClass(),
method.getName(),
method.getParameterTypes(),
method.getReturnType(),
method.getExceptionTypes(),
method.getModifiers());
} else {
NativeMethodAccessorImpl acc =
new NativeMethodAccessorImpl(method);
DelegatingMethodAccessorImpl res =
new DelegatingMethodAccessorImpl(acc);
acc.setParent(res);
return res;
}
}
|
public void setConstructorAccessor(Constructor c,
ConstructorAccessor accessor) {
langReflectAccess().setConstructorAccessor(c, accessor);
}
Sets the ConstructorAccessor object for a
java.lang.reflect.Constructor |
public void setLangReflectAccess(LangReflectAccess access) {
langReflectAccess = access;
}
Called only by java.lang.reflect.Modifier's static initializer |
public void setMethodAccessor(Method m,
MethodAccessor accessor) {
langReflectAccess().setMethodAccessor(m, accessor);
}
Sets the MethodAccessor object for a java.lang.reflect.Method |