use of javax.tools.FileObject in project accent4j by cinchapi.
the class RuntimeDynamics method newAnonymousObject.
/**
* Make a best effort to return an object that belongs to a class that is
* dynamically created at runtime.
*
* @return the anonymous object
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object newAnonymousObject() {
try {
StringBuilder source = new StringBuilder();
String clazz = "A" + System.currentTimeMillis();
source.append("public class ").append(clazz).append(" {}");
ByteArrayOutputStream output = new ByteArrayOutputStream();
SimpleJavaFileObject file = new SimpleJavaFileObject(URI.create(clazz + ".java"), javax.tools.JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return source.toString();
}
@Override
public OutputStream openOutputStream() throws IOException {
return output;
}
};
JavaFileManager manager = new ForwardingJavaFileManager(ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) {
@Override
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
return file;
}
};
ToolProvider.getSystemJavaCompiler().getTask(null, manager, null, null, null, Collections.singletonList(file)).call();
final byte[] bytes = output.toByteArray();
final Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
final sun.misc.Unsafe unsafe = (sun.misc.Unsafe) f.get(null);
final Class<?> anonymous = unsafe.defineClass(clazz, bytes, 0, bytes.length, null, null);
return anonymous.newInstance();
} catch (Exception e) {
return new Object() {
};
}
}
use of javax.tools.FileObject in project hudson-2.x by hudson.
the class PluginMarker method write.
/**
* Writes plugin class name to the defined location.
*
* @param typeElement detected element.
* @throws IOException exception.
*/
private void write(TypeElement typeElement) throws IOException {
FileObject f = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", META_INF_SERVICES_LOCATION);
Writer w = new OutputStreamWriter(f.openOutputStream(), "UTF-8");
try {
w.write(typeElement.getQualifiedName().toString());
} finally {
w.close();
}
}
use of javax.tools.FileObject 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.tools.FileObject in project dubbo by alibaba.
the class ClassPathMetadataStorage method getWriter.
private Writer getWriter(String resourceName) throws IOException {
FileObject fileObject = createResource(resourceName);
info("The resource[path : %s , deleted : %s] will be written", fileObject.toUri().getPath(), fileObject.delete());
return fileObject.openWriter();
}
use of javax.tools.FileObject in project spring-boot by spring-projects.
the class MetadataStore method getAdditionalMetadataStream.
private InputStream getAdditionalMetadataStream() throws IOException {
// Most build systems will have copied the file to the class output location
FileObject fileObject = this.environment.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", ADDITIONAL_METADATA_PATH);
File file = locateAdditionalMetadataFile(new File(fileObject.toUri()));
return (file.exists() ? new FileInputStream(file) : fileObject.toUri().toURL().openStream());
}
Aggregations