use of javax.tools.FileObject in project hibernate-orm by hibernate.
the class ClassWriter method writeFile.
public static void writeFile(MetaEntity entity, Context context) {
try {
String metaModelPackage = entity.getPackageName();
// need to generate the body first, since this will also update the required imports which need to
// be written out first
String body = generateBody(entity, context).toString();
FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(getFullyQualifiedClassName(entity, metaModelPackage));
OutputStream os = fo.openOutputStream();
PrintWriter pw = new PrintWriter(os);
if (!metaModelPackage.isEmpty()) {
pw.println("package " + metaModelPackage + ";");
pw.println();
}
pw.println(entity.generateImports());
pw.println(body);
pw.flush();
pw.close();
} catch (FilerException filerEx) {
context.logMessage(Diagnostic.Kind.ERROR, "Problem with Filer: " + filerEx.getMessage());
} catch (IOException ioEx) {
context.logMessage(Diagnostic.Kind.ERROR, "Problem opening file to write MetaModel for " + entity.getSimpleName() + ioEx.getMessage());
}
}
use of javax.tools.FileObject in project neo4j by neo4j.
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();
}
try (PrintWriter writer = newFilePrintWriter(file, StandardCharsets.UTF_8)) {
writer.append(line).append("\n");
}
}
use of javax.tools.FileObject in project robolectric by robolectric.
the class ServiceLoaderGenerator method generate.
@Override
public void generate(String shadowPackage) {
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.println(shadowPackage + '.' + GEN_CLASS);
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 graal by oracle.
the class ServiceProviderProcessor method writeProviderFile.
private void writeProviderFile(TypeElement serviceProvider, String interfaceName) {
String filename = "META-INF/providers/" + serviceProvider.getQualifiedName();
try {
FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, serviceProvider);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
writer.println(interfaceName);
writer.close();
} catch (IOException e) {
processingEnv.getMessager().printMessage(isBug367599(e) ? Kind.NOTE : Kind.ERROR, e.getMessage(), serviceProvider);
}
}
use of javax.tools.FileObject in project graal by oracle.
the class InstrumentRegistrationProcessor method loadIfFileAlreadyExists.
private int loadIfFileAlreadyExists(String filename, Properties p) {
try {
FileObject file = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", filename);
p.load(file.openInputStream());
return p.keySet().size() / NUMBER_OF_PROPERTIES_PER_ENTRY;
} catch (IOException e) {
// Ignore error. It is ok if the file does not exist
return 0;
}
}
Aggregations