use of javax.tools.FileObject 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.tools.FileObject in project suite by stupidsing.
the class JdkUtilTest method compile.
private <I> Class<? extends I> compile(Class<I> clazz, String className, String source) {
var filename = className.replace('.', '/') + ".java";
var baos_ = new ByteArrayOutputStream();
try (var baos = baos_) {
var sjfo = new SimpleJavaFileObject(URI.create(filename), Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return source;
}
public OutputStream openOutputStream() {
return baos;
}
};
var sfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
var jfm = new ForwardingJavaFileManager<>(sfm) {
public JavaFileObject getJavaFileForOutput(Location loc, String clazz, Kind kind, FileObject sibling) {
return sjfo;
}
};
ToolProvider.getSystemJavaCompiler().getTask(null, jfm, null, null, null, List.of(sjfo)).call();
} catch (IOException ex) {
fail(ex);
}
return new UnsafeUtil().defineClass(clazz, filename, baos_.toByteArray());
}
use of javax.tools.FileObject in project classindex by atteo.
the class ClassIndexProcessor method writeSimpleNameIndexFile.
private void writeSimpleNameIndexFile(Set<String> elementList, String resourceName) throws IOException {
FileObject file = readOldIndexFile(elementList, resourceName);
if (file != null) {
/**
* Ugly hack for Eclipse JDT incremental compilation.
* Eclipse JDT can't createResource() after successful getResource().
* But we can file.openWriter().
*/
try {
writeIndexFile(elementList, resourceName, file);
return;
} catch (IllegalStateException e) {
// Thrown by HotSpot Java Compiler
}
}
writeIndexFile(elementList, resourceName, null);
}
use of javax.tools.FileObject in project robolectric by robolectric.
the class ServiceLoaderGenerator method generate.
@Override
public void generate() {
final String fileName = "org.robolectric.internal.ShadowProvider";
try {
FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/services/" + fileName);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
pw.print(shadowPackage + '.' + GEN_CLASS + '\n');
pw.close();
} catch (IOException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Failed to write service loader metadata file: " + e);
throw new RuntimeException(e);
}
}
use of javax.tools.FileObject in project neo4j-mobile-android by neo4j-contrib.
the class AnnotationProcessor method addTo.
void addTo(String line, String... path) throws IOException {
FileObject fo = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", path(path));
URI uri = fo.toUri();
File file;
try {
file = new File(uri);
} catch (Exception e) {
file = new File(uri.toString());
}
if (file.exists()) {
for (String previous : nl.split(fo.getCharContent(true), 0)) {
if (line.equals(previous))
return;
}
} else {
file.getParentFile().mkdirs();
}
new FileWriter(file, true).append(line).append("\n").close();
}
Aggregations