/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/** <P> The master factory for all reflective objects, both those in
java.lang.reflect (Fields, Methods, Constructors) as well as their
delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).
</P>
<P> 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
{@link sun.misc.Unsafe}. </P>
*/
public class ReflectionFactory {
private static boolean initted = false;
= new RuntimePermission("reflectionFactoryAccess");
// Provides access to package-private mechanisms in java.lang.reflect
//
// "Inflation" mechanism. Loading bytecodes to implement
// Method.invoke() and Constructor.newInstance() currently costs
// 3-4x more than an invocation via native code for the first
// invocation (though subsequent invocations have been benchmarked
// to be over 20x faster). Unfortunately this cost increases
// startup time for certain applications that use reflection
// intensively (but only once per class) to bootstrap themselves.
// To avoid this penalty we reuse the existing JVM entry points
// for the first few invocations of Methods and Constructors and
// then switch to the bytecode-based implementations.
//
// Package-private to be accessible to NativeMethodAccessorImpl
// and NativeConstructorAccessorImpl
private static boolean noInflation = false;
private ReflectionFactory() {
}
/**
* A convenience class for acquiring the capability to instantiate
* reflective objects. Use this instead of a raw call to {@link
* #getReflectionFactory} in order to avoid being limited by the
* permissions of your callers.
*
* <p>An instance of this class can be used as the argument of
* <code>AccessController.doPrivileged</code>.
*/
public static final class GetReflectionFactoryAction
implements PrivilegedAction<ReflectionFactory> {
return getReflectionFactory();
}
}
/**
* Provides the caller with the capability to instantiate reflective
* objects.
*
* <p> First, if there is a security manager, its
* <code>checkPermission</code> method is called with a {@link
* java.lang.RuntimePermission} with target
* <code>"reflectionFactoryAccess"</code>. This may result in a
* security exception.
*
* <p> The returned <code>ReflectionFactory</code> 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.
*
* @exception SecurityException if a security manager exists and its
* <code>checkPermission</code> method doesn't allow
* access to the RuntimePermission "reflectionFactoryAccess". */
// TO DO: security.checkReflectionFactoryAccess();
}
return soleInstance;
}
//--------------------------------------------------------------------------
//
// Routines used by java.lang.reflect
//
//
/** Called only by java.lang.reflect.Modifier's static initializer */
}
/**
* Note: this routine can cause the declaring class for the field
* be initialized and therefore must not be called until the
* @param field the field
* @param override true if caller has overridden aaccessibility
*/
checkInitted();
}
checkInitted();
if (noInflation) {
return new MethodAccessorGenerator().
method.getModifiers());
} else {
return res;
}
}
checkInitted();
return new InstantiationExceptionConstructorAccessorImpl(null);
}
if (declaringClass == Class.class) {
("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.
ConstructorAccessorImpl.class)) {
return new BootstrapConstructorAccessorImpl(c);
}
if (noInflation) {
return new MethodAccessorGenerator().
c.getParameterTypes(),
c.getExceptionTypes(),
c.getModifiers());
} else {
new NativeConstructorAccessorImpl(c);
return res;
}
}
//--------------------------------------------------------------------------
//
// Routines used by java.lang
//
//
/** Creates a new java.lang.reflect.Field. Access checks as per
java.lang.reflect.AccessibleObject are not overridden. */
int modifiers,
int slot,
byte[] annotations)
{
name,
type,
slot,
}
/** Creates a new java.lang.reflect.Method. Access checks as per
java.lang.reflect.AccessibleObject are not overridden. */
Class<?>[] parameterTypes,
Class<?> returnType,
Class<?>[] checkedExceptions,
int modifiers,
int slot,
byte[] annotations,
byte[] parameterAnnotations,
byte[] annotationDefault)
{
name,
slot,
}
/** Creates a new java.lang.reflect.Constructor. Access checks as
per java.lang.reflect.AccessibleObject are not overridden. */
Class<?>[] parameterTypes,
Class<?>[] checkedExceptions,
int modifiers,
int slot,
byte[] annotations,
byte[] parameterAnnotations)
{
slot,
}
/** Gets the MethodAccessor object for a java.lang.reflect.Method */
return langReflectAccess().getMethodAccessor(m);
}
/** Sets the MethodAccessor object for a java.lang.reflect.Method */
}
/** Gets the ConstructorAccessor object for a
java.lang.reflect.Constructor */
return langReflectAccess().getConstructorAccessor(c);
}
/** Sets the ConstructorAccessor object for a
java.lang.reflect.Constructor */
{
}
/** 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. */
}
/** 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. */
}
/** 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. */
}
//--------------------------------------------------------------------------
//
// Routines used by serialization
//
//
{
// Fast path
return constructorToCall;
}
return c;
}
//--------------------------------------------------------------------------
//
// Internals only below this point
//
static int inflationThreshold() {
return inflationThreshold;
}
/** We have to defer full initialization of this class until after
the static initializer is run since java.lang.reflect.Method's
static initializer (more properly, that for
java.lang.reflect.AccessibleObject) causes this class's to be
run, before the system properties are set up. */
private static void checkInitted() {
if (initted) return;
new PrivilegedAction<Void>() {
// Tests to ensure the system properties table is fully
// initialized. This is needed because reflection code is
// called very early in the initialization process (before
// command-line arguments have been parsed and therefore
// these user-settable properties installed.) We assume that
// if System.out is non-null then the System class has been
// fully initialized and that the bulk of the startup code
// has been run.
// java.lang.System not yet fully initialized
return null;
}
noInflation = true;
}
try {
} catch (NumberFormatException e) {
throw (RuntimeException)
new RuntimeException("Unable to parse property sun.reflect.inflationThreshold").
initCause(e);
}
}
initted = true;
return null;
}
});
}
if (langReflectAccess == null) {
// Call a static method to get class java.lang.reflect.Modifier
// initialized. Its static initializer will cause
// setLangReflectAccess() to be called from the context of the
// java.lang.reflect package.
}
return langReflectAccess;
}
}