Search in sources :

Example 11 with CallerSensitive

use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.

the class Class method forName.

/**
 * Returns the {@code Class} object associated with the class or
 * interface with the given string name, using the given class loader.
 * Given the fully qualified name for a class or interface (in the same
 * format returned by {@code getName}) this method attempts to
 * locate, load, and link the class or interface.  The specified class
 * loader is used to load the class or interface.  If the parameter
 * {@code loader} is null, the class is loaded through the bootstrap
 * class loader.  The class is initialized only if the
 * {@code initialize} parameter is {@code true} and if it has
 * not been initialized earlier.
 *
 * <p> If {@code name} denotes a primitive type or void, an attempt
 * will be made to locate a user-defined class in the unnamed package whose
 * name is {@code name}. Therefore, this method cannot be used to
 * obtain any of the {@code Class} objects representing primitive
 * types or void.
 *
 * <p> If {@code name} denotes an array class, the component type of
 * the array class is loaded but not initialized.
 *
 * <p> For example, in an instance method the expression:
 *
 * <blockquote>
 *  {@code Class.forName("Foo")}
 * </blockquote>
 *
 * is equivalent to:
 *
 * <blockquote>
 *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
 * </blockquote>
 *
 * Note that this method throws errors related to loading, linking or
 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
 * Java Language Specification</em>.
 * Note that this method does not check whether the requested class
 * is accessible to its caller.
 *
 * @param name       fully qualified name of the desired class
 * @param initialize if {@code true} the class will be initialized.
 *                   See Section 12.4 of <em>The Java Language Specification</em>.
 * @param loader     class loader from which the class must be loaded
 * @return           class object representing the desired class
 *
 * @exception LinkageError if the linkage fails
 * @exception ExceptionInInitializerError if the initialization provoked
 *            by this method fails
 * @exception ClassNotFoundException if the class cannot be located by
 *            the specified class loader
 * @exception SecurityException
 *            if a security manager is present, and the {@code loader} is
 *            {@code null}, and the caller's class loader is not
 *            {@code null}, and the caller does not have the
 *            {@link RuntimePermission}{@code ("getClassLoader")}
 *
 * @see       java.lang.Class#forName(String)
 * @see       java.lang.ClassLoader
 * @since     1.2
 */
@CallerSensitive
public static Class<?> forName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException {
    Class<?> caller = null;
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        // Reflective call to get caller class is only needed if a security manager
        // is present.  Avoid the overhead of making this call otherwise.
        caller = Reflection.getCallerClass();
        if (loader == null) {
            ClassLoader ccl = ClassLoader.getClassLoader(caller);
            if (ccl != null) {
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
            }
        }
    }
    return forName0(name, initialize, loader, caller);
}
Also used : BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) CallerSensitive(jdk.internal.reflect.CallerSensitive)

Example 12 with CallerSensitive

use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.

the class Class method getClassLoader.

/**
 * Returns the class loader for the class.  Some implementations may use
 * null to represent the bootstrap class loader. This method will return
 * null in such implementations if this class was loaded by the bootstrap
 * class loader.
 *
 * <p>If this object
 * represents a primitive type or void, null is returned.
 *
 * @return  the class loader that loaded the class or interface
 *          represented by this object.
 * @throws  SecurityException
 *          if a security manager is present, and the caller's class loader
 *          is not {@code null} and is not the same as or an ancestor of the
 *          class loader for the class whose class loader is requested,
 *          and the caller does not have the
 *          {@link RuntimePermission}{@code ("getClassLoader")}
 * @see java.lang.ClassLoader
 * @see SecurityManager#checkPermission
 * @see java.lang.RuntimePermission
 */
@CallerSensitive
// to ensure Reflection.getCallerClass optimization
@ForceInline
public ClassLoader getClassLoader() {
    ClassLoader cl = getClassLoader0();
    if (cl == null)
        return null;
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
    }
    return cl;
}
Also used : BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) ForceInline(jdk.internal.vm.annotation.ForceInline) CallerSensitive(jdk.internal.reflect.CallerSensitive)

Example 13 with CallerSensitive

use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.

the class Class method getField.

/**
 * Returns a {@code Field} object that reflects the specified public member
 * field of the class or interface represented by this {@code Class}
 * object. The {@code name} parameter is a {@code String} specifying the
 * simple name of the desired field.
 *
 * <p> The field to be reflected is determined by the algorithm that
 * follows.  Let C be the class or interface represented by this object:
 *
 * <OL>
 * <LI> If C declares a public field with the name specified, that is the
 *      field to be reflected.</LI>
 * <LI> If no field was found in step 1 above, this algorithm is applied
 *      recursively to each direct superinterface of C. The direct
 *      superinterfaces are searched in the order they were declared.</LI>
 * <LI> If no field was found in steps 1 and 2 above, and C has a
 *      superclass S, then this algorithm is invoked recursively upon S.
 *      If C has no superclass, then a {@code NoSuchFieldException}
 *      is thrown.</LI>
 * </OL>
 *
 * <p> If this {@code Class} object represents an array type, then this
 * method does not find the {@code length} field of the array type.
 *
 * @param name the field name
 * @return the {@code Field} object of this class specified by
 *         {@code name}
 * @throws NoSuchFieldException if a field with the specified name is
 *         not found.
 * @throws NullPointerException if {@code name} is {@code null}
 * @throws SecurityException
 *         If a security manager, <i>s</i>, is present and
 *         the caller's class loader is not the same as or an
 *         ancestor of the class loader for the current class and
 *         invocation of {@link SecurityManager#checkPackageAccess
 *         s.checkPackageAccess()} denies access to the package
 *         of this class.
 *
 * @since 1.1
 * @jls 8.2 Class Members
 * @jls 8.3 Field Declarations
 */
@CallerSensitive
public Field getField(String name) throws NoSuchFieldException, SecurityException {
    Objects.requireNonNull(name);
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
    }
    Field field = getField0(name);
    if (field == null) {
        throw new NoSuchFieldException(name);
    }
    return getReflectionFactory().copyField(field);
}
Also used : ObjectStreamField(java.io.ObjectStreamField) Field(java.lang.reflect.Field) CallerSensitive(jdk.internal.reflect.CallerSensitive)

Example 14 with CallerSensitive

use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.

the class Class method getEnclosingMethod.

/**
 * If this {@code Class} object represents a local or anonymous
 * class within a method, returns a {@link
 * java.lang.reflect.Method Method} object representing the
 * immediately enclosing method of the underlying class. Returns
 * {@code null} otherwise.
 *
 * In particular, this method returns {@code null} if the underlying
 * class is a local or anonymous class immediately enclosed by a type
 * declaration, instance initializer or static initializer.
 *
 * @return the immediately enclosing method of the underlying class, if
 *     that class is a local or anonymous class; otherwise {@code null}.
 *
 * @throws SecurityException
 *         If a security manager, <i>s</i>, is present and any of the
 *         following conditions is met:
 *
 *         <ul>
 *
 *         <li> the caller's class loader is not the same as the
 *         class loader of the enclosing class and invocation of
 *         {@link SecurityManager#checkPermission
 *         s.checkPermission} method with
 *         {@code RuntimePermission("accessDeclaredMembers")}
 *         denies access to the methods within the enclosing class
 *
 *         <li> the caller's class loader is not the same as or an
 *         ancestor of the class loader for the enclosing class and
 *         invocation of {@link SecurityManager#checkPackageAccess
 *         s.checkPackageAccess()} denies access to the package
 *         of the enclosing class
 *
 *         </ul>
 * @since 1.5
 */
@CallerSensitive
public Method getEnclosingMethod() throws SecurityException {
    EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
    if (enclosingInfo == null)
        return null;
    else {
        if (!enclosingInfo.isMethod())
            return null;
        MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(), getFactory());
        Class<?> returnType = toClass(typeInfo.getReturnType());
        Type[] parameterTypes = typeInfo.getParameterTypes();
        Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
        // don't have generics information
        for (int i = 0; i < parameterClasses.length; i++) parameterClasses[i] = toClass(parameterTypes[i]);
        // Perform access check
        final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            enclosingCandidate.checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
        }
        Method[] candidates = enclosingCandidate.privateGetDeclaredMethods(false);
        /*
             * Loop over all declared methods; match method name,
             * number of and type of parameters, *and* return
             * type.  Matching return type is also necessary
             * because of covariant returns, etc.
             */
        ReflectionFactory fact = getReflectionFactory();
        for (Method m : candidates) {
            if (m.getName().equals(enclosingInfo.getName()) && arrayContentsEq(parameterClasses, fact.getExecutableSharedParameterTypes(m))) {
                // finally, check return type
                if (m.getReturnType().equals(returnType)) {
                    return fact.copyMethod(m);
                }
            }
        }
        throw new InternalError("Enclosing method not found");
    }
}
Also used : Method(java.lang.reflect.Method) GenericArrayType(java.lang.reflect.GenericArrayType) AnnotatedType(java.lang.reflect.AnnotatedType) Type(java.lang.reflect.Type) MethodRepository(sun.reflect.generics.repository.MethodRepository) CoreReflectionFactory(sun.reflect.generics.factory.CoreReflectionFactory) ReflectionFactory(jdk.internal.reflect.ReflectionFactory) CallerSensitive(jdk.internal.reflect.CallerSensitive)

Example 15 with CallerSensitive

use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.

the class Class method getMethod.

/**
 * Returns a {@code Method} object that reflects the specified public
 * member method of the class or interface represented by this
 * {@code Class} object. The {@code name} parameter is a
 * {@code String} specifying the simple name of the desired method. The
 * {@code parameterTypes} parameter is an array of {@code Class}
 * objects that identify the method's formal parameter types, in declared
 * order. If {@code parameterTypes} is {@code null}, it is
 * treated as if it were an empty array.
 *
 * <p> If this {@code Class} object represents an array type, then this
 * method finds any public method inherited by the array type from
 * {@code Object} except method {@code clone()}.
 *
 * <p> If this {@code Class} object represents an interface then this
 * method does not find any implicitly declared method from
 * {@code Object}. Therefore, if no methods are explicitly declared in
 * this interface or any of its superinterfaces, then this method does not
 * find any method.
 *
 * <p> This method does not find any method with name "{@code <init>}" or
 * "{@code <clinit>}".
 *
 * <p> Generally, the method to be reflected is determined by the 4 step
 * algorithm that follows.
 * Let C be the class or interface represented by this {@code Class} object:
 * <ol>
 * <li> A union of methods is composed of:
 *   <ol type="a">
 *   <li> C's declared public instance and static methods as returned by
 *        {@link #getDeclaredMethods()} and filtered to include only public
 *        methods that match given {@code name} and {@code parameterTypes}</li>
 *   <li> If C is a class other than {@code Object}, then include the result
 *        of invoking this algorithm recursively on the superclass of C.</li>
 *   <li> Include the results of invoking this algorithm recursively on all
 *        direct superinterfaces of C, but include only instance methods.</li>
 *   </ol></li>
 * <li> This union is partitioned into subsets of methods with same
 *      return type (the selection of methods from step 1 also guarantees that
 *      they have the same method name and parameter types).</li>
 * <li> Within each such subset only the most specific methods are selected.
 *      Let method M be a method from a set of methods with same VM
 *      signature (return type, name, parameter types).
 *      M is most specific if there is no such method N != M from the same
 *      set, such that N is more specific than M. N is more specific than M
 *      if:
 *   <ol type="a">
 *   <li> N is declared by a class and M is declared by an interface; or</li>
 *   <li> N and M are both declared by classes or both by interfaces and
 *        N's declaring type is the same as or a subtype of M's declaring type
 *        (clearly, if M's and N's declaring types are the same type, then
 *        M and N are the same method).</li>
 *   </ol></li>
 * <li> The result of this algorithm is chosen arbitrarily from the methods
 *      with most specific return type among all selected methods from step 3.
 *      Let R be a return type of a method M from the set of all selected methods
 *      from step 3. M is a method with most specific return type if there is
 *      no such method N != M from the same set, having return type S != R,
 *      such that S is a subtype of R as determined by
 *      R.class.{@link #isAssignableFrom}(S.class).
 * </ol>
 *
 * @apiNote There may be more than one method with matching name and
 * parameter types in a class because while the Java language forbids a
 * class to declare multiple methods with the same signature but different
 * return types, the Java virtual machine does not.  This
 * increased flexibility in the virtual machine can be used to
 * implement various language features.  For example, covariant
 * returns can be implemented with {@linkplain
 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
 * method and the overriding method would have the same
 * signature but different return types. This method would return the
 * overriding method as it would have a more specific return type.
 *
 * @param name the name of the method
 * @param parameterTypes the list of parameters
 * @return the {@code Method} object that matches the specified
 *         {@code name} and {@code parameterTypes}
 * @throws NoSuchMethodException if a matching method is not found
 *         or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
 * @throws NullPointerException if {@code name} is {@code null}
 * @throws SecurityException
 *         If a security manager, <i>s</i>, is present and
 *         the caller's class loader is not the same as or an
 *         ancestor of the class loader for the current class and
 *         invocation of {@link SecurityManager#checkPackageAccess
 *         s.checkPackageAccess()} denies access to the package
 *         of this class.
 *
 * @jls 8.2 Class Members
 * @jls 8.4 Method Declarations
 * @since 1.1
 */
@CallerSensitive
public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException {
    Objects.requireNonNull(name);
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
    }
    Method method = getMethod0(name, parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException(methodToString(name, parameterTypes));
    }
    return getReflectionFactory().copyMethod(method);
}
Also used : Method(java.lang.reflect.Method) CallerSensitive(jdk.internal.reflect.CallerSensitive)

Aggregations

CallerSensitive (jdk.internal.reflect.CallerSensitive)20 BuiltinClassLoader (jdk.internal.loader.BuiltinClassLoader)6 ResolvedModule (java.lang.module.ResolvedModule)5 Method (java.lang.reflect.Method)3 ForceInline (jdk.internal.vm.annotation.ForceInline)3 IOException (java.io.IOException)2 ObjectStreamField (java.io.ObjectStreamField)2 AnnotatedType (java.lang.reflect.AnnotatedType)2 Field (java.lang.reflect.Field)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Type (java.lang.reflect.Type)2 URL (java.net.URL)2 ReflectionFactory (jdk.internal.reflect.ReflectionFactory)2 CoreReflectionFactory (sun.reflect.generics.factory.CoreReflectionFactory)2 Constructor (java.lang.reflect.Constructor)1 ResourceBundle (java.util.ResourceBundle)1 HotSpotIntrinsicCandidate (jdk.internal.HotSpotIntrinsicCandidate)1 ConstructorAccessor (jdk.internal.reflect.ConstructorAccessor)1 MethodAccessor (jdk.internal.reflect.MethodAccessor)1 ConstructorRepository (sun.reflect.generics.repository.ConstructorRepository)1