package com.oracle.svm.hosted.config;
import java.lang.reflect.Executable;
import java.util.List;
import org.graalvm.nativeimage.impl.ReflectionRegistry;
import com.oracle.svm.core.TypeResult;
import com.oracle.svm.core.configure.ReflectionConfigurationParserDelegate;
import com.oracle.svm.hosted.ImageClassLoader;
import jdk.vm.ci.meta.MetaUtil;
public class ReflectionRegistryAdapter implements ReflectionConfigurationParserDelegate<Class<?>> {
private final ReflectionRegistry registry;
private final ImageClassLoader classLoader;
public ReflectionRegistryAdapter(ReflectionRegistry registry, ImageClassLoader classLoader) {
this.registry = registry;
this.classLoader = classLoader;
}
@Override
public void registerType(Class<?> type) {
registry.register(type);
}
@Override
@SuppressWarnings("deprecation")
public Class<?> resolveType(String typeName) {
return resolveTypeResult(typeName).get();
}
@Override
public TypeResult<Class<?>> resolveTypeResult(String typeName) {
String name = typeName;
if (name.indexOf('[') != -1) {
name = MetaUtil.internalNameToJava(MetaUtil.toInternalName(name), true, true);
}
return classLoader.findClass(name);
}
@Override
public void registerPublicClasses(Class<?> type) {
registry.register(type.getClasses());
}
@Override
public void registerDeclaredClasses(Class<?> type) {
registry.register(type.getDeclaredClasses());
}
@Override
public void registerPublicFields(Class<?> type) {
registry.register(false, false, type.getFields());
}
@Override
public void registerDeclaredFields(Class<?> type) {
registry.register(false, false, type.getDeclaredFields());
}
@Override
public void registerPublicMethods(Class<?> type) {
registry.register(type.getMethods());
}
@Override
public void registerDeclaredMethods(Class<?> type) {
registry.register(type.getDeclaredMethods());
}
@Override
public void registerPublicConstructors(Class<?> type) {
registry.register(type.getConstructors());
}
@Override
public void registerDeclaredConstructors(Class<?> type) {
registry.register(type.getDeclaredConstructors());
}
@Override
public void registerField(Class<?> type, String fieldName, boolean allowWrite, boolean allowUnsafeAccess) throws NoSuchFieldException {
registry.register(allowWrite, allowUnsafeAccess, type.getDeclaredField(fieldName));
}
@Override
public boolean registerAllMethodsWithName(Class<?> type, String methodName) {
boolean found = false;
Executable[] methods = type.getDeclaredMethods();
for (Executable method : methods) {
if (method.getName().equals(methodName)) {
registry.register(method);
found = true;
}
}
return found;
}
@Override
public boolean registerAllConstructors(Class<?> clazz) {
Executable[] methods = clazz.getDeclaredConstructors();
for (Executable method : methods) {
registry.register(method);
}
return methods.length > 0;
}
@Override
public void registerMethod(Class<?> type, String methodName, List<Class<?>> methodParameterTypes) throws NoSuchMethodException {
Class<?>[] parameterTypesArray = methodParameterTypes.toArray(new Class<?>[0]);
registry.register((Executable) type.getDeclaredMethod(methodName, parameterTypesArray));
}
@Override
public void registerConstructor(Class<?> clazz, List<Class<?>> methodParameterTypes) throws NoSuchMethodException {
Class<?>[] parameterTypesArray = methodParameterTypes.toArray(new Class<?>[0]);
registry.register((Executable) clazz.getDeclaredConstructor(parameterTypesArray));
}
@Override
public String getTypeName(Class<?> type) {
return type.getTypeName();
}
@Override
public String getSimpleName(Class<?> type) {
return type.getSimpleName();
}
}