use of com.oracle.svm.core.annotate.TargetClass in project graal by oracle.
the class AnnotationSubstitutionProcessor method getTargetClass.
public Class<?> getTargetClass(Class<?> annotatedClass) {
Class<?> annotatedBaseClass = annotatedClass;
int arrayDepth = 0;
while (annotatedBaseClass.isArray()) {
arrayDepth++;
annotatedBaseClass = annotatedBaseClass.getComponentType();
}
TargetClass targetClassAnnotation = lookupAnnotation(annotatedBaseClass, TargetClass.class);
if (targetClassAnnotation == null) {
/* No annotation found, so return unchanged argument. */
return annotatedClass;
}
Class<?> targetClass = findTargetClass(annotatedBaseClass, targetClassAnnotation);
for (int i = 0; i < arrayDepth; i++) {
/*
* The only way to look up the array class is to instantiate a dummy array and take the
* class of the array.
*/
targetClass = Array.newInstance(targetClass, 0).getClass();
}
return targetClass;
}
use of com.oracle.svm.core.annotate.TargetClass in project graal by oracle.
the class AnnotationSubstitutionProcessor method interceptParameterType.
private Class<?> interceptParameterType(Class<?> type) {
TargetClass targetClassAnnotation = lookupAnnotation(type, TargetClass.class);
if (targetClassAnnotation != null) {
return findTargetClass(type, targetClassAnnotation);
}
if (type.isArray()) {
Class<?> componentType = type.getComponentType();
Class<?> componentRet = interceptParameterType(componentType);
if (!componentRet.equals(componentType)) {
Object tmp = Array.newInstance(componentRet, 0);
return tmp.getClass();
}
}
return type;
}
use of com.oracle.svm.core.annotate.TargetClass in project graal by oracle.
the class FieldDescriptor method loadFile.
private void loadFile(Reader reader) throws IOException {
Set<Class<?>> annotatedClasses = new HashSet<>(imageClassLoader.findAnnotatedClasses(TargetClass.class));
JSONParser parser = new JSONParser(reader);
@SuppressWarnings("unchecked") List<Object> allDescriptors = (List<Object>) parser.parse();
for (Object classDescriptorData : allDescriptors) {
if (classDescriptorData == null) {
/* Empty or trailing array elements are parsed to null. */
continue;
}
ClassDescriptor classDescriptor = new ClassDescriptor(classDescriptorData);
Class<?> annotatedClass = imageClassLoader.findClassByName(classDescriptor.annotatedClass());
if (annotatedClasses.contains(annotatedClass)) {
throw UserError.abort("target class already registered using explicit @TargetClass annotation: " + annotatedClass);
} else if (classDescriptors.containsKey(annotatedClass)) {
throw UserError.abort("target class already registered using substitution file: " + annotatedClass);
}
classDescriptors.put(annotatedClass, classDescriptor);
for (Object methodDescriptorData : classDescriptor.methods()) {
if (methodDescriptorData == null) {
/* Empty or trailing array elements are parsed to null. */
continue;
}
MethodDescriptor methodDescriptor = new MethodDescriptor(methodDescriptorData);
Executable annotatedMethod;
if (methodDescriptor.parameterTypes() != null) {
annotatedMethod = findMethod(annotatedClass, methodDescriptor.annotatedName(), methodDescriptor.parameterTypes());
} else {
annotatedMethod = findMethod(annotatedClass, methodDescriptor.annotatedName(), true);
}
methodDescriptors.put(annotatedMethod, methodDescriptor);
}
for (Object fieldDescriptorData : classDescriptor.fields()) {
FieldDescriptor fieldDescriptor = new FieldDescriptor(fieldDescriptorData);
Field annotatedField = findField(annotatedClass, fieldDescriptor.annotatedName());
fieldDescriptors.put(annotatedField, fieldDescriptor);
}
}
}
use of com.oracle.svm.core.annotate.TargetClass in project graal by oracle.
the class AnnotationSubstitutionProcessor method handleClass.
private void handleClass(Class<?> annotatedClass) {
guarantee(Modifier.isFinal(annotatedClass.getModifiers()) || annotatedClass.isInterface(), "Annotated class must be final: %s", annotatedClass);
guarantee(annotatedClass.getSuperclass() == Object.class || annotatedClass.isInterface(), "Annotated class must inherit directly from Object: %s", annotatedClass);
if (!NativeImageGenerator.includedIn(ImageSingletons.lookup(Platform.class), lookupAnnotation(annotatedClass, Platforms.class))) {
return;
}
TargetClass targetClassAnnotation = lookupAnnotation(annotatedClass, TargetClass.class);
Class<?> originalClass = findTargetClass(annotatedClass, targetClassAnnotation);
if (originalClass == null) {
return;
}
Delete deleteAnnotation = lookupAnnotation(annotatedClass, Delete.class);
Substitute substituteAnnotation = lookupAnnotation(annotatedClass, Substitute.class);
int numAnnotations = (deleteAnnotation != null ? 1 : 0) + (substituteAnnotation != null ? 1 : 0);
guarantee(numAnnotations <= 1, "Only one of @Delete or @Substitute can be used: %s", annotatedClass);
if (deleteAnnotation != null) {
handleDeletedClass(originalClass, deleteAnnotation);
} else if (substituteAnnotation != null) {
handleSubstitutionClass(annotatedClass, originalClass);
} else {
handleAliasClass(annotatedClass, originalClass);
}
}
Aggregations