Java 8 JavaFileObject - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 JavaFileObject class.

This code example shows how to use the following methods:openOutputStream
				LOGGER.info("Generating class with no originating element: {}", qualifiedClassName);
			} 
 
			sourceFile = filer.createSourceFile(qualifiedClassName, classOriginatingElements);
 
			return sourceFile.openOutputStream();
		} catch (FilerException e) {
			LOGGER.error("Could not generate source file for {}", qualifiedClassName, e.getMessage());
			/* 
			 * This exception is expected, when some files are created twice. We 
			 * cannot delete existing files, unless using a dirty hack. Files a 
			 * created twice when the same file is created from different 
			 * annotation rounds. Happens when renaming classes, and for 
			 * Background executor. It also probably means I didn't fully 
			 * understand how annotation processing works. If anyone can point 
			 * me out... 
			 */ 
			return VOID_OUTPUT_STREAM;
		} 
	} 
6
CodeRank
This code example shows how to use the following methods:openOutputStream
    } 
 
    @Test
    public void testCreateSourceFile() throws IOException {
 
        when(mockFiler.createSourceFile(TEST_PACKAGE + "." + TEST_CLASS)).thenReturn(mockFile);
        when(mockFile.openOutputStream()).thenReturn(mockOutputStream);
 
        assertEquals(mockOutputStream, codeWriter.openBinary(codeModel._package(TEST_PACKAGE), TEST_CLASS));
 
        codeWriter.close();
        verify(mockOutputStream).flush();
        verify(mockOutputStream).close();
    } 
} 
5
CodeRank
This code example shows how to use the following methods:openWriter
        Template template = null;
 
        PrintWriter w =  null;
        try { 
            template = Velocity.getTemplate(templatePath);
            w = new PrintWriter(jfo.openWriter());
            template.merge(context, w);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IOException("Impossible to generate annotation database.", ex);
        } finally { 
            if( w != null ) {
                try { 
                    w.close();
                } catch( Exception ex ) {
                    ex.printStackTrace();
                    throw new IOException("Impossible to close annotation database.", ex);
                } 
            } 
        } 
5
CodeRank
This code example shows how to use the following methods:getName, openWriter
			_messager.printMessage(Diagnostic.Kind.WARNING, "Unable to open file for class " + qname, annotatedEl);
			return; 
		} 
		PrintWriter pw = null;
		try { 
			pw = new PrintWriter(jfo.openWriter());
		} catch (IOException e) {
			_messager.printMessage(Diagnostic.Kind.WARNING, "Unable to get writer for file " + jfo.getName());
			return; 
		} 
		 
		// Generate the class 
		if (summary) {
			_classesToSummarize.put(qname, annotatedEl);
		} 
		pw.println("// Generated by " + this.getClass().getName());
		pw.println("package " + pkg + ";");
		if (rounds > 1) {
			pw.println("import " + GenClass6.class.getCanonicalName() + ";");
			StringBuilder sb = new StringBuilder();
5
CodeRank
This code example shows how to use the following methods:getName
    } 
 
    @Override 
    public String inferBinaryName(Location loc, JavaFileObject file) {
        if (file instanceof JavaFileObjectImpl) {
            return file.getName();
        } 
 
        return super.inferBinaryName(loc, file);
    } 
 
    @Override 
    public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse)
                                                                                                                 throws IOException {
        Iterable<JavaFileObject> result = super.list(location, packageName, kinds, recurse);
 
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        List<URL> urlList = new ArrayList<URL>();
        Enumeration<URL> e = contextClassLoader.getResources("com");
        while (e.hasMoreElements()) {
5
CodeRank
This code example shows how to use the following methods:getName, openWriter
        public URI toUri() {
            return delegate.toUri();
        } 
 
        public String getName() {
            return delegate.getName();
        } 
 
        public InputStream openInputStream() throws IOException {
            checkRead(); 
            return delegate.openInputStream();
        } 
 
        public OutputStream openOutputStream() throws IOException {
            checkWrite(); 
            return delegate.openOutputStream();
        } 
 
        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
            checkRead(); 

5
CodeRank
This code example shows how to use the following methods:getLastModified
        JavacFileManager fm = new JavacFileManager(context, false, null);
        ZipFileIndexArchive zfia = new ZipFileIndexArchive(fm, zfi);
        JavaFileObject jfo =
            zfia.getFileObject(TEST_ENTRY_NAME.dirname(),
                                   TEST_ENTRY_NAME.basename());
        long jfoTime = jfo.getLastModified();
 
        check(je, jarEntryTime, jfo, jfoTime);
 
        if (errors > 0)
            throw new Exception(errors + " occurred");
    } 
 
    void check(Object ref, long refTime, Object test, long testTime) {
        if (refTime == testTime)
            return; 
        System.err.println("Error: ");
        System.err.println("Expected: " + getText(ref, refTime));
        System.err.println("   Found: " + getText(test, testTime));
        errors++;
5
CodeRank
This code example shows how to use the following methods:openOutputStream
    @Override 
    public OutputStream openBinary(JPackage jPackage, String fileName) throws IOException {
        //generate a source file based on package and filename 
        JavaFileObject sourceFile = filer.createSourceFile(toQualifiedClassName(jPackage, fileName));
 
        OutputStream os = sourceFile.openOutputStream();
        openStreams.add(os);
 
        return os;
    } 
 
    private String toQualifiedClassName(JPackage pkg, String fileName) {
        return new PackageClass(pkg.name(), fileName).getFullyQualifiedName();
    } 
 
    @Override 
    public void close() throws IOException {
        for (OutputStream openStream : openStreams) {
            openStream.flush();
            openStream.close();
5
CodeRank
This code example shows how to use the following methods:openOutputStream
    @Override 
    public OutputStream openBinary(JPackage jPackage, String fileName) throws IOException {
        //generate a source file based on package and filename 
        JavaFileObject sourceFile = filer.createSourceFile(toQualifiedClassName(jPackage, fileName));
 
        OutputStream os = sourceFile.openOutputStream();
        openStreams.add(os);
 
        return os;
    } 
 
    private String toQualifiedClassName(JPackage pkg, String fileName) {
        return pkg.name() + "." + fileName.replace(".java", "");
    } 
 
    @Override 
    public void close() throws IOException {
        for (OutputStream openStream : openStreams) {
            openStream.flush();
            openStream.close();
5
CodeRank
This code example shows how to use the following methods:getKind
        super(context, definitive);
    } 
 
    @Override 
    protected JavaFileObject preferredFileObject(JavaFileObject a, JavaFileObject b) {
        return a.getKind() == Kind.CLASS ? a : b;
    } 
 
    @Override 
    protected EnumSet<JavaFileObject.Kind> getPackageFileKinds() {
        return EnumSet.of(JavaFileObject.Kind.CLASS);
    } 
} 
5
CodeRank
See Code Examples for Java 8 JavaFileObject Methods: