Search in sources :

Example 71 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project st-js by st-js.

the class ClassWriter method isBridge.

private boolean isBridge(ClassTree tree) {
    TypeElement type = TreeUtils.elementFromDeclaration(tree);
    List<? extends AnnotationMirror> annotations = type.getAnnotationMirrors();
    for (AnnotationMirror a : annotations) {
        if (a.getAnnotationType().toString().equals(STJSBridge.class.getName())) {
            return true;
        }
    }
    return false;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) STJSBridge(org.stjs.javascript.annotation.STJSBridge) TypeElement(javax.lang.model.element.TypeElement)

Example 72 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project graal by oracle.

the class OptionProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        return true;
    }
    ProcessorContext context = new ProcessorContext(processingEnv, null);
    ProcessorContext.setThreadLocalInstance(context);
    try {
        Map<Element, OptionsInfo> map = new HashMap<>();
        for (Element element : roundEnv.getElementsAnnotatedWith(Option.class)) {
            if (!processed.contains(element)) {
                processed.add(element);
                Element topElement = element.getEnclosingElement();
                OptionsInfo options = map.get(topElement);
                if (options == null) {
                    options = new OptionsInfo(topElement);
                    map.put(topElement, options);
                }
                AnnotationMirror mirror = ElementUtils.findAnnotationMirror(processingEnv, element.getAnnotationMirrors(), Option.class);
                try {
                    processElement(element, mirror, options);
                } catch (Throwable t) {
                    handleThrowable(t, topElement);
                }
            }
        }
        Map<String, OptionInfo> seenKeys = new HashMap<>();
        for (OptionsInfo infos : map.values()) {
            for (OptionInfo info : infos.options) {
                if (seenKeys.containsKey(info.name)) {
                    OptionInfo otherInfo = seenKeys.get(info.name);
                    String message = "Two options with duplicated resolved descriptor name '%s' found.";
                    info.valid = false;
                    otherInfo.valid = false;
                    error(info.field, info.annotation, message, info.name);
                    error(otherInfo.field, otherInfo.annotation, message, otherInfo.name);
                } else {
                    seenKeys.put(info.name, info);
                }
            }
        }
        for (OptionsInfo infos : map.values()) {
            ListIterator<OptionInfo> listIterator = infos.options.listIterator();
            while (listIterator.hasNext()) {
                OptionInfo info = listIterator.next();
                if (info.valid) {
                    ExpectError.assertNoErrorExpected(processingEnv, info.field);
                } else {
                    listIterator.remove();
                }
            }
            Collections.sort(infos.options, new Comparator<OptionInfo>() {

                public int compare(OptionInfo o1, OptionInfo o2) {
                    return o1.name.compareTo(o2.name);
                }
            });
        }
        for (OptionsInfo info : map.values()) {
            try {
                generateOptionDescriptor(info);
            } catch (Throwable t) {
                handleThrowable(t, info.type);
            }
        }
    } finally {
        ProcessorContext.setThreadLocalInstance(null);
    }
    return true;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashMap(java.util.HashMap) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Example 73 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project graal by oracle.

the class ElementUtils method collectAnnotations.

public static List<AnnotationMirror> collectAnnotations(ProcessorContext context, AnnotationMirror markerAnnotation, String elementName, Element element, Class<? extends Annotation> annotationClass) {
    List<AnnotationMirror> result = new ArrayList<>();
    if (markerAnnotation != null) {
        result.addAll(ElementUtils.getAnnotationValueList(AnnotationMirror.class, markerAnnotation, elementName));
    }
    AnnotationMirror explicit = ElementUtils.findAnnotationMirror(context.getEnvironment(), element, annotationClass);
    if (explicit != null) {
        result.add(explicit);
    }
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayList(java.util.ArrayList)

Example 74 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project graal by oracle.

the class InstrumentableProcessor method processLegacyInstrumentable.

/*
     * TO BE REMOVED WITH DEPRECATIONS
     */
@SuppressWarnings("deprecation")
private void processLegacyInstrumentable(RoundEnvironment roundEnv, ProcessorContext context) {
    for (Element element : roundEnv.getElementsAnnotatedWith(com.oracle.truffle.api.instrumentation.Instrumentable.class)) {
        if (!element.getKind().isClass() && !element.getKind().isInterface()) {
            continue;
        }
        try {
            if (element.getKind() != ElementKind.CLASS) {
                emitError(element, String.format("Only classes can be annotated with %s.", com.oracle.truffle.api.instrumentation.Instrumentable.class.getSimpleName()));
                continue;
            }
            AnnotationMirror generateWrapperMirror = ElementUtils.findAnnotationMirror(element.getAnnotationMirrors(), context.getType(GenerateWrapper.class));
            if (generateWrapperMirror != null) {
                continue;
            }
            TypeMirror instrumentableType = context.getType(com.oracle.truffle.api.instrumentation.Instrumentable.class);
            AnnotationMirror instrumentable = ElementUtils.findAnnotationMirror(element.getAnnotationMirrors(), instrumentableType);
            if (instrumentable == null) {
                continue;
            } else {
                final boolean generateWrapper;
                TypeMirror factoryType = ElementUtils.getAnnotationValue(TypeMirror.class, instrumentable, "factory");
                if (factoryType == null || factoryType.getKind() == TypeKind.ERROR) {
                    // factory type is erroneous or null (can mean error in javac)
                    // generate it
                    generateWrapper = true;
                } else {
                    TypeElement type = context.getEnvironment().getElementUtils().getTypeElement("com.oracle.truffle.api.instrumentation.test.TestErrorFactory");
                    if (type != null && ElementUtils.typeEquals(factoryType, type.asType())) {
                        generateWrapper = true;
                    } else {
                        // factory is user defined or already generated
                        generateWrapper = false;
                    }
                }
                if (!generateWrapper) {
                    continue;
                }
            }
            CodeTypeElement unit = generateWrapperAndFactory(context, element);
            if (unit == null) {
                continue;
            }
            DeclaredType overrideType = (DeclaredType) context.getType(Override.class);
            DeclaredType unusedType = (DeclaredType) context.getType(SuppressWarnings.class);
            unit.accept(new GenerateOverrideVisitor(overrideType), null);
            unit.accept(new FixWarningsVisitor(context.getEnvironment(), unusedType, overrideType), null);
            unit.accept(new CodeWriter(context.getEnvironment(), element), null);
        } catch (Throwable e) {
            // never throw annotation processor exceptions to the compiler
            // it might screw up its state.
            handleThrowable(e, element);
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) GenerateWrapper(com.oracle.truffle.api.instrumentation.GenerateWrapper) FixWarningsVisitor(com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor) CodeAnnotationMirror(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) GenerateOverrideVisitor(com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor) DeclaredType(javax.lang.model.type.DeclaredType)

Example 75 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project graal by oracle.

the class InstrumentableProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        return false;
    }
    try {
        ProcessorContext context = new ProcessorContext(processingEnv, null);
        ProcessorContext.setThreadLocalInstance(context);
        DeclaredType instrumentableNode = context.getDeclaredType(InstrumentableNode.class);
        ExecutableElement createWrapper = ElementUtils.findExecutableElement(instrumentableNode, CREATE_WRAPPER_NAME);
        for (Element element : roundEnv.getElementsAnnotatedWith(GenerateWrapper.class)) {
            if (!element.getKind().isClass() && !element.getKind().isInterface()) {
                continue;
            }
            try {
                if (element.getKind() != ElementKind.CLASS) {
                    emitError(element, String.format("Only classes can be annotated with %s.", GenerateWrapper.class.getSimpleName()));
                    continue;
                }
                if (createWrapper == null) {
                    emitError(element, String.format("Fatal %s.%s not found.", InstrumentableNode.class.getSimpleName(), CREATE_WRAPPER_NAME));
                    continue;
                }
                if (!ElementUtils.isAssignable(element.asType(), instrumentableNode)) {
                    emitError(element, String.format("Classes annotated with @%s must implement %s.", GenerateWrapper.class.getSimpleName(), InstrumentableNode.class.getSimpleName()));
                    continue;
                } else {
                    boolean createWrapperFound = false;
                    for (ExecutableElement declaredMethod : ElementFilter.methodsIn(element.getEnclosedElements())) {
                        if (ElementUtils.signatureEquals(declaredMethod, createWrapper)) {
                            createWrapperFound = true;
                            break;
                        }
                    }
                    if (!createWrapperFound) {
                        emitError(element, String.format("Classes annotated with @%s must declare/override %s.%s and return a new instance of the generated wrapper class called %s." + " You may copy the following generated implementation: %n" + "  @Override public %s createWrapper(%s probeNode) {%n" + "    return new %s(this, probeNode);%n" + "  }", GenerateWrapper.class.getSimpleName(), InstrumentableNode.class.getSimpleName(), CREATE_WRAPPER_NAME, createWrapperClassName((TypeElement) element), com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode.class.getSimpleName(), ProbeNode.class.getSimpleName(), createWrapperClassName((TypeElement) element)));
                        continue;
                    }
                    if (!ElementUtils.isAssignable(element.asType(), context.getType(Node.class))) {
                        emitError(element, String.format("Classes annotated with @%s must extend %s.", GenerateWrapper.class.getSimpleName(), Node.class.getSimpleName()));
                        continue;
                    }
                }
                AnnotationMirror generateWrapperMirror = ElementUtils.findAnnotationMirror(element.getAnnotationMirrors(), context.getType(GenerateWrapper.class));
                if (generateWrapperMirror == null) {
                    continue;
                }
                CodeTypeElement unit = generateWrapperOnly(context, element);
                if (unit == null) {
                    continue;
                }
                DeclaredType overrideType = (DeclaredType) context.getType(Override.class);
                DeclaredType unusedType = (DeclaredType) context.getType(SuppressWarnings.class);
                unit.accept(new GenerateOverrideVisitor(overrideType), null);
                unit.accept(new FixWarningsVisitor(context.getEnvironment(), unusedType, overrideType), null);
                unit.accept(new CodeWriter(context.getEnvironment(), element), null);
            } catch (Throwable e) {
                // never throw annotation processor exceptions to the compiler
                // it might screw up its state.
                handleThrowable(e, element);
            }
        }
        // remove with deprecations
        processLegacyInstrumentable(roundEnv, context);
        return true;
    } finally {
        ProcessorContext.setThreadLocalInstance(null);
    }
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) GenerateWrapper(com.oracle.truffle.api.instrumentation.GenerateWrapper) FixWarningsVisitor(com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) CodeAnnotationMirror(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) GenerateOverrideVisitor(com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)665 TypeElement (javax.lang.model.element.TypeElement)159 ExecutableElement (javax.lang.model.element.ExecutableElement)112 TypeMirror (javax.lang.model.type.TypeMirror)99 ArrayList (java.util.ArrayList)92 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)90 Element (javax.lang.model.element.Element)87 VariableElement (javax.lang.model.element.VariableElement)79 AnnotationValue (javax.lang.model.element.AnnotationValue)73 DeclaredType (javax.lang.model.type.DeclaredType)58 List (java.util.List)52 CFValue (org.checkerframework.framework.flow.CFValue)52 Map (java.util.Map)46 HashSet (java.util.HashSet)40 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)39 CFStore (org.checkerframework.framework.flow.CFStore)39 HashMap (java.util.HashMap)37 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)36 ExpressionTree (com.sun.source.tree.ExpressionTree)30 MethodTree (com.sun.source.tree.MethodTree)29