use of javax.tools.FileObject in project lombok by rzwitserloot.
the class SpiProcessorPersistence method determineOutputLocation.
private File determineOutputLocation() {
FileObject resource;
try {
resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "META-INF", "locator.tmp");
} catch (FileNotFoundException e) {
e.printStackTrace();
// Could happen
return null;
} catch (IOException e) {
e.printStackTrace();
logger.printMessage(Kind.NOTE, "IOException while determining output location: " + e.getMessage());
return null;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
}
URI uri = resource.toUri();
return new File(new File(uri).getParentFile(), "services");
}
use of javax.tools.FileObject in project buck by facebook.
the class ClassUsageTrackerTest method readingNonJavaFileFromGetFileForOutputShouldBeTracked.
@Test
public void readingNonJavaFileFromGetFileForOutputShouldBeTracked() throws IOException {
final FileObject fileObject = fileManager.getFileForOutput(null, null, SINGLE_NON_JAVA_FILE_NAME, null);
fileObject.openInputStream();
assertTrue(fileWasRead(TEST_JAR_PATH, SINGLE_NON_JAVA_FILE_NAME));
}
use of javax.tools.FileObject in project buck by facebook.
the class MyAnnotationProcessor method process.
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
if (runCount > 0) {
return false;
}
try {
List<String> required_configs = Arrays.asList("res1.json");
List<String> maybe_configs = Arrays.asList("res2.json");
OutputStream outputStream = mFiler.createResource(StandardLocation.CLASS_OUTPUT, "com.facebook.example.config", "collected_configs.json", mElementUtils.getTypeElement("com.facebook.example.MyAnnotationProcessor")).openOutputStream();
StringBuilder sb = new StringBuilder();
sb.append("{\n");
byte[] buffer = new byte[1024];
for (String config : required_configs) {
try {
FileObject inputFile = mFiler.getResource(StandardLocation.CLASS_PATH, "", "META-INF/" + config);
int len = inputFile.openInputStream().read(buffer);
String contents = new String(buffer, 0, len);
sb.append(" \"").append(config).append("\": ");
sb.append("\"").append(contents).append("\"");
sb.append(",\n");
} catch (Exception e) {
mMessager.printMessage(Diagnostic.Kind.ERROR, "Could not open " + config + " because of " + e.getLocalizedMessage());
}
}
for (String config : maybe_configs) {
try {
FileObject inputFile = mFiler.getResource(StandardLocation.CLASS_PATH, "", "META-INF/" + config);
int len = inputFile.openInputStream().read(buffer);
String contents = new String(buffer, 0, len);
sb.append(" \"").append(config).append("\": ");
sb.append("\"").append(contents).append("\"");
sb.append(",\n");
} catch (Exception e) {
mMessager.printMessage(Diagnostic.Kind.NOTE, config + " not found");
}
}
sb.append("\"version\": 1");
sb.append("\n}");
outputStream.write(sb.toString().getBytes());
outputStream.close();
} catch (Exception e) {
mMessager.printMessage(Diagnostic.Kind.ERROR, "" + e);
throw new RuntimeException(e);
}
runCount += 1;
return true;
}
use of javax.tools.FileObject 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.tools.FileObject in project druid by druid-io.
the class LocalDataSegmentPuller method buildFileObject.
public static FileObject buildFileObject(final URI uri) {
final Path path = Paths.get(uri);
final File file = path.toFile();
return new FileObject() {
@Override
public URI toUri() {
return uri;
}
@Override
public String getName() {
return path.getFileName().toString();
}
@Override
public InputStream openInputStream() throws IOException {
return new FileInputStream(file);
}
@Override
public OutputStream openOutputStream() throws IOException {
return new FileOutputStream(file);
}
@Override
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
return new FileReader(file);
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
throw new UOE("CharSequence not supported");
}
@Override
public Writer openWriter() throws IOException {
return new FileWriter(file);
}
@Override
public long getLastModified() {
return file.lastModified();
}
@Override
public boolean delete() {
return file.delete();
}
};
}
Aggregations