Search in sources :

Example 1 with InjectAccessors

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

the class InjectedAccessorsPlugin method handleField.

private static boolean handleField(GraphBuilderContext b, ResolvedJavaField field, boolean isStatic, ValueNode receiver, boolean isGet, ValueNode value) {
    InjectAccessors injectAccesors = field.getAnnotation(InjectAccessors.class);
    if (injectAccesors == null) {
        return false;
    }
    Class<?> accessorsClass = injectAccesors.value();
    ResolvedJavaType accessorsType = b.getMetaAccess().lookupJavaType(accessorsClass);
    String shortName = isGet ? "get" : "set";
    String fieldName = field.getName();
    String longName = shortName + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
    ResolvedJavaMethod foundMethod = null;
    for (ResolvedJavaMethod method : accessorsType.getDeclaredMethods()) {
        if (method.getName().equals(shortName) || method.getName().equals(longName)) {
            if (foundMethod != null) {
                error(field, accessorsType, null, "found two methods " + foundMethod.format("%n(%p)") + " and " + method.format("%n(%p)"));
            }
            foundMethod = method;
        }
    }
    if (foundMethod == null) {
        error(field, accessorsType, null, "found no method named " + shortName + " or " + longName);
    }
    if (!foundMethod.isStatic()) {
        error(field, accessorsType, foundMethod, "method is not static");
    }
    int paramIdx = 0;
    if (!isStatic) {
        if (foundMethod.getSignature().getParameterCount(false) < paramIdx + 1) {
            error(field, accessorsType, foundMethod, "not enough parameters");
        }
        JavaType actualReceiver = foundMethod.getSignature().getParameterType(paramIdx, null);
        ResolvedJavaType expectedReceiver = field.getDeclaringClass();
        if (!actualReceiver.equals(expectedReceiver)) {
            error(field, accessorsType, foundMethod, "wrong receiver type: expected " + expectedReceiver.toJavaName(true) + ", found " + actualReceiver.toJavaName(true));
        }
        paramIdx++;
    }
    JavaType expectedValue = field.getType();
    if (isGet) {
        JavaType actualValue = foundMethod.getSignature().getReturnType(null);
        if (!actualValue.equals(expectedValue)) {
            error(field, accessorsType, foundMethod, "wrong return type: expected " + expectedValue.toJavaName(true) + ", found " + actualValue.toJavaName(true));
        }
    } else {
        if (foundMethod.getSignature().getParameterCount(false) < paramIdx + 1) {
            error(field, accessorsType, foundMethod, "not enough parameters");
        }
        JavaType actualValue = foundMethod.getSignature().getParameterType(paramIdx, null);
        if (!actualValue.equals(expectedValue)) {
            error(field, accessorsType, foundMethod, "wrong value type: expected " + expectedValue.toJavaName(true) + ", found " + actualValue.toJavaName(true));
        }
        paramIdx++;
    }
    if (foundMethod.getSignature().getParameterCount(false) != paramIdx) {
        error(field, accessorsType, foundMethod, "Wrong number of parameters: expected " + paramIdx + ", found " + foundMethod.getSignature().getParameterCount(false));
    }
    List<ValueNode> args = new ArrayList<>();
    if (!isStatic) {
        args.add(receiver);
    }
    if (!isGet) {
        args.add(value);
    }
    b.handleReplacedInvoke(InvokeKind.Static, foundMethod, args.toArray(new ValueNode[args.size()]), false);
    return true;
}
Also used : JavaType(jdk.vm.ci.meta.JavaType) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) InjectAccessors(com.oracle.svm.core.annotate.InjectAccessors) ValueNode(org.graalvm.compiler.nodes.ValueNode) ArrayList(java.util.ArrayList) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 2 with InjectAccessors

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

the class AnnotationSubstitutionProcessor method fieldValueRecomputation.

private ResolvedJavaField fieldValueRecomputation(Class<?> originalClass, ResolvedJavaField original, ResolvedJavaField annotated, Field annotatedField) {
    RecomputeFieldValue recomputeAnnotation = lookupAnnotation(annotatedField, RecomputeFieldValue.class);
    InjectAccessors injectAccessorsAnnotation = lookupAnnotation(annotatedField, InjectAccessors.class);
    int numAnnotations = (recomputeAnnotation != null ? 1 : 0) + (injectAccessorsAnnotation != null ? 1 : 0);
    guarantee(numAnnotations <= 1, "Only one of @RecomputeFieldValue or @InjectAccessors can be used: %s", annotatedField);
    if (injectAccessorsAnnotation != null) {
        return new AnnotatedField(original, injectAccessorsAnnotation);
    }
    if (recomputeAnnotation == null && !original.isFinal()) {
        return original;
    }
    RecomputeFieldValue.Kind kind = RecomputeFieldValue.Kind.None;
    Class<?> targetClass = originalClass;
    String targetName = "";
    boolean isFinal = false;
    if (recomputeAnnotation != null) {
        kind = recomputeAnnotation.kind();
        targetName = recomputeAnnotation.name();
        isFinal = recomputeAnnotation.isFinal();
        guarantee(!isFinal || ComputedValueField.isFinalValid(kind), "@%s with %s can never be final during analysis: unset isFinal in the annotation on %s", RecomputeFieldValue.class.getSimpleName(), kind, annotated);
        if (recomputeAnnotation.declClass() != RecomputeFieldValue.class) {
            guarantee(recomputeAnnotation.declClassName().isEmpty(), "Both class and class name specified");
            targetClass = recomputeAnnotation.declClass();
        } else if (!recomputeAnnotation.declClassName().isEmpty()) {
            targetClass = imageClassLoader.findClassByName(recomputeAnnotation.declClassName());
        }
    }
    return new ComputedValueField(original, annotated, kind, targetClass, targetName, isFinal);
}
Also used : InjectAccessors(com.oracle.svm.core.annotate.InjectAccessors) RecomputeFieldValue(com.oracle.svm.core.annotate.RecomputeFieldValue)

Aggregations

InjectAccessors (com.oracle.svm.core.annotate.InjectAccessors)2 RecomputeFieldValue (com.oracle.svm.core.annotate.RecomputeFieldValue)1 ArrayList (java.util.ArrayList)1 JavaType (jdk.vm.ci.meta.JavaType)1 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)1 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)1 ValueNode (org.graalvm.compiler.nodes.ValueNode)1