Java Code Examples for javax.tools.JavaFileObject#openOutputStream()

The following examples show how to use javax.tools.JavaFileObject#openOutputStream() . 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: ClassWriter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** Emit a class file for a given class.
 *  @param c      The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
    throws IOException, PoolOverflow, StringOverflow
{
    JavaFileObject outFile
        = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
                                           c.flatname.toString(),
                                           JavaFileObject.Kind.CLASS,
                                           c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propagating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
Example 2
Source File: ClassWriter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Emit a class file for a given class.
 *
 * @param c The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
        throws IOException, PoolOverflow, StringOverflow {
    JavaFileObject outFile
            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
            c.flatname.toString(),
            JavaFileObject.Kind.CLASS,
            c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propogating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
Example 3
Source File: ClassWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** Emit a class file for a given class.
 *  @param c      The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
    throws IOException, PoolOverflow, StringOverflow
{
    JavaFileObject outFile
        = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
                                           c.flatname.toString(),
                                           JavaFileObject.Kind.CLASS,
                                           c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propagating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
Example 4
Source File: AnnotationProcessor.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  if (!generated) {
    try {
      JavaFileObject sourceFile =
          processingEnv.getFiler().createSourceFile("com.facebook.buck.Generated");
      try (OutputStream outputStream = sourceFile.openOutputStream()) {
        outputStream.write(
            "package com.facebook.buck;\npublic class Generated { public static final int Foo = 3; }"
                .getBytes());
      }
      generated = true;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  return true;
}
 
Example 5
Source File: ModuleNamesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NonNull
private Iterable<JavaFileObject> listModuleContent(
        @NonNull final ModLoc modLoc,
        @NonNull final String packageName,
        @NonNull final Set<JavaFileObject.Kind> kinds) throws IOException {
    final CachingArchiveProvider cap = CachingArchiveProvider.getDefault();
    final Collection<JavaFileObject> res = new ArrayList<>();
    if (javaBaseModInfo != null && "java.base".equals(modLoc.getModuleName()) && packageName.isEmpty()) {
        final JavaFileObject jfo = new MemJFO("module-info");
        try(OutputStream out = jfo.openOutputStream()) {
            final byte[] data = javaBaseModInfo.get();
            out.write(data, 0, data.length);
        }
        res.add(jfo);
    }
    for (URL url : modLoc.getRoots()) {
        final Archive ca = cap.getArchive(url, false);
        if (ca != null) {
            StreamSupport.stream(
                    ca.getFiles(FileObjects.convertPackage2Folder(packageName), null, kinds, null, false).spliterator(),
                    false)
                    .forEach(res::add);
        }
    }
    return res;
}
 
Example 6
Source File: ClassWriter.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Emit a class file for a given class.
 *  @param c      The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
    throws IOException, PoolOverflow, StringOverflow
{
    JavaFileObject outFile
        = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
                                           c.flatname.toString(),
                                           JavaFileObject.Kind.CLASS,
                                           c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propagating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
Example 7
Source File: JavacTurbineTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  if (!first) {
    return false;
  }
  if (roundEnv.getRootElements().isEmpty()) {
    return false;
  }
  first = false;
  Element element = roundEnv.getRootElements().iterator().next();
  try {
    JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile("Generated", element);
    try (OutputStream os = sourceFile.openOutputStream()) {
      os.write("class Generated { public static String x = \"".getBytes(UTF_8));
      os.write(0xc2); // write an unpaired surrogate
      os.write("\";}}".getBytes(UTF_8));
    }
  } catch (IOException e) {
    throw new IOError(e);
  }
  return false;
}
 
Example 8
Source File: ClassWriter.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Emit a class file for a given class.
 *
 * @param c The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
        throws IOException, PoolOverflow, StringOverflow {
    JavaFileObject outFile
            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
            c.flatname.toString(),
            JavaFileObject.Kind.CLASS,
            c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propogating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
Example 9
Source File: ClassWriter.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/** Emit a class file for a given class.
 *  @param c      The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
    throws IOException, PoolOverflow, StringOverflow
{
    JavaFileObject outFile
        = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
                                           c.flatname.toString(),
                                           JavaFileObject.Kind.CLASS,
                                           c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propagating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
Example 10
Source File: SPIProcessor.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void writeSearchIndexFile() throws IOException {
    Map<String, Object> model = new HashMap<>();
    model.put("metaData", searchIndexDeclarations);

    JavaFileObject sourceFile = filer.createSourceFile(SEARCH_INDEX_FILENAME);
    OutputStream output = sourceFile.openOutputStream();
    new TemplateProcessor().process(SEARCH_INDEX_TEMPLATE, model, output);
    output.flush();
    output.close();
}
 
Example 11
Source File: SPIProcessor.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void writeAccessControlFile() throws Exception {
    Map<String, Object> model = new HashMap<>();
    model.put("metaData", requiredResourcesDeclarations);
    model.put("operations", bootstrapOperations);

    JavaFileObject sourceFile = filer.createSourceFile(REQUIRED_RESOURCES_FILENAME);
    OutputStream output = sourceFile.openOutputStream();
    new TemplateProcessor().process(REQUIRED_RESOURCES_TEMPLATE, model, output);
    output.flush();
    output.close();
}
 
Example 12
Source File: SPIProcessor.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void writeVersionInfo() throws IOException {
    Map<String, String> options = processingEnv.getOptions();
    String version = options.containsKey("version") ? options.get("version") : "n/a";
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("version", version);

    JavaFileObject sourceFile = filer.createSourceFile(VERSION_INFO_FILENAME);
    OutputStream output = sourceFile.openOutputStream();
    new TemplateProcessor().process(VERSION_INFO_TEMPLATE, model, output);
    output.flush();
    output.close();
}
 
Example 13
Source File: MyAnnotationProcessor.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
    try {
      TypeElement cls = (TypeElement) element;
      PackageElement pkg = (PackageElement) cls.getEnclosingElement();
      String clsSimpleName = "My" + cls.getSimpleName();
      String clsQualifiedName = pkg.getQualifiedName() + "." + clsSimpleName;
      JavaFileObject sourceFile =
          processingEnv.getFiler().createSourceFile(clsQualifiedName, element);
      OutputStream ios = sourceFile.openOutputStream();
      OutputStreamWriter writer = new OutputStreamWriter(ios, "UTF-8");
      BufferedWriter w = new BufferedWriter(writer);
      try {
        w.append("package ").append(pkg.getQualifiedName()).append(";");
        w.newLine();
        w.append("public class ").append(clsSimpleName).append(" { }");
      } finally {
        w.close();
        writer.close();
        ios.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    } 
  }
  return true;
}
 
Example 14
Source File: JavaInMemoryFileManagerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteContent() throws Exception {
  JavaFileObject fileObject =
      inMemoryFileManager.getJavaFileForOutput(
          StandardLocation.CLASS_OUTPUT, "JavaFileParser", JavaFileObject.Kind.CLASS, null);

  OutputStream stream = fileObject.openOutputStream();
  stream.write("Hello World!".getBytes());
  stream.close();

  TestJar jar = writeToJar();
  List<String> entries = jar.getEntriesContent();
  assertEquals(3, entries.size());
  assertEquals("Hello World!", entries.get(2));
}
 
Example 15
Source File: SPIProcessor.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
    output.close();
}
 
Example 16
Source File: SPIProcessor.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void writeBindingFile() throws Exception {
    JavaFileObject sourceFile = filer.createSourceFile(BINDING_FILENAME);
    Map<String, Object> model = new HashMap<>();
    model.put("extensions", discoveredBindings);

    OutputStream output = sourceFile.openOutputStream();
    new TemplateProcessor().process(BINDING_TEMPLATE, model, output);

    output.flush();
    output.close();
}
 
Example 17
Source File: OutputFileManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testInvalidClassName() throws IOException {
    assertNotNull(fm);
    final JavaFileObject fobj = fm.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, "org.netbeans.java.<any>", JavaFileObject.Kind.CLASS, null);
    assertNotNull(fobj);
    try (OutputStream out = fobj.openOutputStream()) {
        out.write(new byte[]{(byte)0xca,(byte)0xfe,(byte)0xba, (byte) 0xbe});
    }
    wbTx.commit();
    FileUtil.refreshFor(FileUtil.toFile(outCp.getRoots()[0]));
    assertNull(outCp.findResource("org/netbeans/java/<any>.sig"));
}
 
Example 18
Source File: OutputFileManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testValidClassName() throws IOException {
    assertNotNull(fm);
    final JavaFileObject fobj = fm.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, "org.netbeans.java.Test", JavaFileObject.Kind.CLASS, null);
    assertNotNull(fobj);
    try (OutputStream out = fobj.openOutputStream()) {
        out.write(new byte[]{(byte)0xca,(byte)0xfe,(byte)0xba, (byte) 0xbe});
    }
    wbTx.commit();
    FileUtil.refreshFor(FileUtil.toFile(outCp.getRoots()[0]));
    assertNotNull(outCp.findResource("org/netbeans/java/Test.sig"));
}
 
Example 19
Source File: ClassWriter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Emit a class file for a given class.
 *  @param c      The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
    throws IOException, PoolOverflow, StringOverflow
{
    String name = (c.owner.kind == MDL ? c.name : c.flatname).toString();
    Location outLocn;
    if (multiModuleMode) {
        ModuleSymbol msym = c.owner.kind == MDL ? (ModuleSymbol) c.owner : c.packge().modle;
        outLocn = fileManager.getLocationForModule(CLASS_OUTPUT, msym.name.toString());
    } else {
        outLocn = CLASS_OUTPUT;
    }
    JavaFileObject outFile
        = fileManager.getJavaFileForOutput(outLocn,
                                           name,
                                           JavaFileObject.Kind.CLASS,
                                           c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propagating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
Example 20
Source File: VanillaCompileWorker.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void copyFile(FileObject updatedFile, JavaFileObject target) throws IOException {
    try (InputStream ins = updatedFile.getInputStream();
         OutputStream out = target.openOutputStream()) {
        FileUtil.copy(ins, out);
    }
}