Java Code Examples for javax.tools.StandardJavaFileManager#getJavaFileForInput()

The following examples show how to use javax.tools.StandardJavaFileManager#getJavaFileForInput() . These examples are extracted from open source projects. 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
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 2
Source Project: jdk8u60   File: GenStubs.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 3
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 4
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 5
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 6
Source Project: hottub   File: GenStubs.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 7
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 8
Source Project: openjdk-8   File: GenStubs.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c: classes) {
            JavaFileObject fo = fm.getJavaFileForInput(
                    StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }

        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree: trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }

    return (errors == 0);
}
 
Example 9
private void doTestTypeSimplifier(
    AbstractTestProcessor testProcessor, File tmpDir, ImmutableMap<String, String> classToSource)
    throws IOException {
  JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
  DiagnosticCollector<JavaFileObject> diagnosticCollector =
      new DiagnosticCollector<JavaFileObject>();
  StandardJavaFileManager fileManager =
      javac.getStandardFileManager(diagnosticCollector, null, null);

  StringWriter compilerOut = new StringWriter();

  List<String> options = ImmutableList.of(
      "-sourcepath", tmpDir.getPath(),
      "-d", tmpDir.getPath(),
      "-Xlint");
  javac.getTask(compilerOut, fileManager, diagnosticCollector, options, null, null);
  // This doesn't compile anything but communicates the paths to the JavaFileManager.

  ImmutableList.Builder<JavaFileObject> javaFilesBuilder = ImmutableList.builder();
  for (String className : classToSource.keySet()) {
    JavaFileObject sourceFile = fileManager.getJavaFileForInput(
        StandardLocation.SOURCE_PATH, className, Kind.SOURCE);
    javaFilesBuilder.add(sourceFile);
  }

  // Compile the empty source file to trigger the annotation processor.
  // (Annotation processors are somewhat misnamed because they run even on classes with no
  // annotations.)
  JavaCompiler.CompilationTask javacTask = javac.getTask(
      compilerOut, fileManager, diagnosticCollector, options,
      classToSource.keySet(), javaFilesBuilder.build());
  javacTask.setProcessors(ImmutableList.of(testProcessor));
  javacTask.call();
  List<Diagnostic<? extends JavaFileObject>> diagnostics =
      new ArrayList<Diagnostic<? extends JavaFileObject>>(diagnosticCollector.getDiagnostics());

  // In the ErrorTestProcessor case, the code being compiled contains a deliberate reference to an
  // undefined type, so that we can capture an instance of ErrorType. (Synthesizing one ourselves
  // leads to ClassCastException inside javac.) So remove any errors for that from the output, and
  // only fail if there were other errors.
  for (Iterator<Diagnostic<? extends JavaFileObject>> it = diagnostics.iterator();
       it.hasNext(); ) {
    Diagnostic<? extends JavaFileObject> diagnostic = it.next();
    if (diagnostic.getSource() != null
        && diagnostic.getSource().getName().contains("ExtendsUndefinedType")) {
      it.remove();
    }
  }
  // In the ErrorTestProcessor case, compilerOut.toString() will include the error for
  // ExtendsUndefinedType, which can safely be ignored, as well as stack traces for any failing
  // assertion.
  assertEquals(compilerOut.toString() + diagnosticCollector.getDiagnostics(),
      ImmutableList.of(), diagnostics);
}
 
Example 10
private void doTestTypeSimplifier(
    AbstractTestProcessor testProcessor, File tmpDir, ImmutableMap<String, String> classToSource)
    throws IOException {
  JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
  DiagnosticCollector<JavaFileObject> diagnosticCollector =
      new DiagnosticCollector<JavaFileObject>();
  StandardJavaFileManager fileManager =
      javac.getStandardFileManager(diagnosticCollector, null, null);

  StringWriter compilerOut = new StringWriter();

  List<String> options = ImmutableList.of(
      "-sourcepath", tmpDir.getPath(),
      "-d", tmpDir.getPath(),
      "-Xlint");
  javac.getTask(compilerOut, fileManager, diagnosticCollector, options, null, null);
  // This doesn't compile anything but communicates the paths to the JavaFileManager.

  ImmutableList.Builder<JavaFileObject> javaFilesBuilder = ImmutableList.builder();
  for (String className : classToSource.keySet()) {
    JavaFileObject sourceFile = fileManager.getJavaFileForInput(
        StandardLocation.SOURCE_PATH, className, Kind.SOURCE);
    javaFilesBuilder.add(sourceFile);
  }

  // Compile the empty source file to trigger the annotation processor.
  // (Annotation processors are somewhat misnamed because they run even on classes with no
  // annotations.)
  JavaCompiler.CompilationTask javacTask = javac.getTask(
      compilerOut, fileManager, diagnosticCollector, options,
      classToSource.keySet(), javaFilesBuilder.build());
  javacTask.setProcessors(ImmutableList.of(testProcessor));
  javacTask.call();
  List<Diagnostic<? extends JavaFileObject>> diagnostics =
      new ArrayList<Diagnostic<? extends JavaFileObject>>(diagnosticCollector.getDiagnostics());

  // In the ErrorTestProcessor case, the code being compiled contains a deliberate reference to an
  // undefined type, so that we can capture an instance of ErrorType. (Synthesizing one ourselves
  // leads to ClassCastException inside javac.) So remove any errors for that from the output, and
  // only fail if there were other errors.
  for (Iterator<Diagnostic<? extends JavaFileObject>> it = diagnostics.iterator();
       it.hasNext(); ) {
    Diagnostic<? extends JavaFileObject> diagnostic = it.next();
    if (diagnostic.getSource() != null
        && diagnostic.getSource().getName().contains("ExtendsUndefinedType")) {
      it.remove();
    }
  }
  // In the ErrorTestProcessor case, compilerOut.toString() will include the error for
  // ExtendsUndefinedType, which can safely be ignored, as well as stack traces for any failing
  // assertion.
  assertEquals(compilerOut.toString() + diagnosticCollector.getDiagnostics(),
      ImmutableList.of(), diagnostics);
}
 
Example 11
Source Project: SimpleWeibo   File: TypeSimplifierTest.java    License: Apache License 2.0 4 votes vote down vote up
private void doTestTypeSimplifier(
    AbstractTestProcessor testProcessor, File tmpDir, ImmutableMap<String, String> classToSource)
    throws IOException {
  JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
  DiagnosticCollector<JavaFileObject> diagnosticCollector =
      new DiagnosticCollector<JavaFileObject>();
  StandardJavaFileManager fileManager =
      javac.getStandardFileManager(diagnosticCollector, null, null);

  StringWriter compilerOut = new StringWriter();

  List<String> options = ImmutableList.of(
      "-sourcepath", tmpDir.getPath(),
      "-d", tmpDir.getPath(),
      "-Xlint");
  javac.getTask(compilerOut, fileManager, diagnosticCollector, options, null, null);
  // This doesn't compile anything but communicates the paths to the JavaFileManager.

  ImmutableList.Builder<JavaFileObject> javaFilesBuilder = ImmutableList.builder();
  for (String className : classToSource.keySet()) {
    JavaFileObject sourceFile = fileManager.getJavaFileForInput(
        StandardLocation.SOURCE_PATH, className, Kind.SOURCE);
    javaFilesBuilder.add(sourceFile);
  }

  // Compile the empty source file to trigger the annotation processor.
  // (Annotation processors are somewhat misnamed because they run even on classes with no
  // annotations.)
  JavaCompiler.CompilationTask javacTask = javac.getTask(
      compilerOut, fileManager, diagnosticCollector, options,
      classToSource.keySet(), javaFilesBuilder.build());
  javacTask.setProcessors(ImmutableList.of(testProcessor));
  javacTask.call();
  List<Diagnostic<? extends JavaFileObject>> diagnostics =
      new ArrayList<Diagnostic<? extends JavaFileObject>>(diagnosticCollector.getDiagnostics());

  // In the ErrorTestProcessor case, the code being compiled contains a deliberate reference to an
  // undefined type, so that we can capture an instance of ErrorType. (Synthesizing one ourselves
  // leads to ClassCastException inside javac.) So remove any errors for that from the output, and
  // only fail if there were other errors.
  for (Iterator<Diagnostic<? extends JavaFileObject>> it = diagnostics.iterator();
       it.hasNext(); ) {
    Diagnostic<? extends JavaFileObject> diagnostic = it.next();
    if (diagnostic.getSource() != null
        && diagnostic.getSource().getName().contains("ExtendsUndefinedType")) {
      it.remove();
    }
  }
  // In the ErrorTestProcessor case, compilerOut.toString() will include the error for
  // ExtendsUndefinedType, which can safely be ignored, as well as stack traces for any failing
  // assertion.
  assertEquals(compilerOut.toString() + diagnosticCollector.getDiagnostics(),
      ImmutableList.of(), diagnostics);
}