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

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

源代码1 项目: hottub   文件: DocTreePathScannerTest.java
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

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

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
源代码2 项目: openjdk-jdk8u   文件: DocTreePathScannerTest.java
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

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

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
源代码3 项目: TencentKona-8   文件: T6410706.java
public static void main(String... args) throws IOException {
    String testSrc = System.getProperty("test.src", ".");
    String testClasses = System.getProperty("test.classes", ".");
    JavacTool tool = JavacTool.create();
    MyDiagListener dl = new MyDiagListener();
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(testClasses)));
    Iterable<? extends JavaFileObject> files =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6410706.class.getName()+".java")));
    JavacTask task = tool.getTask(null, fm, dl, null, null, files);
    task.parse();
    task.analyze();
    task.generate();

    // expect 2 notes:
    // Note: T6410706.java uses or overrides a deprecated API.
    // Note: Recompile with -Xlint:deprecation for details.

    if (dl.notes != 2)
        throw new AssertionError(dl.notes + " notes given");
}
 
源代码4 项目: openjdk-8   文件: DocTreePathScannerTest.java
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

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

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
源代码5 项目: jdk8u60   文件: DocTreePathScannerTest.java
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

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

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
源代码6 项目: jdk8u60   文件: ArrayCreationTree.java
public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayCreationTree.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();

    Scanner s = new Scanner();
    for (CompilationUnitTree t: trees)
        s.scan(t, null);

}
 
源代码7 项目: jdk8u60   文件: AnnotatedArrayOrder.java
public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "AnnotatedArrayOrder.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();

    Scanner s = new Scanner();
    for (CompilationUnitTree t: trees)
        s.scan(t, null);

}
 
源代码8 项目: openjdk-8   文件: ArrayPositionConsistency.java
public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayPositionConsistency.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();

    Scanner s = new Scanner();
    for (CompilationUnitTree t: trees)
        s.scan(t, null);

}
 
源代码9 项目: TencentKona-8   文件: T6345974.java
public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();

    Scanner s = new Scanner();
    for (CompilationUnitTree t: trees)
        s.scan(t, null);
}
 
源代码10 项目: TencentKona-8   文件: SimpleDocTreeVisitorTest.java
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

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

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class);
    DeclScanner ds = new DeclScanner(trees, found);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    for (DocTree.Kind k: DocTree.Kind.values()) {
        if (!found.contains(k) && k != DocTree.Kind.OTHER)
            error("not found: " + k);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
源代码11 项目: openjdk-jdk8u   文件: SchemaGenerator.java
public static boolean compile(String[] args, File episode) throws Exception {

            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
            JavacOptions options = JavacOptions.parse(compiler, fileManager, args);
            List<String> unrecognizedOptions = options.getUnrecognizedOptions();
            if (!unrecognizedOptions.isEmpty())
                Logger.getLogger(SchemaGenerator.class.getName()).log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions);
            Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles());
            JavaCompiler.CompilationTask task = compiler.getTask(
                    null,
                    fileManager,
                    diagnostics,
                    options.getRecognizedOptions(),
                    options.getClassNames(),
                    compilationUnits);
            com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator();
            if (episode != null)
                r.setEpisodeFile(episode);
            task.setProcessors(Collections.singleton(r));
            boolean res = task.call();
            //Print messages generated by compiler
            for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
                 System.err.println(d.toString());
            }
            return res;
        }
 
源代码12 项目: openjdk-jdk8u   文件: Example.java
@Override
boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
    if (out != null && keys != null)
        throw new IllegalArgumentException();

    if (verbose)
        System.err.println("run_jsr199: " + opts + " " + files);

    DiagnosticCollector<JavaFileObject> dc = null;
    if (keys != null)
        dc = new DiagnosticCollector<JavaFileObject>();

    if (raw) {
        List<String> newOpts = new ArrayList<String>();
        newOpts.add("-XDrawDiagnostics");
        newOpts.addAll(opts);
        opts = newOpts;
    }

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
    if (fmOpts != null)
        fm = new FileManager(fm, fmOpts);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    CompilationTask t = c.getTask(out, fm, dc, opts, null, fos);
    Boolean ok = t.call();

    if (keys != null) {
        for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
            scanForKeys(unwrap(d), keys);
        }
    }

    return ok;
}
 
源代码13 项目: TencentKona-8   文件: SchemaGenerator.java
public static boolean compile(String[] args, File episode) throws Exception {

            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
            JavacOptions options = JavacOptions.parse(compiler, fileManager, args);
            List<String> unrecognizedOptions = options.getUnrecognizedOptions();
            if (!unrecognizedOptions.isEmpty())
                Logger.getLogger(SchemaGenerator.class.getName()).log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions);
            Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles());
            JavaCompiler.CompilationTask task = compiler.getTask(
                    null,
                    fileManager,
                    diagnostics,
                    options.getRecognizedOptions(),
                    options.getClassNames(),
                    compilationUnits);
            com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator();
            if (episode != null)
                r.setEpisodeFile(episode);
            task.setProcessors(Collections.singleton(r));
            boolean res = task.call();
            //Print messages generated by compiler
            for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
                 System.err.println(d.toString());
            }
            return res;
        }
 
源代码14 项目: openjdk-8   文件: Example.java
@Override
boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
    if (out != null && keys != null)
        throw new IllegalArgumentException();

    if (verbose)
        System.err.println("run_jsr199: " + opts + " " + files);

    DiagnosticCollector<JavaFileObject> dc = null;
    if (keys != null)
        dc = new DiagnosticCollector<JavaFileObject>();

    if (raw) {
        List<String> newOpts = new ArrayList<String>();
        newOpts.add("-XDrawDiagnostics");
        newOpts.addAll(opts);
        opts = newOpts;
    }

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
    if (fmOpts != null)
        fm = new FileManager(fm, fmOpts);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    CompilationTask t = c.getTask(out, fm, dc, opts, null, fos);
    Boolean ok = t.call();

    if (keys != null) {
        for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
            scanForKeys(unwrap(d), keys);
        }
    }

    return ok;
}
 
源代码15 项目: neembuu-uploader   文件: NUCompiler.java
/**
 * Compile all the given files in the given build directory.
 *
 * @param files The array of files to compiles.
 * @param buildDirectory The build directory in which put all the compiled
 * files.
 * @throws FileNotFoundException
 * @throws IOException
 */
private void compileFiles(File[] files, File buildDirectory) throws FileNotFoundException, IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    buildDirectory.mkdir();

    /**/
    List<String> optionList = new ArrayList<String>();
    // set compiler's classpath to be same as the runtime's
    //optionList.addAll(Arrays.asList("-classpath", "C:\\neembuuuploader\\modules\\libs\\jsoup-1.7.2.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\commons-codec-1.6.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\commons-logging-1.1.1.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpclient-4.2.5.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpclient-cache-4.2.5.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpcore-4.2.4.jar;C:\\neembuuuploader\\modules\\libs\\ApacheHttpComponent-4.2.5\\httpmime-4.2.5.jar;C:\\neembuuuploader\\modules\\libs\\json-java.jar;C:\\neembuuuploader\\modules\\neembuu-uploader-utils\\build\\classes;C:\\neembuuuploader\\modules\\neembuu-uploader-api\\build\\classes;C:\\neembuuuploader\\modules\\neembuu-uploader-interfaces-abstractimpl\\build\\classes;C:\\neembuuuploader\\modules\\libs\\neembuu-now-api-ui.jar;C:\\neembuuuploader\\modules\\libs\\neembuu-release1-ui-mc.jar;C:\\neembuuuploader\\modules\\neembuu-uploader-uploaders\\build\\classes;C:\\neembuuuploader\\modules\\NeembuuUploader\\build\\classes"));
    optionList.addAll(Arrays.asList("-classpath", classPath));
    optionList.addAll(Arrays.asList("-d", buildDirectory.getAbsolutePath()));

    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits
            = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files));

    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

    JavaCompiler.CompilationTask task = compiler.getTask(null, null, diagnostics, optionList, null, compilationUnits);
    boolean result = task.call();

    if (result) {
        System.out.println("Compilation was successful");
    } else {
        System.out.println("Compilation failed");

        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            System.out.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic);
        }
    }
}
 
源代码16 项目: openjdk-jdk8u-backup   文件: T6345974.java
public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();

    Scanner s = new Scanner();
    for (CompilationUnitTree t: trees)
        s.scan(t, null);
}
 
private static Iterable<? extends JavaFileObject> getCompilationUnitOfClass(StandardJavaFileManager fileManager,
    String classToCompile) throws IOException {
  ClassPathResource resource = new ClassPathResource(classToCompile + ".java");
  return fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(resource.getFile()));
}
 
源代码18 项目: auto   文件: CompileWithEclipseTest.java
private void compileWithEclipse(String version, Predicate<File> predicate) throws IOException {
  File sourceRootFile = new File(SOURCE_ROOT);
  File javaDir = new File(sourceRootFile, "src/main/java");
  File javatestsDir = new File(sourceRootFile, "src/test/java");
  Set<File> sources =
      new ImmutableSet.Builder<File>()
          .addAll(filesUnderDirectory(javaDir, predicate))
          .addAll(filesUnderDirectory(javatestsDir, predicate))
          .build();
  assertThat(sources).isNotEmpty();
  JavaCompiler compiler = new EclipseCompiler();
  StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
  // This hack is only needed in a Google-internal Java 8 environment where symbolic links make it
  // hard for ecj to find the boot class path. Elsewhere it is unnecessary but harmless. Notably,
  // on Java 9+ there is no rt.jar. There, fileManager.getLocation(PLATFORM_CLASS_PATH) returns
  // null, because the relevant classes are in modules inside
  // fileManager.getLocation(SYSTEM_MODULES).
  File rtJar = new File(JAVA_HOME.value() + "/lib/rt.jar");
  if (rtJar.exists()) {
    List<File> bootClassPath = ImmutableList.<File>builder()
        .add(rtJar)
        .addAll(fileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH))
        .build();
    fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
  }
  Iterable<? extends JavaFileObject> sourceFileObjects =
      fileManager.getJavaFileObjectsFromFiles(sources);
  String outputDir = tmp.getRoot().toString();
  ImmutableList<String> options =
      ImmutableList.of("-d", outputDir, "-s", outputDir, "-source", version, "-target", version);
  JavaCompiler.CompilationTask task =
      compiler.getTask(null, fileManager, null, options, null, sourceFileObjects);
  // Explicitly supply an empty list of extensions for AutoValueProcessor, because otherwise this
  // test will pick up a test one and get confused.
  AutoValueProcessor autoValueProcessor = new AutoValueProcessor(ImmutableList.of());
  ImmutableList<? extends Processor> processors =
      ImmutableList.of(
          autoValueProcessor, new AutoOneOfProcessor(), new AutoAnnotationProcessor());
  task.setProcessors(processors);
  assertWithMessage("Compilation should succeed").that(task.call()).isTrue();
}
 
源代码19 项目: openjdk-jdk8u-backup   文件: TestJavacTask.java
static JavacTaskImpl getTask(File... file) {
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
    return (JavacTaskImpl)compiler.getTask(null, fm, null, null, null, files);
}
 
源代码20 项目: jdk8u60   文件: TestJavacTask.java
static JavacTaskImpl getTask(File... file) {
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
    return (JavacTaskImpl)compiler.getTask(null, fm, null, null, null, files);
}