Java Code Examples for sun.reflect.generics.repository.MethodRepository

The following examples show how to use sun.reflect.generics.repository.MethodRepository. These examples are extracted from open source projects. 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 Project: jdk1.8-source-analysis   Source File: Method.java    License: Apache License 2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 2
Source Project: dragonwell8_jdk   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 3
Source Project: TencentKona-8   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 4
Source Project: jdk8u60   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 5
Source Project: JDKSourceCode1.8   Source File: Method.java    License: MIT License 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 6
Source Project: openjdk-jdk8u   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 7
Source Project: openjdk-jdk8u-backup   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 8
Source Project: Bytecoder   Source File: Method.java    License: Apache License 2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 9
Source Project: openjdk-jdk9   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 10
Source Project: jdk8u-jdk   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 11
Source Project: Java8CN   Source File: Method.java    License: Apache License 2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 12
Source Project: hottub   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 13
Source Project: openjdk-8-source   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 14
Source Project: openjdk-8   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 15
Source Project: jdk8u_jdk   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 16
Source Project: jdk8u-jdk   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 17
Source Project: jdk-1.7-annotated   Source File: Method.java    License: Apache License 2.0 5 votes vote down vote up
private MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 18
Source Project: jdk8u-dev-jdk   Source File: Method.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
MethodRepository getGenericInfo() {
    // lazily initialize repository if necessary
    if (genericInfo == null) {
        // create and cache generic info repository
        genericInfo = MethodRepository.make(getGenericSignature(),
                                            getFactory());
    }
    return genericInfo; //return cached repository
}
 
Example 19
Source Project: totallylazy   Source File: Methods.java    License: Apache License 2.0 5 votes vote down vote up
public static Method method(String internalName, String name, String descriptior) throws ClassNotFoundException {
    Class<?> aClass = Class.forName(internalName.replace('/', '.'));
    MethodRepository repository = MethodRepository.make(descriptior, Signature.genericsFactory(aClass));
    Sequence<Type> parameterTypes = sequence(repository.getParameterTypes());
    Type returnType = repository.getReturnType();
        return sequence(aClass.getMethods()).
                filter(m -> m.getName().equals(name)).
                filter(m -> m.getReturnType().equals(returnType)).
                filter(m -> sequence(m.getParameterTypes()).equals(parameterTypes)).
                head();
}
 
Example 20
Source Project: jdk1.8-source-analysis   Source File: Class.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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];

        // 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 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");
    }
}
 
Example 21
Source Project: dragonwell8_jdk   Source File: Class.java    License: 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 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];

        // 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 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");
    }
}
 
Example 22
Source Project: TencentKona-8   Source File: Class.java    License: 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 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];

        // 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 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");
    }
}
 
Example 23
Source Project: jdk8u60   Source File: Class.java    License: 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 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];

        // 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 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");
    }
}
 
Example 24
Source Project: JDKSourceCode1.8   Source File: Class.java    License: MIT License 4 votes vote down vote up
/**
 * 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];

        // 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 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");
    }
}
 
Example 25
Source Project: openjdk-jdk8u   Source File: Class.java    License: 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 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];

        // 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 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");
    }
}
 
Example 26
Source Project: openjdk-jdk8u-backup   Source File: Class.java    License: 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 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];

        // 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 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");
    }
}
 
Example 27
Source Project: Bytecoder   Source File: Class.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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];

        // 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);
        }
        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");
    }
}
 
Example 28
Source Project: openjdk-jdk9   Source File: Class.java    License: 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 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];

        // 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);
        }
        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");
    }
}
 
Example 29
Source Project: jdk8u-jdk   Source File: Class.java    License: 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 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];

        // 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 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");
    }
}
 
Example 30
Source Project: Java8CN   Source File: Class.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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];

        // 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 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");
    }
}