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