javax.tools.StandardJavaFileManager#getJavaFileForInput ( )源码实例Demo

下面列出了javax.tools.StandardJavaFileManager#getJavaFileForInput ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: TencentKona-8   文件: GenStubs.java
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);
}
 
源代码2 项目: jdk8u60   文件: GenStubs.java
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);
}
 
源代码3 项目: openjdk-jdk8u   文件: GenStubs.java
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);
}
 
源代码4 项目: openjdk-jdk8u-backup   文件: GenStubs.java
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);
}
 
源代码5 项目: openjdk-jdk9   文件: GenStubs.java
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);
}
 
源代码6 项目: hottub   文件: GenStubs.java
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);
}
 
源代码7 项目: openjdk-8-source   文件: GenStubs.java
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);
}
 
源代码8 项目: openjdk-8   文件: GenStubs.java
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);
}
 
源代码9 项目: RetroFacebook   文件: TypeSimplifierTest.java
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);
}
 
源代码10 项目: RetroFacebook   文件: TypeSimplifierTest.java
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);
}
 
源代码11 项目: SimpleWeibo   文件: TypeSimplifierTest.java
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);
}