Search in sources :

Example 1 with TargetElement

use of com.oracle.svm.core.annotate.TargetElement in project graal by oracle.

the class AnnotationSubstitutionProcessor method findOriginalMethod.

private ResolvedJavaMethod findOriginalMethod(Executable annotatedMethod, Class<?> originalClass) {
    TargetElement targetElementAnnotation = lookupAnnotation(annotatedMethod, TargetElement.class);
    String originalName = "";
    boolean optional = false;
    if (targetElementAnnotation != null) {
        originalName = targetElementAnnotation.name();
        optional = targetElementAnnotation.optional();
    }
    if (originalName.length() == 0) {
        originalName = annotatedMethod.getName();
    }
    try {
        if (annotatedMethod instanceof Method && !originalName.equals(TargetElement.CONSTRUCTOR_NAME)) {
            Class<?>[] originalParams = interceptParameterTypes(annotatedMethod.getParameterTypes());
            Method originalMethod = originalClass.getDeclaredMethod(originalName, originalParams);
            guarantee(Modifier.isStatic(annotatedMethod.getModifiers()) == Modifier.isStatic(originalMethod.getModifiers()), "Static modifier mismatch: %s, %s", annotatedMethod, originalMethod);
            return metaAccess.lookupJavaMethod(originalMethod);
        } else {
            guarantee(!Modifier.isStatic(annotatedMethod.getModifiers()), "Constructor Alias method %s must not be static", annotatedMethod);
            Class<?>[] originalParams = interceptParameterTypes(annotatedMethod.getParameterTypes());
            Constructor<?> originalMethod = originalClass.getDeclaredConstructor(originalParams);
            return metaAccess.lookupJavaMethod(originalMethod);
        }
    } catch (NoSuchMethodException ex) {
        guarantee(optional, "could not find non-optional target method: %s", annotatedMethod);
        return null;
    }
}
Also used : TargetClass(com.oracle.svm.core.annotate.TargetClass) Method(java.lang.reflect.Method) CustomSubstitutionMethod(com.oracle.svm.hosted.annotation.CustomSubstitutionMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) TargetElement(com.oracle.svm.core.annotate.TargetElement)

Example 2 with TargetElement

use of com.oracle.svm.core.annotate.TargetElement in project graal by oracle.

the class AnnotationSubstitutionProcessor method findOriginalField.

private ResolvedJavaField findOriginalField(Field annotatedField, Class<?> originalClass, boolean forceOptional) {
    TargetElement targetElementAnnotation = lookupAnnotation(annotatedField, TargetElement.class);
    String originalName = "";
    boolean optional = false;
    if (targetElementAnnotation != null) {
        originalName = targetElementAnnotation.name();
        optional = targetElementAnnotation.optional();
    }
    if (originalName.length() == 0) {
        originalName = annotatedField.getName();
    }
    try {
        Field originalField = originalClass.getDeclaredField(originalName);
        guarantee(getTargetClass(annotatedField.getType()).equals(originalField.getType()), "Type mismatch:%n    %s %s%n    %s %s", annotatedField.getType(), annotatedField, originalField.getType(), originalField);
        return metaAccess.lookupJavaField(originalField);
    } catch (NoSuchFieldException ex) {
        /*
             * Some fields are hidden from reflection. Try to find the field via the
             * ResolvedJavaType.
             */
        for (ResolvedJavaField f : metaAccess.lookupJavaType(originalClass).getInstanceFields(true)) {
            if (f.getName().equals(originalName)) {
                return f;
            }
        }
        guarantee(optional || forceOptional, "could not find non-optional target field: %s", annotatedField);
        return null;
    }
}
Also used : ResolvedJavaField(jdk.vm.ci.meta.ResolvedJavaField) Field(java.lang.reflect.Field) AnalysisField(com.oracle.graal.pointsto.meta.AnalysisField) TargetElement(com.oracle.svm.core.annotate.TargetElement) ResolvedJavaField(jdk.vm.ci.meta.ResolvedJavaField)

Aggregations

TargetElement (com.oracle.svm.core.annotate.TargetElement)2 AnalysisField (com.oracle.graal.pointsto.meta.AnalysisField)1 TargetClass (com.oracle.svm.core.annotate.TargetClass)1 CustomSubstitutionMethod (com.oracle.svm.hosted.annotation.CustomSubstitutionMethod)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ResolvedJavaField (jdk.vm.ci.meta.ResolvedJavaField)1 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)1