当前位置: 首页>>代码示例>>Java>>正文


Java StandardJavaFileManager.close方法代码示例

本文整理汇总了Java中javax.tools.StandardJavaFileManager.close方法的典型用法代码示例。如果您正苦于以下问题:Java StandardJavaFileManager.close方法的具体用法?Java StandardJavaFileManager.close怎么用?Java StandardJavaFileManager.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.tools.StandardJavaFileManager的用法示例。


在下文中一共展示了StandardJavaFileManager.close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test

import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
@Test(dataProvider = "versions")
public void test(String version, int expected) throws Throwable {
    StandardJavaFileManager jfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
    jfm.setLocation(jloc, List.of(new File("multi-release.jar")));

    if (version.length() > 0) {
        jfm.handleOption("--multi-release", List.of(version).iterator());
    }

    CustomClassLoader cldr = new CustomClassLoader(jfm);
    Class<?> versionClass = cldr.loadClass("version.Version");
    MethodType mt = MethodType.methodType(int.class);
    MethodHandle mh = MethodHandles.lookup().findVirtual(versionClass, "getVersion", mt);
    int v = (int)mh.invoke(versionClass.newInstance());
    Assert.assertEquals(v, expected);

    jfm.close();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:MultiReleaseJarAwareSJFM.java

示例2: testFile

import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
@Test(dataProvider = "crawler")
public void testFile(String fileName) throws IOException {
    File file = getSourceFile(fileName);
    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    boolean success = true;
    StringWriter writer = new StringWriter();
    writer.write("Testing : " + file.toString() + "\n");
    String text = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file);
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    Trees trees = Trees.instance(task);
    SourcePositions sp = trees.getSourcePositions();

    for (CompilationUnitTree cut : asts) {
        for (ImportTree imp : cut.getImports()) {
            success &= testStatement(writer, sp, text, cut, imp);
        }
        for (Tree decl : cut.getTypeDecls()) {
            success &= testStatement(writer, sp, text, cut, decl);
            if (decl instanceof ClassTree) {
                ClassTree ct = (ClassTree) decl;
                for (Tree mem : ct.getMembers()) {
                    if (mem instanceof MethodTree) {
                        MethodTree mt = (MethodTree) mem;
                        BlockTree bt = mt.getBody();
                        // No abstract methods or constructors
                        if (bt != null && mt.getReturnType() != null) {
                            // The modifiers synchronized, abstract, and default are not allowed on
                            // top-level declarations and are errors.
                            Set<Modifier> modifier = mt.getModifiers().getFlags();
                            if (!modifier.contains(Modifier.ABSTRACT)
                                    && !modifier.contains(Modifier.SYNCHRONIZED)
                                    && !modifier.contains(Modifier.DEFAULT)) {
                                success &= testStatement(writer, sp, text, cut, mt);
                            }
                            testBlock(writer, sp, text, cut, bt);
                        }
                    }
                }
            }
        }
    }
    fileManager.close();
    if (!success) {
        throw new AssertionError(writer.toString());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:50,代码来源:CompletenessStressTest.java

示例3: run

import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
void run() throws Exception {
    ToolBox tb = new ToolBox();
    Path jar = createJar(tb);

    Path src = Paths.get("src");
    tb.writeJavaFiles(src, "class C { p1.C1 c1; }");

    JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    PrintWriter out = new PrintWriter(System.err, true);
    StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    List<String> options = Arrays.asList("-classpath", jar.toString(), "-proc:none");
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(src.resolve("C.java"));
    com.sun.source.util.JavacTask task =
            (com.sun.source.util.JavacTask) comp.getTask(out, fm, null, options, null, files);
    task.parse();

    Elements elems = task.getElements();

    try {
        // Use  p1, p1.C1 as a control to verify normal behavior
        PackageElement p1 = elems.getPackageElement("p1");
        TypeElement p1C1 = elems.getTypeElement("p1.C1");
        System.err.println("p1: " + p1 + ";  p1C1: " + p1C1);
        if (p1C1 == null) {
            throw new Exception("p1.C1 not found");
        }

        // Now repeat for p2, p2.C2, closing the file manager early
        PackageElement p2 = elems.getPackageElement("p2");
        System.err.println("closing file manager");
        fm.close();
        TypeElement p2C2 = elems.getTypeElement("p2.C2");
        System.err.println("p2: " + p2 + ";  p2C2: " + p2C2);
        if (p2C2 != null) {
            throw new Exception("p2.C2 found unexpectedly");
        }
    } catch (ClosedFileSystemException e) {
        throw new Exception("unexpected exception thrown", e);
    }

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:42,代码来源:FileSystemClosedTest.java


注:本文中的javax.tools.StandardJavaFileManager.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。