Search in sources :

Example 1 with InstrumentableNode

use of com.oracle.truffle.api.instrumentation.InstrumentableNode 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)

Example 2 with InstrumentableNode

use of com.oracle.truffle.api.instrumentation.InstrumentableNode in project graal by oracle.

the class SuspendableLocationFinder method findNearestBound.

private static SourceSection findNearestBound(Source source, Set<Class<? extends Tag>> elementTags, int line, int column, SuspendAnchor anchor, TruffleInstrument.Env env) {
    int offset = source.getLineStartOffset(line);
    if (column > 0) {
        offset += column - 1;
    }
    NearestSections sectionsCollector = new NearestSections(elementTags, (column <= 0) ? line : 0, offset, anchor);
    // All SourceSections of the Source are loaded already when the source was executed
    env.getInstrumenter().attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().sourceIs(source).build(), sectionsCollector, true).dispose();
    SourceSection section = sectionsCollector.getExactSection();
    if (section != null) {
        return section;
    }
    InstrumentableNode contextNode = sectionsCollector.getContainsNode();
    if (contextNode == null) {
        contextNode = sectionsCollector.getNextNode();
    }
    if (contextNode == null) {
        contextNode = sectionsCollector.getPreviousNode();
    }
    if (contextNode == null) {
        return null;
    }
    Node node = contextNode.findNearestNodeAt(offset, elementTags);
    if (node == null) {
        return null;
    }
    return node.getSourceSection();
}
Also used : InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) SourceSection(com.oracle.truffle.api.source.SourceSection)

Aggregations

InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 GenerateWrapper (com.oracle.truffle.api.instrumentation.GenerateWrapper)1 Node (com.oracle.truffle.api.nodes.Node)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 CodeAnnotationMirror (com.oracle.truffle.dsl.processor.java.model.CodeAnnotationMirror)1 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)1 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)1 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)1 FixWarningsVisitor (com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor)1 GenerateOverrideVisitor (com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1