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));
}
}
}
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;
}
Aggregations