Search in sources :

Example 66 with FileObject

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() {
        };
    }
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) JavaFileManager(javax.tools.JavaFileManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Field(java.lang.reflect.Field) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) FileObject(javax.tools.FileObject) JavaFileObject(javax.tools.JavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) FileObject(javax.tools.FileObject) JavaFileObject(javax.tools.JavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject)

Example 67 with FileObject

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();
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) FileObject(javax.tools.FileObject) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 68 with FileObject

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();
    }
}
Also used : FileObject(javax.tools.FileObject) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Filer(javax.annotation.processing.Filer) URI(java.net.URI) File(java.io.File)

Example 69 with FileObject

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();
}
Also used : FileObject(javax.tools.FileObject)

Example 70 with FileObject

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());
}
Also used : FileObject(javax.tools.FileObject) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FileObject (javax.tools.FileObject)98 IOException (java.io.IOException)61 File (java.io.File)21 TypeElement (javax.lang.model.element.TypeElement)19 PrintWriter (java.io.PrintWriter)17 Writer (java.io.Writer)16 Filer (javax.annotation.processing.Filer)14 Element (javax.lang.model.element.Element)14 BufferedWriter (java.io.BufferedWriter)12 ArrayList (java.util.ArrayList)12 OutputStream (java.io.OutputStream)11 JavaFileObject (javax.tools.JavaFileObject)11 OutputStreamWriter (java.io.OutputStreamWriter)10 URI (java.net.URI)10 Properties (java.util.Properties)10 InputStream (java.io.InputStream)8 FileWriter (java.io.FileWriter)7 FilerException (javax.annotation.processing.FilerException)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)6 BufferedReader (java.io.BufferedReader)6