Java Code Examples for javax.tools.JavaFileObject#openWriter()
The following examples show how to use
javax.tools.JavaFileObject#openWriter() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GwtSerialization.java From auto with Apache License 2.0 | 6 votes |
private void writeSourceFile(String className, String text, TypeElement originatingType) { try { JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(className, originatingType); try (Writer writer = sourceFile.openWriter()) { writer.write(text); } } catch (IOException e) { processingEnv .getMessager() .printMessage( Diagnostic.Kind.WARNING, "Could not write generated class " + className + ": " + e); // A warning rather than an error for the reason explained in // AutoValueOrOneOfProcessor.writeSourceFile. } }
Example 2
Source File: JavaCompiler.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * Emit plain Java source for a class. * * @param env The attribution environment of the outermost class * containing this class. * @param cdef The class definition to be printed. */ JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException { JavaFileObject outFile = fileManager.getJavaFileForOutput(CLASS_OUTPUT, cdef.sym.flatname.toString(), JavaFileObject.Kind.SOURCE, null); if (inputFiles.contains(outFile)) { log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile); return null; } else { BufferedWriter out = new BufferedWriter(outFile.openWriter()); try { new Pretty(out, true).printUnit(env.toplevel, cdef); if (verbose) log.printVerbose("wrote.file", outFile); } finally { out.close(); } return outFile; } }
Example 3
Source File: Processor.java From courier with Apache License 2.0 | 6 votes |
private void writeClasses() { for(TypeElement element : postalAreaMap.keySet()) { final PostalArea area = postalAreaMap.get(element); try { JavaFileObject jfo = processingEnv.getFiler().createSourceFile( area.getTargetClassName() + CLASS_SUFFIX, element); Writer writer = jfo.openWriter(); writer.write(area.writeJava()); writer.flush(); writer.close(); } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage(), element); } } }
Example 4
Source File: DummyProcessor.java From j2cl with Apache License 2.0 | 6 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (createdDummy) { return false; } createdDummy = true; try { JavaFileObject dummySourceFile = processingEnv .getFiler() .createSourceFile("com.google.j2cl.transpiler.readable.aptgeneratordep.Dummy"); Writer writer = dummySourceFile.openWriter(); writer.write( "package com.google.j2cl.transpiler.readable.aptgeneratordep;\n" + "// Generated by DummyProcessor.java\n" + "public class Dummy {}\n"); writer.close(); } catch (IOException e) { throw new RuntimeException(e); } return false; }
Example 5
Source File: JavaCompiler.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** Emit plain Java source for a class. * @param env The attribution environment of the outermost class * containing this class. * @param cdef The class definition to be printed. */ JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException { JavaFileObject outFile = fileManager.getJavaFileForOutput(CLASS_OUTPUT, cdef.sym.flatname.toString(), JavaFileObject.Kind.SOURCE, null); if (inputFiles.contains(outFile)) { log.error(cdef.pos(), Errors.SourceCantOverwriteInputFile(outFile)); return null; } else { try (BufferedWriter out = new BufferedWriter(outFile.openWriter())) { new Pretty(out, true).printUnit(env.toplevel, cdef); if (verbose) log.printVerbose("wrote.file", outFile); } return outFile; } }
Example 6
Source File: RetrofitServiceProcessor.java From kaif-android with Apache License 2.0 | 6 votes |
private void generateCode(RetrofitServiceInterface retrofitServiceInterface) { TypeSpec typeSpec = retrofitServiceInterface.createRetryStaleInterface(); TypeElement annotatedClassElement = retrofitServiceInterface.getAnnotatedClassElement(); JavaFile javaFile = JavaFile.builder(elementUtils.getPackageOf(annotatedClassElement) .getQualifiedName() .toString(), typeSpec).build(); try { JavaFileObject jfo = filer.createSourceFile(retrofitServiceInterface.getQualifiedName()); try (Writer writer = jfo.openWriter()) { javaFile.writeTo(writer); } } catch (IOException e) { error(annotatedClassElement, e.getMessage()); } }
Example 7
Source File: ProcessingIntegrationTest.java From turbine with Apache License 2.0 | 6 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (first) { processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "proc warning"); try { JavaFileObject file = processingEnv.getFiler().createSourceFile("Gen.java"); try (Writer writer = file.openWriter()) { writer.write("class Gen {}"); } } catch (IOException e) { throw new UncheckedIOException(e); } first = false; } if (roundEnv.processingOver()) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "proc error"); } return false; }
Example 8
Source File: AdapterHolderStep.java From Ollie with Apache License 2.0 | 6 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (roundEnv.processingOver()) { try { String name = sourceWriter.createSourceName(null); JavaFileObject object = registry.getFiler().createSourceFile(name); Writer writer = object.openWriter(); sourceWriter.writeSource(writer, null); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } return false; }
Example 9
Source File: JavaCompiler.java From hottub with GNU General Public License v2.0 | 6 votes |
/** Emit plain Java source for a class. * @param env The attribution environment of the outermost class * containing this class. * @param cdef The class definition to be printed. */ JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException { JavaFileObject outFile = fileManager.getJavaFileForOutput(CLASS_OUTPUT, cdef.sym.flatname.toString(), JavaFileObject.Kind.SOURCE, null); if (inputFiles.contains(outFile)) { log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile); return null; } else { BufferedWriter out = new BufferedWriter(outFile.openWriter()); try { new Pretty(out, true).printUnit(env.toplevel, cdef); if (verbose) log.printVerbose("wrote.file", outFile); } finally { out.close(); } return outFile; } }
Example 10
Source File: Processor.java From immutables with Apache License 2.0 | 6 votes |
private void generateTemplateType(TypeElement templateType) throws IOException, Exception { SwissArmyKnife knife = new SwissArmyKnife(processingEnv, templateType); String string = readTemplateResource(templateType, knife); Unit unit = parseUnit(string); Unit resolved = transformUnit(knife, unit); TemplateWriter writingTransformer = new TemplateWriter(knife, templateType, GeneratedTypes.getSimpleName(templateType)); CharSequence template = writingTransformer.toCharSequence(resolved); JavaFileObject templateFile = knife.environment.getFiler().createSourceFile( GeneratedTypes.getQualifiedName(knife.elements, templateType), templateType); try (Writer writer = templateFile.openWriter()) { writer.append(template); } }
Example 11
Source File: BaseProcessorTest.java From vertx-docgen with Apache License 2.0 | 5 votes |
@Test public void testGen() throws Exception { AtomicInteger count = new AtomicInteger(); AbstractProcessor proc = new AbstractProcessor() { @Override public Set<String> getSupportedAnnotationTypes() { return Collections.singleton("*"); } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (count.getAndIncrement() == 0) { try { Filer filer = processingEnv.getFiler(); Element elt = processingEnv.getElementUtils().getTypeElement("gen.GeneratedClass"); JavaFileObject src = filer.createSourceFile("io.vertx.test.gen.GeneratedClass", elt); try (Writer writer = src.openWriter()) { writer.append("package io.vertx.test.gen;\npublic class GeneratedClass {\n}"); } } catch (Exception e) { e.printStackTrace(); } } return true; } }; Compiler<TestGenProcessor> compiler = buildCompiler(new TestGenProcessor(), "io.vertx.test.gen"); compiler.addProcessor(proc); compiler.assertCompile(); assertEquals(3, count.get()); }
Example 12
Source File: CompilationTest.java From RetroFacebook with Apache License 2.0 | 5 votes |
private void generateFoo(TypeElement type) throws IOException { String pkg = TypeSimplifier.packageNameOf(type); String className = type.getSimpleName().toString(); String generatedClassName = className + "Foo"; JavaFileObject source = processingEnv.getFiler().createSourceFile(pkg + "." + generatedClassName, type); PrintWriter writer = new PrintWriter(source.openWriter()); writer.println("package " + pkg + ";"); writer.println("public class " + generatedClassName + " {}"); writer.close(); }
Example 13
Source File: AutoValueCompilationTest.java From auto with Apache License 2.0 | 5 votes |
private void generateFoo(TypeElement type) throws IOException { String pkg = TypeSimplifier.packageNameOf(type); String className = type.getSimpleName().toString(); String generatedClassName = className + "Foo"; JavaFileObject source = processingEnv.getFiler().createSourceFile(pkg + "." + generatedClassName, type); PrintWriter writer = new PrintWriter(source.openWriter()); writer.println("package " + pkg + ";"); writer.println("public class " + generatedClassName + " {}"); writer.close(); }
Example 14
Source File: RetroFacebookProcessor.java From RetroFacebook with Apache License 2.0 | 5 votes |
private void writeSourceFile(String className, String text, TypeElement originatingType) { try { JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(className, originatingType); Writer writer = sourceFile.openWriter(); try { writer.write(text); } finally { writer.close(); } } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not write generated class " + className + ": " + e); } }
Example 15
Source File: CrnkProcessor.java From crnk-framework with Apache License 2.0 | 5 votes |
private void write(Element element, TypeSpec.Builder typeBuilder, boolean query) { try { String packageName = getPackageName(element); String qualifiedName = getQualifiedName(element, query); JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(qualifiedName, element); try (Writer writer = sourceFile.openWriter()) { JavaFile.builder(packageName, typeBuilder.build()) .indent(" ") .build() .writeTo(writer); } } catch (IOException e) { throw new IllegalStateException(e); } }
Example 16
Source File: AptinaTestCase.java From doma with Apache License 2.0 | 5 votes |
@Override public JavaFileObject getJavaFileObject() throws IOException { final JavaFileObject javaFileObject = testingJavaFileManager.getJavaFileForOutput( StandardLocation.SOURCE_OUTPUT, className, Kind.SOURCE, null); final Writer writer = javaFileObject.openWriter(); try { writer.write(source); } finally { closeSilently(writer); } return javaFileObject; }
Example 17
Source File: RetroWeiboProcessor.java From SimpleWeibo with Apache License 2.0 | 5 votes |
private void writeSourceFile(String className, String text, TypeElement originatingType) { try { JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(className, originatingType); Writer writer = sourceFile.openWriter(); try { writer.write(text); } finally { writer.close(); } } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not write generated class " + className + ": " + e); } }
Example 18
Source File: SugarProcessor.java From SugarAdapter with Apache License 2.0 | 4 votes |
private void generateInjectDelegateImpl(@NonNull Map<String, Set<InjectInfo>> map) { for (String holderClass : map.keySet()) { StringBuilder builder = new StringBuilder(); int lastIndex = holderClass.lastIndexOf("."); if (lastIndex < 0) { lastIndex = 0; } String className = holderClass.substring(lastIndex + 1) + "$InjectDelegateImpl"; builder.append("package ").append(holderClass, 0, lastIndex).append(";\n\n"); builder.append("import android.annotation.SuppressLint;\n"); builder.append("import android.view.View;\n"); builder.append("import androidx.annotation.NonNull;\n\n"); builder.append("import com.zhihu.android.sugaradapter.InjectDelegate;\n"); builder.append("import com.zhihu.android.sugaradapter.SugarHolder;\n\n"); builder.append("public final class ").append(className).append(" implements InjectDelegate {\n"); builder.append(" @Override\n"); builder.append(" @SuppressLint(\"ResourceType\")\n"); builder.append(" public <SH extends SugarHolder> void injectView(@NonNull SH sh, @NonNull View view) {\n"); builder.append(" if (sh instanceof ").append(holderClass).append(") {\n"); builder.append(" ").append(holderClass).append(" th = (").append(holderClass).append(") sh;\n"); for (InjectInfo info : map.get(holderClass)) { builder.append(" th.").append(info.getViewName()) .append(" = (").append(info.getViewType()).append(")") .append(" view.findViewById(").append(info.getViewIdStr()).append(");\n"); } builder.append(" }\n"); builder.append(" }\n"); builder.append("}\n"); try { JavaFileObject object = processingEnv.getFiler().createSourceFile(className); Writer writer = object.openWriter(); writer.write(builder.toString()); writer.flush(); writer.close(); } catch (IOException e) { throw new IllegalStateException(e); } } }
Example 19
Source File: BuckModuleAdapterPluginGenerator.java From buck with Apache License 2.0 | 4 votes |
private Writer createSourceCodeWriter(String className, TypeElement annotation) throws IOException { JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(className, annotation); return fileObject.openWriter(); }
Example 20
Source File: AnnotationsProcessor.java From android-transformer with Apache License 2.0 | 4 votes |
private void generateMapperJavaFile(MapperInfo mapper, Collection<String> classVars, Collection<String> imports, Collection<String> directFields, Collection<String> inverseFields) { try { String mapperCanonicalName = String.format("%s.%s", mapper.mapperPackageName, mapper.mapperClassName); writeTrace(String.format("Generating source file for Mapper with name %s", mapperCanonicalName)); JavaFileObject javaFileObject = processingEnv.getFiler().createSourceFile(mapperCanonicalName); BufferedWriter buffer = new BufferedWriter(javaFileObject.openWriter()); buffer.append(String.format(Tools.PACKAGE_PATTERN, mapper.mapperPackageName)); buffer.newLine(); for (String classImport : imports) { buffer.newLine(); buffer.append(classImport); } buffer.newLine(); buffer.newLine(); buffer.append(String.format(Tools.CLASS_PATTERN, mapper.mapperClassName)); if (classVars.size() > 0) { buffer.newLine(); for (String classVar : classVars) { buffer.newLine(); buffer.append("\t").append(classVar); } } generateTransformMethod(buffer, mapper.className, mapper.linkedClassName, directFields); generateTransformMethod(buffer, mapper.linkedClassName, mapper.className, inverseFields); buffer.newLine(); buffer.append("}"); buffer.close(); } catch (IOException error) { throw new RuntimeException(error); } }