use of javax.annotation.processing.Filer in project qpid-broker-j by apache.
the class ConfiguredObjectFactoryGenerator method process.
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
return true;
}
Filer filer = processingEnv.getFiler();
try {
for (Element e : roundEnv.getElementsAnnotatedWith(ManagedObjectFactoryConstructor.class)) {
if (e.getKind() == ElementKind.CONSTRUCTOR) {
ExecutableElement constructorElement = (ExecutableElement) e;
generateAccessControlEnforcingSubclass(filer, constructorElement);
generateObjectFactory(filer, constructorElement);
}
}
} catch (Exception e) {
try (StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw)) {
e.printStackTrace(pw);
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Unexpected Error: " + sw.toString());
} catch (IOException ioe) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error: " + ioe.getLocalizedMessage());
}
}
return true;
}
use of javax.annotation.processing.Filer in project ballerina by ballerina-lang.
the class ComposerServiceProcessor method createServiceConfig.
private void createServiceConfig(String interfaceName, List<String> implClasses) {
Filer filer = this.processingEnv.getFiler();
Writer writer = null;
try {
writer = filer.createResource(StandardLocation.CLASS_OUTPUT, "", JAVA_SPI_SERVICES_BASE_PATH + interfaceName).openWriter();
writer.write(String.join("\n", implClasses));
} catch (IOException e) {
throw new RuntimeException("Error creating Java SPI services file: " + e.getMessage(), e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException ignore) {
}
}
}
}
use of javax.annotation.processing.Filer in project jvarkit by lindenb.
the class JVarkitAnnotationProcessor method copySource.
private void copySource(final javax.lang.model.element.Element element) {
if (element == null || element.getKind() != ElementKind.CLASS)
return;
final String thisDir = System.getProperty("jvarkit.this.dir");
if (thisDir == null || thisDir.isEmpty()) {
System.err.println("jvarkit.this.dir is not defined");
return;
}
Writer writer = null;
Reader reader = null;
try {
do {
String className = element.toString();
if (className == null || className.isEmpty() || className.equals("java.lang.Object"))
return;
int dollar = className.indexOf('$');
if (dollar != -1)
className = className.substring(0, dollar);
File javaFile = new File(thisDir + "/src/main/java/" + className.replace('.', '/') + ".java");
if (!javaFile.exists())
break;
final Filer filer = super.processingEnv.getFiler();
final String packageName;
final String fileName;
int dot = className.lastIndexOf('.');
if (dot == -1) {
packageName = "";
fileName = className;
} else {
packageName = className.substring(0, dot);
fileName = className.substring(dot + 1);
}
final FileObject fo = filer.createResource(StandardLocation.CLASS_OUTPUT, packageName, fileName + ".java");
if (new File(fo.getName()).exists())
break;
System.err.println("## Copying " + javaFile + " -> " + fo.getName());
reader = new FileReader(javaFile);
writer = fo.openWriter();
final char[] buffer = new char[1024];
int nRead;
while ((nRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, nRead);
}
writer.close();
writer = null;
reader.close();
reader = null;
} while (false);
} catch (final Exception err) {
System.err.println(err.getMessage());
} finally {
if (writer != null)
try {
writer.close();
} catch (Exception err) {
}
if (reader != null)
try {
reader.close();
} catch (Exception err) {
}
}
final TypeMirror superclass = ((TypeElement) element).getSuperclass();
if (superclass == null || superclass.getKind() == TypeKind.NONE) {
return;
}
final DeclaredType kind = (DeclaredType) superclass;
copySource(kind.asElement());
}
use of javax.annotation.processing.Filer 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;
}
use of javax.annotation.processing.Filer in project fabric8 by fabric8io.
the class AbstractAnnotationProcessor method writeFile.
/**
* Helper method to produce class output text file using the given handler
*/
protected void writeFile(String packageName, String fileName, String text) {
Writer writer = null;
try {
Writer out;
Filer filer = processingEnv.getFiler();
FileObject resource;
try {
resource = filer.getResource(StandardLocation.CLASS_OUTPUT, packageName, fileName);
} catch (Throwable e) {
resource = filer.createResource(StandardLocation.CLASS_OUTPUT, packageName, fileName);
}
URI uri = resource.toUri();
File file = null;
if (uri != null) {
try {
file = new File(uri.getPath());
} catch (Exception e) {
warning("Could not convert output directory resource URI to a file " + e);
}
}
if (file == null) {
warning("No class output directory could be found!");
} else {
file.getParentFile().mkdirs();
log("Generating file " + file);
writer = new FileWriter(file);
writer.write(text);
}
} catch (IOException e) {
log(e);
} finally {
IOHelper.close(writer);
}
}
Aggregations