Search in sources :

Example 1 with MethodRepository

use of sun.reflect.generics.repository.MethodRepository in project checker-framework by typetools.

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}.
 * @since 1.5
 */
public Method getEnclosingMethod() {
    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]);
        /*
             * 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.
             */
        for (Method m : enclosingInfo.getEnclosingClass().getDeclaredMethods()) {
            if (m.getName().equals(enclosingInfo.getName())) {
                Class<?>[] candidateParamClasses = m.getParameterTypes();
                if (candidateParamClasses.length == parameterClasses.length) {
                    boolean matches = true;
                    for (int i = 0; i < candidateParamClasses.length; i++) {
                        if (!candidateParamClasses[i].equals(parameterClasses[i])) {
                            matches = false;
                            break;
                        }
                    }
                    if (matches) {
                        // finally, check return type
                        if (m.getReturnType().equals(returnType))
                            return m;
                    }
                }
            }
        }
        throw new InternalError("Enclosing method not found");
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) MethodRepository(sun.reflect.generics.repository.MethodRepository) Method(java.lang.reflect.Method)

Example 2 with MethodRepository

use of sun.reflect.generics.repository.MethodRepository in project jdk8u_jdk by JetBrains.

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
        Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
        enclosingCandidate.checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
        /*
             * 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.
             */
        for (Method m : enclosingCandidate.getDeclaredMethods()) {
            if (m.getName().equals(enclosingInfo.getName())) {
                Class<?>[] candidateParamClasses = m.getParameterTypes();
                if (candidateParamClasses.length == parameterClasses.length) {
                    boolean matches = true;
                    for (int i = 0; i < candidateParamClasses.length; i++) {
                        if (!candidateParamClasses[i].equals(parameterClasses[i])) {
                            matches = false;
                            break;
                        }
                    }
                    if (matches) {
                        // finally, check return type
                        if (m.getReturnType().equals(returnType))
                            return m;
                    }
                }
            }
        }
        throw new InternalError("Enclosing method not found");
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) AnnotatedType(java.lang.reflect.AnnotatedType) Type(java.lang.reflect.Type) MethodRepository(sun.reflect.generics.repository.MethodRepository) Method(java.lang.reflect.Method) CallerSensitive(sun.reflect.CallerSensitive)

Example 3 with MethodRepository

use of sun.reflect.generics.repository.MethodRepository 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)

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)3 Method (java.lang.reflect.Method)3 Type (java.lang.reflect.Type)3 MethodRepository (sun.reflect.generics.repository.MethodRepository)3 AnnotatedType (java.lang.reflect.AnnotatedType)2 CallerSensitive (jdk.internal.reflect.CallerSensitive)1 ReflectionFactory (jdk.internal.reflect.ReflectionFactory)1 CallerSensitive (sun.reflect.CallerSensitive)1 CoreReflectionFactory (sun.reflect.generics.factory.CoreReflectionFactory)1