use of javax.annotation.processing.RoundEnvironment in project auto by google.
the class AutoValueishProcessor method process.
@Override
public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (annotationType == null) {
// This should not happen. If the annotation type is not found, how did the processor get
// triggered?
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Did not process @" + annotationClassName + " because the annotation class was not found");
return false;
}
List<TypeElement> deferredTypes = deferredTypeNames.stream().map(name -> elementUtils().getTypeElement(name)).collect(toList());
if (roundEnv.processingOver()) {
// was in deferredTypes.
for (TypeElement type : deferredTypes) {
errorReporter.reportError(type, "[%sUndefined] Did not generate @%s class for %s because it references" + " undefined types", simpleAnnotationName, simpleAnnotationName, type.getQualifiedName());
}
return false;
}
Collection<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotationType);
List<TypeElement> types = new ImmutableList.Builder<TypeElement>().addAll(deferredTypes).addAll(ElementFilter.typesIn(annotatedElements)).build();
deferredTypeNames.clear();
for (TypeElement type : types) {
try {
validateType(type);
processType(type);
} catch (AbortProcessingException e) {
// We abandoned this type; continue with the next.
} catch (MissingTypeException e) {
// We abandoned this type, but only because we needed another type that it references and
// that other type was missing. It is possible that the missing type will be generated by
// further annotation processing, so we will try again on the next round (perhaps failing
// again and adding it back to the list).
addDeferredType(type);
} catch (RuntimeException e) {
String trace = Throwables.getStackTraceAsString(e);
errorReporter.reportError(type, "[%sException] @%s processor threw an exception: %s", simpleAnnotationName, simpleAnnotationName, trace);
throw e;
}
}
// never claim annotation, because who knows what other processors want?
return false;
}
use of javax.annotation.processing.RoundEnvironment in project vertx-docgen by vert-x3.
the class BaseProcessorTest method testGen.
@Test
public void testGen() throws Exception {
AtomicInteger count = new AtomicInteger();
AbstractProcessor proc = new AbstractProcessor() {
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton("*");
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (count.getAndIncrement() == 0) {
try {
Filer filer = processingEnv.getFiler();
Element elt = processingEnv.getElementUtils().getTypeElement("gen.GeneratedClass");
JavaFileObject src = filer.createSourceFile("io.vertx.test.gen.GeneratedClass", elt);
try (Writer writer = src.openWriter()) {
writer.append("package io.vertx.test.gen;\npublic class GeneratedClass {\n}");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
};
Compiler<TestGenProcessor> compiler = buildCompiler(new TestGenProcessor(), "io.vertx.test.gen");
compiler.addProcessor(proc);
compiler.assertCompile();
assertEquals(3, count.get());
}
use of javax.annotation.processing.RoundEnvironment in project qpid-broker-j by apache.
the class ConfiguredObjectRegistrationGenerator method process.
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
if (!_elementProcessingDone) {
final Elements elementUtils = processingEnv.getElementUtils();
final TypeElement managedObjectElement = elementUtils.getTypeElement(MANAGED_OBJECT_CANONICAL_NAME);
try {
roundEnv.getElementsAnnotatedWith(managedObjectElement).stream().map(element -> elementUtils.getPackageOf(element)).flatMap(packageElement -> packageElement.getEnclosedElements().stream()).filter(element -> hasAnnotation(element, managedObjectElement)).forEach(annotatedElement -> processAnnotatedElement(elementUtils, managedObjectElement, annotatedElement));
for (Map.Entry<String, Set<String>> entry : _managedObjectClasses.entrySet()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format("Generating CO registration for package '%s'", entry.getKey()));
generateRegistrationFile(entry.getKey(), entry.getValue());
}
_managedObjectClasses.clear();
_typeMap.clear();
_categoryMap.clear();
_elementProcessingDone = true;
} catch (Exception e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error: " + e.getLocalizedMessage());
}
}
return false;
}
use of javax.annotation.processing.RoundEnvironment in project mule by mulesoft.
the class DescriptionDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext loadingContext) {
ProcessingEnvironment processingEnv = getParameterOrFail(loadingContext, PROCESSING_ENVIRONMENT);
TypeElement extensionElement = getParameterOrFail(loadingContext, EXTENSION_ELEMENT);
RoundEnvironment roundEnvironment = getParameterOrFail(loadingContext, ROUND_ENVIRONMENT);
ExtensionDescriptionDocumenter declarer = new ExtensionDescriptionDocumenter(processingEnv, roundEnvironment);
declarer.document(loadingContext.getExtensionDeclarer().getDeclaration(), extensionElement);
}
use of javax.annotation.processing.RoundEnvironment in project jvarkit by lindenb.
the class JVarkitAnnotationProcessor method process.
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
final String mainClass = System.getProperty("jvarkit.main.class");
final String thisDir = System.getProperty("jvarkit.this.dir");
roundEnv.getElementsAnnotatedWith(IncludeSourceInJar.class).stream().filter(E -> E.getKind() == ElementKind.CLASS).filter(E -> E.getAnnotation(IncludeSourceInJar.class) != null).forEach(E -> {
if (thisDir == null || thisDir.isEmpty())
return;
copySource(E);
});
/* find if roundEnv contains main class annotated with 'Program' annotation
* if true: generate a file that will tell Make to compile the markdown
* documentation
*/
roundEnv.getElementsAnnotatedWith(Program.class).stream().filter(E -> E.getKind() == ElementKind.CLASS).filter(E -> {
final Program prog = E.getAnnotation(Program.class);
return prog != null && prog.generate_doc();
}).forEach(E -> {
copySource(E);
final String className = E.toString();
if (mainClass == null)
return;
if (!mainClass.equals(className))
return;
final Program prog = E.getAnnotation(Program.class);
if (prog == null || !prog.generate_doc())
return;
try {
final Filer filer = super.processingEnv.getFiler();
FileObject fo = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "markdown.flag");
fo.openWriter().append(String.valueOf(mainClass)).close();
} catch (final Exception err) {
LOG.warn(err);
}
if (thisDir != null) {
final File index_html = new File(thisDir, "docs/index.html");
if (index_html.exists()) {
try {
final Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(index_html);
Element tr = dom.createElement("tr");
tr.setAttribute("id", prog.name());
// name
Element td = dom.createElement("th");
tr.appendChild(td);
Element a = dom.createElement("a");
a.setAttribute("href", E.getSimpleName() + ".html");
a.setAttribute("title", E.getSimpleName().toString());
td.appendChild(a);
a.appendChild(dom.createTextNode(prog.name()));
// desc
td = dom.createElement("td");
tr.appendChild(td);
td.appendChild(dom.createTextNode(prog.description()));
// keywords
td = dom.createElement("td");
tr.appendChild(td);
td.appendChild(dom.createTextNode(Arrays.asList(prog.keywords()).stream().collect(Collectors.joining(" "))));
// terms
td = dom.createElement("td");
tr.appendChild(td);
td.appendChild(dom.createTextNode(Arrays.asList(prog.terms()).stream().map(T -> "[" + T.getAccession() + "]" + T.getLabel()).collect(Collectors.joining(" "))));
// misc
td = dom.createElement("td");
tr.appendChild(td);
final DocumentFragment misc = dom.createDocumentFragment();
if (prog.deprecatedMsg() != null && !prog.deprecatedMsg().isEmpty()) {
Element span = dom.createElement("span");
span.setAttribute("style", "color:orange;");
span.setAttribute("title", "deprecated");
span.appendChild(dom.createTextNode(prog.deprecatedMsg()));
misc.appendChild(span);
misc.appendChild(dom.createTextNode(". "));
}
td.appendChild(misc);
final XPath xpath = XPathFactory.newInstance().newXPath();
final NodeList nodeList = (NodeList) xpath.evaluate("//table[1]/tbody/tr[@id='" + prog.name() + "']", dom, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
Node oldTr = nodeList.item(i);
if (tr != null) {
oldTr.getParentNode().replaceChild(tr, oldTr);
tr = null;
} else {
oldTr.getParentNode().removeChild(oldTr);
}
}
if (tr != null) {
Node tbody = (Node) xpath.evaluate("//table[1]/tbody", dom, XPathConstants.NODE);
if (tbody != null) {
tbody.appendChild(tr);
tbody.appendChild(dom.createTextNode("\n"));
tr = null;
}
}
if (tr != null) {
LOG.warn("Cannot insert new doc in " + index_html);
} else {
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(dom), new StreamResult(index_html));
}
} catch (final Exception err) {
LOG.warn(err);
}
} else {
LOG.warn("Cannot get " + index_html);
}
}
});
return true;
}
Aggregations