Java Code Examples for sun.reflect.generics.repository.ConstructorRepository#getParameterTypes()

The following examples show how to use sun.reflect.generics.repository.ConstructorRepository#getParameterTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Class.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 2
Source File: Class.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 3
Source File: Class.java    From jdk-1.7-annotated with Apache License 2.0 4 votes vote down vote up
/**
 * 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
 */
@CallerSensitive
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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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();
        // be very careful not to change the stack depth of this
        // checkMemberAccess call for security reasons
        // see java.lang.SecurityManager.checkMemberAccess
        //
        // Note that we need to do this on the enclosing class
        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");
    }
}
 
Example 4
Source File: Class.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 5
Source File: Class.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 6
Source File: Class.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 7
Source File: Class.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 8
Source File: Class.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 9
Source File: Class.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 10
Source File: Class.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 11
Source File: Class.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 12
Source File: Class.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 13
Source File: Class.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 14
Source File: Class.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 15
Source File: Class.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 16
Source File: Class.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 17
Source File: Class.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}
 
Example 18
Source File: Class.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];

        // Convert Types to Classes; returned types *should*
        // be class objects since the methodDescriptor's used
        // 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");
    }
}