Search in sources :

Example 1 with TargetClass

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;
}
Also used : TargetClass(com.oracle.svm.core.annotate.TargetClass)

Example 2 with 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;
}
Also used : TargetClass(com.oracle.svm.core.annotate.TargetClass)

Example 3 with TargetClass

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);
        }
    }
}
Also used : Field(java.lang.reflect.Field) TargetClass(com.oracle.svm.core.annotate.TargetClass) JSONParser(com.oracle.svm.hosted.json.JSONParser) List(java.util.List) TargetClass(com.oracle.svm.core.annotate.TargetClass) Executable(java.lang.reflect.Executable) HashSet(java.util.HashSet)

Example 4 with TargetClass

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);
    }
}
Also used : Delete(com.oracle.svm.core.annotate.Delete) TargetClass(com.oracle.svm.core.annotate.TargetClass) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

TargetClass (com.oracle.svm.core.annotate.TargetClass)4 Delete (com.oracle.svm.core.annotate.Delete)1 Substitute (com.oracle.svm.core.annotate.Substitute)1 JSONParser (com.oracle.svm.hosted.json.JSONParser)1 Executable (java.lang.reflect.Executable)1 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 List (java.util.List)1