本文整理汇总了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();
}
示例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());
}
}
示例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);
}
}