Search in sources :

Example 1 with Registration

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

the class InstrumentRegistrationProcessor method generateFile.

private void generateFile(List<TypeElement> instruments) {
    String filename = "META-INF/truffle/instrument";
    Properties p = new SortedProperties();
    int numInstruments = loadIfFileAlreadyExists(filename, p);
    for (TypeElement l : instruments) {
        Registration annotation = l.getAnnotation(Registration.class);
        if (annotation == null) {
            continue;
        }
        int instNum = findInstrument(annotation.id(), p);
        if (instNum == 0) {
            // not found
            numInstruments += 1;
            instNum = numInstruments;
        }
        String prefix = "instrument" + instNum + ".";
        String className = processingEnv.getElementUtils().getBinaryName(l).toString();
        p.setProperty(prefix + "id", annotation.id());
        p.setProperty(prefix + "name", annotation.name());
        p.setProperty(prefix + "version", annotation.version());
        p.setProperty(prefix + "className", className);
        p.setProperty(prefix + "internal", Boolean.toString(annotation.internal()));
        int serviceCounter = 0;
        for (AnnotationMirror anno : l.getAnnotationMirrors()) {
            final String annoName = anno.getAnnotationType().asElement().toString();
            if (Registration.class.getCanonicalName().equals(annoName)) {
                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : anno.getElementValues().entrySet()) {
                    final Name attrName = entry.getKey().getSimpleName();
                    if (attrName.contentEquals("services")) {
                        AnnotationValue attrValue = entry.getValue();
                        List<?> classes = (List<?>) attrValue.getValue();
                        for (Object clazz : classes) {
                            AnnotationValue clazzValue = (AnnotationValue) clazz;
                            p.setProperty(prefix + "service" + serviceCounter++, clazzValue.getValue().toString());
                        }
                    }
                }
            }
        }
    }
    if (numInstruments > 0) {
        try {
            FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename);
            try (OutputStream os = file.openOutputStream()) {
                p.store(os, "Generated by " + InstrumentRegistrationProcessor.class.getName());
            }
        } catch (IOException e) {
            processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), instruments.get(0));
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor.SortedProperties) SortedProperties(com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor.SortedProperties) Name(javax.lang.model.element.Name) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Registration(com.oracle.truffle.api.instrumentation.TruffleInstrument.Registration) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) FileObject(javax.tools.FileObject) FileObject(javax.tools.FileObject) Map(java.util.Map)

Example 2 with Registration

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

the class InstrumentRegistrationProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        generateFile(registrations);
        registrations.clear();
        return true;
    }
    for (Element e : roundEnv.getElementsAnnotatedWith(Registration.class)) {
        Registration annotation = e.getAnnotation(Registration.class);
        if (annotation != null && e.getKind() == ElementKind.CLASS) {
            if (!e.getModifiers().contains(Modifier.PUBLIC)) {
                emitError("Registered instrument class must be public", e);
                continue;
            }
            if (e.getEnclosingElement().getKind() != ElementKind.PACKAGE && !e.getModifiers().contains(Modifier.STATIC)) {
                emitError("Registered instrument inner-class must be static", e);
                continue;
            }
            TypeMirror truffleLang = processingEnv.getTypeUtils().erasure(processingEnv.getElementUtils().getTypeElement(TruffleInstrument.class.getName()).asType());
            if (!processingEnv.getTypeUtils().isAssignable(e.asType(), truffleLang)) {
                emitError("Registered instrument class must subclass TruffleInstrument", e);
                continue;
            }
            assertNoErrorExpected(e);
            registrations.add((TypeElement) e);
        }
    }
    return true;
}
Also used : TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) TypeMirror(javax.lang.model.type.TypeMirror) Registration(com.oracle.truffle.api.instrumentation.TruffleInstrument.Registration) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Aggregations

Registration (com.oracle.truffle.api.instrumentation.TruffleInstrument.Registration)2 TypeElement (javax.lang.model.element.TypeElement)2 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 SortedProperties (com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor.SortedProperties)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 Name (javax.lang.model.element.Name)1 TypeMirror (javax.lang.model.type.TypeMirror)1 FileObject (javax.tools.FileObject)1