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

The following examples show how to use javax.tools.JavaFileObject#delete() . 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: JavaFile.java    From javapoet with Apache License 2.0 6 votes vote down vote up
/** Writes this to {@code filer}. */
public void writeTo(Filer filer) throws IOException {
  String fileName = packageName.isEmpty()
      ? typeSpec.name
      : packageName + "." + typeSpec.name;
  List<Element> originatingElements = typeSpec.originatingElements;
  JavaFileObject filerSourceFile = filer.createSourceFile(fileName,
      originatingElements.toArray(new Element[originatingElements.size()]));
  try (Writer writer = filerSourceFile.openWriter()) {
    writeTo(writer);
  } catch (Exception e) {
    try {
      filerSourceFile.delete();
    } catch (Exception ignored) {
    }
    throw e;
  }
}
 
Example 3
Source File: JavaWriter.java    From dagger2-sample with Apache License 2.0 6 votes vote down vote up
public void file(Filer filer, CharSequence name,  Iterable<? extends Element> originatingElements)
    throws IOException {
  JavaFileObject sourceFile = filer.createSourceFile(name,
      Iterables.toArray(originatingElements, Element.class));
  Closer closer = Closer.create();
  try {
    write(closer.register(sourceFile.openWriter()));
  } catch (Exception e) {
    try {
      sourceFile.delete();
    } catch (Exception e2) {
      // couldn't delete the file
    }
    throw closer.rethrow(e);
  } finally {
    closer.close();
  }
}
 
Example 4
Source File: JavaFile.java    From JReFrameworker with MIT License 6 votes vote down vote up
/** Writes this to {@code filer}. */
public void writeTo(Filer filer) throws IOException {
  String fileName = packageName.isEmpty()
      ? typeSpec.name
      : packageName + "." + typeSpec.name;
  List<Element> originatingElements = typeSpec.originatingElements;
  JavaFileObject filerSourceFile = filer.createSourceFile(fileName,
      originatingElements.toArray(new Element[originatingElements.size()]));
  try (Writer writer = filerSourceFile.openWriter()) {
    writeTo(writer);
  } catch (Exception e) {
    try {
      filerSourceFile.delete();
    } catch (Exception ignored) {
    }
    throw e;
  }
}
 
Example 5
Source File: JavaFile.java    From JReFrameworker with MIT License 6 votes vote down vote up
/** Writes this to {@code filer}. */
public void writeTo(Filer filer) throws IOException {
  String fileName = packageName.isEmpty()
      ? typeSpec.name
      : packageName + "." + typeSpec.name;
  List<Element> originatingElements = typeSpec.originatingElements;
  JavaFileObject filerSourceFile = filer.createSourceFile(fileName,
      originatingElements.toArray(new Element[originatingElements.size()]));
  try (Writer writer = filerSourceFile.openWriter()) {
    writeTo(writer);
  } catch (Exception e) {
    try {
      filerSourceFile.delete();
    } catch (Exception ignored) {
    }
    throw e;
  }
}
 
Example 6
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 7
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 8
Source File: ClassWriter.java    From hottub 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 9
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 10
Source File: ClassWriter.java    From openjdk-jdk8u-backup 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 11
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 12
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 13
Source File: CommonConfigurationClassWriter.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
private void generate(List<ConnectorAttribute> attributes, String packageName,
        String className, String simpleName, String connector) throws IOException {
    JavaFileObject file = environment.getFiler().createSourceFile(className);
    file.delete();
    try (PrintWriter out = new PrintWriter(file.openWriter())) {
        writePackageDeclaration(packageName, out);

        writeImportStatements(out);
        out.println("import " + ConfigProvider.class.getName() + ";");
        out.println("import " + ConnectorFactory.class.getName() + ";");

        writeClassDeclaration(simpleName, connector, out);
        writeConstructorAndConfigAccessor(simpleName, out);
        attributes.forEach(ca -> generateGetterForAttribute(ca, connector, out));
        writeValidateMethod(attributes, out);

        out.println("}"); // End of class.
    }
}
 
Example 14
Source File: ClassWriter.java    From TencentKona-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 15
Source File: ClassWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 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 16
Source File: StateProcessor.java    From android-state with Eclipse Public License 1.0 5 votes vote down vote up
private boolean writeJavaFile(JavaFile javaFile) {
    StringBuilder builder = new StringBuilder();

    JavaFileObject filerSourceFile = null;

    try {
        builder.append(LICENSE_HEADER);
        javaFile.writeTo(builder);

        String fileName = javaFile.packageName.isEmpty() ? javaFile.typeSpec.name : javaFile.packageName + "." + javaFile.typeSpec.name;
        List<Element> originatingElements = javaFile.typeSpec.originatingElements;
        filerSourceFile = mFiler.createSourceFile(fileName, originatingElements.toArray(new Element[0]));

        try (Writer writer = filerSourceFile.openWriter()) {
            writer.write(builder.toString());
        }
        return true;

    } catch (Exception e) {
        mMessager.printMessage(Diagnostic.Kind.ERROR, "Couldn't generate classes " + javaFile.packageName + '.' + javaFile.typeSpec.name);
        e.printStackTrace();

        if (filerSourceFile != null) {
            filerSourceFile.delete();
        }

        return false;
    }
}
 
Example 17
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 18
Source File: ConfigurationClassWriter.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private void generate(List<ConnectorAttribute> attributes, String packageName,
        String configClassName, String configSimpleClassName, String parentConfigSimpleClassName,
        String direction, String connector) throws IOException {
    JavaFileObject file = environment.getFiler().createSourceFile(configClassName);
    file.delete();
    try (PrintWriter out = new PrintWriter(file.openWriter())) {
        writePackageDeclaration(packageName, out);
        writeImportStatements(out);
        writeClassDeclaration(configSimpleClassName, direction, connector, out, parentConfigSimpleClassName);
        writeConstructor(configSimpleClassName, out);
        attributes.forEach(ca -> generateGetterForAttribute(ca, connector, out));
        writeValidateMethod(attributes, out);
        out.println("}"); // End of class.
    }
}
 
Example 19
Source File: TestMRRJobsDAGApi.java    From incubator-tez with Apache License 2.0 5 votes vote down vote up
private static void createTestJar(OutputStream outStream, String dummyClassName)
    throws URISyntaxException, IOException {
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  JavaFileObject srcFileObject = new SimpleJavaFileObjectImpl(
      URI.create("string:///" + dummyClassName + Kind.SOURCE.extension), Kind.SOURCE);
  StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
  compiler.getTask(null, fileManager, null, null, null, Collections.singletonList(srcFileObject))
      .call();

  JavaFileObject javaFileObject = fileManager.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT,
      dummyClassName, Kind.CLASS, null);

  File classFile = new File(dummyClassName + Kind.CLASS.extension);

  JarOutputStream jarOutputStream = new JarOutputStream(outStream);
  JarEntry jarEntry = new JarEntry(classFile.getName());
  jarEntry.setTime(classFile.lastModified());
  jarOutputStream.putNextEntry(jarEntry);

  InputStream in = javaFileObject.openInputStream();
  byte buffer[] = new byte[4096];
  while (true) {
    int nRead = in.read(buffer, 0, buffer.length);
    if (nRead <= 0)
      break;
    jarOutputStream.write(buffer, 0, nRead);
  }
  in.close();
  jarOutputStream.close();
  javaFileObject.delete();
}
 
Example 20
Source File: TestMRRJobsDAGApi.java    From tez with Apache License 2.0 5 votes vote down vote up
private static void createTestJar(OutputStream outStream, String dummyClassName)
    throws URISyntaxException, IOException {
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  JavaFileObject srcFileObject = new SimpleJavaFileObjectImpl(
      URI.create("string:///" + dummyClassName + Kind.SOURCE.extension), Kind.SOURCE);
  StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
  compiler.getTask(null, fileManager, null, null, null, Collections.singletonList(srcFileObject))
      .call();

  JavaFileObject javaFileObject = fileManager.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT,
      dummyClassName, Kind.CLASS, null);

  File classFile = new File(dummyClassName + Kind.CLASS.extension);

  JarOutputStream jarOutputStream = new JarOutputStream(outStream);
  JarEntry jarEntry = new JarEntry(classFile.getName());
  jarEntry.setTime(classFile.lastModified());
  jarOutputStream.putNextEntry(jarEntry);

  InputStream in = javaFileObject.openInputStream();
  byte buffer[] = new byte[4096];
  while (true) {
    int nRead = in.read(buffer, 0, buffer.length);
    if (nRead <= 0)
      break;
    jarOutputStream.write(buffer, 0, nRead);
  }
  in.close();
  jarOutputStream.close();
  javaFileObject.delete();
}