Search in sources :

Example 1 with SortedProperties

use of com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor.SortedProperties 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)

Aggregations

Registration (com.oracle.truffle.api.instrumentation.TruffleInstrument.Registration)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 Name (javax.lang.model.element.Name)1 TypeElement (javax.lang.model.element.TypeElement)1 FileObject (javax.tools.FileObject)1