use of javax.annotation.processing.Filer in project camel by apache.
the class AnnotationProcessorHelper method processFile.
/**
* Helper method to produce class output text file using the given handler
*/
public static void processFile(ProcessingEnvironment processingEnv, String packageName, String fileName, Func1<PrintWriter, Void> handler) {
PrintWriter 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(processingEnv, "Cannot convert output directory resource URI to a file " + e);
}
}
if (file == null) {
warning(processingEnv, "No class output directory could be found!");
} else {
file.getParentFile().mkdirs();
out = new FileWriter(file);
writer = new PrintWriter(out);
handler.call(writer);
}
} catch (IOException e) {
log(processingEnv, e);
} finally {
if (writer != null) {
writer.close();
}
}
}
use of javax.annotation.processing.Filer in project JikesRVM by JikesRVM.
the class SysCallProcessor method processCandidates.
private void processCandidates(Set<? extends Element> canidatesForProcessing) {
String canonicalName = this.getClass().getCanonicalName();
Filer filer = processingEnv.getFiler();
Messager messager = processingEnv.getMessager();
for (Element element : canidatesForProcessing) {
if (element.getKind().isClass()) {
SysCallVisitor visitor = new SysCallVisitor(canonicalName, filer, messager, elementUtils);
element.accept(visitor, null);
}
}
}
use of javax.annotation.processing.Filer in project ngAndroid by davityle.
the class FileHelper method findRootProjectHolder.
/**
* We use a dirty trick to find the AndroidManifest.xml file, since it's not
* available in the classpath. The idea is quite simple : create a fake
* class file, retrieve its URI, and start going up in parent folders to
* find the AndroidManifest.xml file. Any better solution will be
* appreciated.
*/
public Option<FileHolder> findRootProjectHolder() {
Filer filer = processingEnv.getFiler();
FileObject dummySourceFile;
try {
dummySourceFile = filer.createResource(StandardLocation.SOURCE_OUTPUT, "com", "dummy" + System.currentTimeMillis());
} catch (IOException ignored) {
return Option.absent();
}
String dummySourceFilePath = dummySourceFile.toUri().toString();
if (dummySourceFilePath.startsWith("file:")) {
if (!dummySourceFilePath.startsWith("file://")) {
dummySourceFilePath = "file://" + dummySourceFilePath.substring("file:".length());
}
} else {
dummySourceFilePath = "file://" + dummySourceFilePath;
}
URI cleanURI;
try {
cleanURI = new URI(dummySourceFilePath);
} catch (URISyntaxException e) {
return Option.absent();
}
try {
File dummyFile = new File(cleanURI);
File sourcesGenerationFolder = dummyFile.getParentFile();
File projectRoot = sourcesGenerationFolder.getParentFile();
return Option.of(new FileHolder(dummySourceFilePath, sourcesGenerationFolder, projectRoot));
} catch (IllegalArgumentException ex) {
return Option.absent();
}
}
use of javax.annotation.processing.Filer in project spring-boot by spring-projects.
the class AutoConfigureAnnotationProcessor method writeProperties.
private void writeProperties() throws IOException {
if (!this.properties.isEmpty()) {
Filer filer = this.processingEnv.getFiler();
FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", PROPERTIES_PATH);
try (Writer writer = new OutputStreamWriter(file.openOutputStream(), StandardCharsets.UTF_8)) {
for (Map.Entry<String, String> entry : this.properties.entrySet()) {
writer.append(entry.getKey());
writer.append("=");
writer.append(entry.getValue());
writer.append(System.lineSeparator());
}
}
}
}
use of javax.annotation.processing.Filer 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());
}
Aggregations