Search in sources :

Example 1 with ReflectionFactory

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

the class Class method copyConstructors.

private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
    Constructor<U>[] out = arg.clone();
    ReflectionFactory fact = getReflectionFactory();
    for (int i = 0; i < out.length; i++) {
        out[i] = fact.copyConstructor(out[i]);
    }
    return out;
}
Also used : Constructor(java.lang.reflect.Constructor) CoreReflectionFactory(sun.reflect.generics.factory.CoreReflectionFactory) ReflectionFactory(jdk.internal.reflect.ReflectionFactory)

Example 2 with ReflectionFactory

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

the class Class method getEnclosingConstructor.

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

Example 3 with ReflectionFactory

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

the class Class method getDeclaredPublicMethods.

/**
 * Returns the list of {@code Method} objects for the declared public
 * methods of this class or interface that have the specified method name
 * and parameter types.
 *
 * @param name the name of the method
 * @param parameterTypes the parameter array
 * @return the list of {@code Method} objects for the public methods of
 *         this class matching the specified name and parameters
 */
List<Method> getDeclaredPublicMethods(String name, Class<?>... parameterTypes) {
    Method[] methods = privateGetDeclaredMethods(/* publicOnly */
    true);
    ReflectionFactory factory = getReflectionFactory();
    List<Method> result = new ArrayList<>();
    for (Method method : methods) {
        if (method.getName().equals(name) && Arrays.equals(factory.getExecutableSharedParameterTypes(method), parameterTypes)) {
            result.add(factory.copyMethod(method));
        }
    }
    return result;
}
Also used : CoreReflectionFactory(sun.reflect.generics.factory.CoreReflectionFactory) ReflectionFactory(jdk.internal.reflect.ReflectionFactory) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 4 with ReflectionFactory

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

the class Class method getConstructor0.

// Returns a "root" Constructor object. This Constructor object must NOT
// be propagated to the outside world, but must instead be copied
// via ReflectionFactory.copyConstructor.
private Constructor<T> getConstructor0(Class<?>[] parameterTypes, int which) throws NoSuchMethodException {
    ReflectionFactory fact = getReflectionFactory();
    Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
    for (Constructor<T> constructor : constructors) {
        if (arrayContentsEq(parameterTypes, fact.getExecutableSharedParameterTypes(constructor))) {
            return constructor;
        }
    }
    throw new NoSuchMethodException(methodToString("<init>", parameterTypes));
}
Also used : CoreReflectionFactory(sun.reflect.generics.factory.CoreReflectionFactory) ReflectionFactory(jdk.internal.reflect.ReflectionFactory) Constructor(java.lang.reflect.Constructor)

Example 5 with ReflectionFactory

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

the class Class method searchMethods.

// This method does not copy the returned Method object!
private static Method searchMethods(Method[] methods, String name, Class<?>[] parameterTypes) {
    ReflectionFactory fact = getReflectionFactory();
    Method res = null;
    for (Method m : methods) {
        if (m.getName().equals(name) && arrayContentsEq(parameterTypes, fact.getExecutableSharedParameterTypes(m)) && (res == null || (res.getReturnType() != m.getReturnType() && res.getReturnType().isAssignableFrom(m.getReturnType()))))
            res = m;
    }
    return res;
}
Also used : CoreReflectionFactory(sun.reflect.generics.factory.CoreReflectionFactory) ReflectionFactory(jdk.internal.reflect.ReflectionFactory) Method(java.lang.reflect.Method)

Aggregations

ReflectionFactory (jdk.internal.reflect.ReflectionFactory)8 CoreReflectionFactory (sun.reflect.generics.factory.CoreReflectionFactory)8 Method (java.lang.reflect.Method)4 Constructor (java.lang.reflect.Constructor)3 AnnotatedType (java.lang.reflect.AnnotatedType)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Type (java.lang.reflect.Type)2 CallerSensitive (jdk.internal.reflect.CallerSensitive)2 ObjectStreamField (java.io.ObjectStreamField)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 ConstructorRepository (sun.reflect.generics.repository.ConstructorRepository)1 MethodRepository (sun.reflect.generics.repository.MethodRepository)1