Search in sources :

Example 1 with ConstructorRepository

use of sun.reflect.generics.repository.ConstructorRepository in project Bytecoder by mirkosertic.

the class Class method getEnclosingConstructor.

/**
 * If this {@code Class} object represents a local or anonymous
 * class within a constructor, returns a {@link
 * java.lang.reflect.Constructor Constructor} object representing
 * the immediately enclosing constructor 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 constructor 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 constructors 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 Constructor<?> getEnclosingConstructor() throws SecurityException {
    EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
    if (enclosingInfo == null)
        return null;
    else {
        if (!enclosingInfo.isConstructor())
            return null;
        ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), getFactory());
        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);
        }
        Constructor<?>[] candidates = enclosingCandidate.privateGetDeclaredConstructors(false);
        /*
             * Loop over all declared constructors; match number
             * of and type of parameters.
             */
        ReflectionFactory fact = getReflectionFactory();
        for (Constructor<?> c : candidates) {
            if (arrayContentsEq(parameterClasses, fact.getExecutableSharedParameterTypes(c))) {
                return fact.copyConstructor(c);
            }
        }
        throw new InternalError("Enclosing constructor not found");
    }
}
Also used : ConstructorRepository(sun.reflect.generics.repository.ConstructorRepository) Constructor(java.lang.reflect.Constructor) GenericArrayType(java.lang.reflect.GenericArrayType) AnnotatedType(java.lang.reflect.AnnotatedType) Type(java.lang.reflect.Type) CoreReflectionFactory(sun.reflect.generics.factory.CoreReflectionFactory) ReflectionFactory(jdk.internal.reflect.ReflectionFactory) CallerSensitive(jdk.internal.reflect.CallerSensitive)

Example 2 with ConstructorRepository

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

the class Class method getEnclosingConstructor.

/**
     * If this {@code Class} object represents a local or anonymous
     * class within a constructor, returns a {@link
     * java.lang.reflect.Constructor Constructor} object representing
     * the immediately enclosing constructor 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 constructor 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 constructors 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 Constructor<?> getEnclosingConstructor() throws SecurityException {
    EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
    if (enclosingInfo == null)
        return null;
    else {
        if (!enclosingInfo.isConstructor())
            return null;
        ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), getFactory());
        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 constructors; match number
             * of and type of parameters.
             */
        for (Constructor<?> c : enclosingCandidate.getDeclaredConstructors()) {
            Class<?>[] candidateParamClasses = c.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)
                    return c;
            }
        }
        throw new InternalError("Enclosing constructor not found");
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) AnnotatedType(java.lang.reflect.AnnotatedType) Type(java.lang.reflect.Type) ConstructorRepository(sun.reflect.generics.repository.ConstructorRepository) CallerSensitive(sun.reflect.CallerSensitive)

Example 3 with ConstructorRepository

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

the class Class method getEnclosingConstructor.

/**
 * If this {@code Class} object represents a local or anonymous
 * class within a constructor, returns a {@link
 * java.lang.reflect.Constructor Constructor} object representing
 * the immediately enclosing constructor 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 constructor of the underlying class, if
 *     that class is a local or anonymous class; otherwise {@code null}.
 * @since 1.5
 */
public Constructor<?> getEnclosingConstructor() {
    EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
    if (enclosingInfo == null)
        return null;
    else {
        if (!enclosingInfo.isConstructor())
            return null;
        ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), getFactory());
        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 constructors; match number
             * of and type of parameters.
             */
        for (Constructor<?> c : enclosingInfo.getEnclosingClass().getDeclaredConstructors()) {
            Class<?>[] candidateParamClasses = c.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)
                    return c;
            }
        }
        throw new InternalError("Enclosing constructor not found");
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) ConstructorRepository(sun.reflect.generics.repository.ConstructorRepository)

Aggregations

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