Java 8 Filer - 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 Filer class.

This code example shows how to use the following methods:createSourceFile
 
			if (classOriginatingElements.length == 0) {
				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:createResource
		this.filer = filer;
	} 
 
	@Override 
	public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
		FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, pkg.name(), fileName);
		return resource.openOutputStream();
	} 
 
	@Override 
	public void close() throws IOException {
	} 
} 
6
CodeRank
This code example shows how to use the following methods:createSourceFile
  } 
 
  public AbsWriter writeClass(EnclosingClass enclosingClass) throws IOException {
    TypeElement classType = enclosingClass.getElement();
    String fqcn = enclosingClass.getClassPackage() + "." + enclosingClass.getClassName() + suffix;
    JavaFileObject jfo = filer.createSourceFile(fqcn, classType);
    boolean isView = typeUtils.isAssignable(classType.asType(),
        elementUtils.getTypeElement("android.view.View").asType());
    return isView ? new ViewWriter(jfo, suffix, enclosingClass)
        : new ObjectWriter(jfo, suffix, enclosingClass);
  } 
} 
5
CodeRank
This code example shows how to use the following methods:createResource, createSourceFile
	 * Test the toUri() method on various file objects. 
	 */ 
	public void testURI(Element e, String pkg, String relName) throws Exception {
		 
		// Generated non-source file 
		FileObject foGenNonSrc = _filer.createResource(StandardLocation.SOURCE_OUTPUT,
				pkg, relName, e);
		checkGenUri(foGenNonSrc, relName, helloStr, "generated non-source file");
		 
		// Generated source file 
		FileObject foGenSrc = _filer.createSourceFile("g.G", e);
		checkGenUri(foGenSrc, "G", javaStr, "generated source file");
	} 
	 
	private void checkGenUri(FileObject fo, String name, String content, String category) throws Exception {
		PrintWriter pw = null;
		try { 
			pw = new PrintWriter(fo.openWriter());
			pw.print(content);
		} finally { 
5
CodeRank
This code example shows how to use the following methods:createSourceFile
    } 
 
    @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:createSourceFile
		} 
		 
		// Get a writer 
		JavaFileObject jfo = null;
		try { 
			jfo = _filer.createSourceFile(qname, annotatedEl);
		} catch (IOException e) {
			_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) {
4
CodeRank
This code example shows how to use the following methods:createSourceFile
This code example shows how to use the following methods:createSourceFile
 
	@Override 
	public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
		String qualifiedClassName = toQualifiedClassName(pkg, fileName);
 
		JavaFileObject sourceFile = filer.createSourceFile(qualifiedClassName);
 
		return sourceFile.openOutputStream();
	} 
 
	private String toQualifiedClassName(JPackage pkg, String fileName) {
		int suffixPosition = fileName.lastIndexOf('.');
		String className = fileName.substring(0, suffixPosition);
		String qualifiedClassName = pkg.name() + "." + className;
		return qualifiedClassName;
	} 
 
	@Override 
	public void close() throws IOException {
	} 
4
CodeRank
This code example shows how to use the following methods:createResource, createSourceFile
 
    private void writeRuntimeFile() throws Exception {
        Map<String, Object> model = new HashMap<>();
        model.put("runtimeMenuItemExtensions", runtimeExtensions);
 
        JavaFileObject sourceFile = filer.createSourceFile(RUNTIME_FILENAME);
        OutputStream output = sourceFile.openOutputStream();
        new TemplateProcessor().process(RUNTIME_TEMPLATE, model, output);
        output.flush();
        output.close();
    } 
 
    private void writeSubsystemFile() throws Exception{
        Map<String, Object> model = new HashMap<>();
        model.put("subsystemExtensions", subsystemDeclararions);
 
        JavaFileObject sourceFile = filer.createSourceFile(SUBSYSTEM_FILENAME);
        OutputStream output = sourceFile.openOutputStream();
        new TemplateProcessor().process(SUBSYSTEM_TEMPLATE, model, output);
        output.flush();

5
CodeRank
This code example shows how to use the following methods:createSourceFile
	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
		if (!roundEnv.processingOver()) {
 
			final Filer filer = processingEnv.getFiler();
			try { 
				final JavaFileObject sourceFile = filer.createSourceFile("tycho590.processed.Processor");
				Writer sourceWriter = sourceFile.openWriter();
				sourceWriter.append("package tycho590.processed;");
				sourceWriter.append(String.format("%n"));
				sourceWriter.append("public class Processor {}");
				sourceWriter.close();				
			} catch (IOException ex) {
				//That will happen on round 2 because source file already exists 
				System.out.println("[APT] " + ex.getMessage());				
			} 
		} 
		return false; 
 
	} 
} 
4
CodeRank
See Code Examples for Java 8 Filer Methods: