本文整理汇总了Java中javax.tools.StandardJavaFileManager类的典型用法代码示例。如果您正苦于以下问题:Java StandardJavaFileManager类的具体用法?Java StandardJavaFileManager怎么用?Java StandardJavaFileManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StandardJavaFileManager类属于javax.tools包,在下文中一共展示了StandardJavaFileManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBadFileObject
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
/**
* Verify bad file object is handled correctly.
*/
@Test
public void testBadFileObject() throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File srcFile = new File(testSrc, "pkg/C.class"); // unacceptable file kind
DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File outDir = getOutDir();
fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(srcFile);
try {
DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
error("getTask succeeded, no exception thrown");
} catch (IllegalArgumentException e) {
System.err.println("exception caught as expected: " + e);
}
}
}
示例2: testNull
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
/**
* Verify null is handled correctly.
*/
@Test
public void testNull() throws Exception {
DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File outDir = getOutDir();
fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
Iterable<? extends JavaFileObject> files = Arrays.asList((JavaFileObject) null);
try {
DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
error("getTask succeeded, no exception thrown");
} catch (NullPointerException e) {
System.err.println("exception caught as expected: " + e);
}
}
}
示例3: testDoclet
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
/**
* Verify that an alternate doclet can be specified.
*
* There is no standard interface or superclass for a doclet;
* the only requirement is that it provides static methods that
* can be invoked via reflection. So, for now, the doclet is
* specified as a class.
* Because we cannot create and use a unique instance of the class,
* we verify that the doclet has been called by having it record
* (in a static field!) the comment from the last time it was invoked,
* which is randomly generated each time the test is run.
*/
@Test
public void testDoclet() throws Exception {
Random r = new Random();
int key = r.nextInt();
JavaFileObject srcFile = createSimpleJavaFileObject(
"pkg/C",
"package pkg; /** " + key + "*/ public class C { }");
DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File outDir = getOutDir();
fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files);
if (t.call()) {
System.err.println("task succeeded");
if (TestDoclet.lastCaller.equals(String.valueOf(key)))
System.err.println("found expected key: " + key);
else
error("Expected key not found");
checkFiles(outDir, Collections.<String>emptySet());
} else {
throw new Exception("task failed");
}
}
}
示例4: testStandardFileObject
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
/**
* Verify that expected output files are written via the file manager,
* for a source file read from the file system with StandardJavaFileManager.
*/
@Test
public void testStandardFileObject() throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File srcFile = new File(testSrc, "pkg/C.java");
DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File outDir = getOutDir();
fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(srcFile);
DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
if (t.call()) {
System.err.println("task succeeded");
checkFiles(outDir, standardExpectFiles);
} else {
throw new Exception("task failed");
}
}
}
示例5: testJavac
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
public void testJavac() throws Exception {
final JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
final StandardJavaFileManager fm = jc.getStandardFileManager(
null,
Locale.ENGLISH,
Charset.forName("UTF-8")); //NOI18N
fm.setLocation(
StandardLocation.CLASS_PATH,
Collections.singleton(FileUtil.archiveOrDirForURL(mvCp.entries().get(0).getURL())));
Iterable<JavaFileObject> res = fm.list(
StandardLocation.CLASS_PATH,
"", //NOI18N
EnumSet.of(JavaFileObject.Kind.CLASS),
true);
assertEquals(3, StreamSupport.stream(res.spliterator(), false).count());
}
示例6: parse
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
public static void parse(Path moduleInfoPath, ModuleClassVisitor moduleClassVisitor) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try(StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(moduleInfoPath);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
JavacTask javacTask = (JavacTask)task;
Iterable<? extends CompilationUnitTree> units= javacTask.parse();
CompilationUnitTree unit = units.iterator().next();
ModuleHandler moduleHandler = new ModuleHandler(moduleClassVisitor);
TreeVisitor<?,?> visitor = (TreeVisitor<?,?>)Proxy.newProxyInstance(TreeVisitor.class.getClassLoader(), new Class<?>[]{ TreeVisitor.class},
(proxy, method, args) -> {
ModuleHandler.METHOD_MAP
.getOrDefault(method.getName(), (handler, node, v) -> {
throw new AssertionError("invalid node " + node.getClass());
})
.visit(moduleHandler, (Tree)args[0], (TreeVisitor<?,?>)proxy);
return null;
});
unit.accept(visitor, null);
}
}
示例7: main
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
public static void main(String... args) throws Exception {
//create default shared JavaCompiler - reused across multiple compilations
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
for (VersionKind vk : VersionKind.values()) {
for (EnclosingKind ek : EnclosingKind.values()) {
for (MethodKind mk : MethodKind.values()) {
for (ModifierKind modk1 : ModifierKind.values()) {
for (ModifierKind modk2 : ModifierKind.values()) {
new TestDefaultMethodsSyntax(vk, ek, mk, modk1, modk2).run(comp, fm);
}
}
}
}
}
System.out.println("Total check executed: " + checkCount);
}
}
示例8: testSystemModules
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
@Test
public void testSystemModules(Path base) throws IOException {
try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
Location locn = StandardLocation.SYSTEM_MODULES;
Location javaCompiler = fm.getLocationForModule(locn, "java.compiler");
// cannot easily verify default setting: could be jrt: or exploded image
Path override1 = Files.createDirectories(base.resolve("override1"));
fm.setLocationForModule(locn, "java.compiler", List.of(override1));
checkEqual("override setting 1",
fm.getLocationAsPaths(javaCompiler), override1);
Path override2 = Files.createDirectories(base.resolve("override2"));
fm.setLocationFromPaths(javaCompiler, List.of(override2));
checkEqual("override setting 2",
fm.getLocationAsPaths(javaCompiler), override2);
}
}
示例9: handleServiceLoaderUnavailability
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
/**
* Returns an empty processor iterator if no processors are on the
* relevant path, otherwise if processors are present, logs an
* error. Called when a service loader is unavailable for some
* reason, either because a service loader class cannot be found
* or because a security policy prevents class loaders from being
* created.
*
* @param key The resource key to use to log an error message
* @param e If non-null, pass this exception to Abort
*/
private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) {
JavaFileManager fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof JavacFileManager) {
StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH)
: standardFileManager.getLocation(CLASS_PATH);
if (needClassLoader(options.get(PROCESSOR), workingPath) )
handleException(key, e);
} else {
handleException(key, e);
}
java.util.List<Processor> pl = Collections.emptyList();
return pl.iterator();
}
示例10: checkContains
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
void checkContains(StandardJavaFileManager fm, Location l, FileObject fo, boolean expect) throws IOException {
boolean found = fm.contains(l, fo);
if (found) {
if (expect) {
out.println("file found, as expected: " + l + " " + fo.getName());
} else {
error("file not found: " + l + " " + fo.getName());
}
} else {
if (expect) {
error("file found unexpectedly: " + l + " " + fo.getName());
} else {
out.println("file not found, as expected: " + l + " " + fo.getName());
}
}
}
示例11: main
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
File src = new File("C.java");
Writer w = new FileWriter(src);
try {
w.write("import static p.Generated.m;\nclass C { {m(); } }\n");
w.flush();
} finally {
w.close();
}
JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = jc.getStandardFileManager(null, null, null)) {
JavaCompiler.CompilationTask task = jc.getTask(null, fm, null, null, null,
fm.getJavaFileObjects(src));
task.setProcessors(Collections.singleton(new Proc()));
if (!task.call()) {
throw new Error("Test failed");
}
}
}
示例12: getFactory
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
/**
* Get the appropriate factory, based on the file manager given in the
* configuration.
*/
static synchronized DocFileFactory getFactory(Configuration configuration) {
DocFileFactory f = factories.get(configuration);
if (f == null) {
JavaFileManager fm = configuration.getFileManager();
if (fm instanceof StandardJavaFileManager)
f = new StandardDocFileFactory(configuration);
else {
try {
Class<?> pathFileManagerClass =
Class.forName("com.sun.tools.javac.nio.PathFileManager");
if (pathFileManagerClass.isAssignableFrom(fm.getClass()))
f = new PathDocFileFactory(configuration);
} catch (Throwable t) {
throw new IllegalStateException(t);
}
}
factories.put(configuration, f);
}
return f;
}
示例13: testBasic
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
@Test
public void testBasic(Path base) throws IOException {
try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
Location[] locns = {
StandardLocation.SOURCE_PATH,
StandardLocation.CLASS_PATH,
StandardLocation.PLATFORM_CLASS_PATH,
};
// set a value
Path out = Files.createDirectories(base.resolve("out"));
for (Location locn : locns) {
checkException("unsupported for location",
IllegalArgumentException.class,
"location is not an output location or a module-oriented location: " + locn,
() -> fm.setLocationForModule(locn, "m", List.of(out)));
}
}
}
示例14: handleServiceLoaderUnavailability
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
/**
* Returns an empty processor iterator if no processors are on the
* relevant path, otherwise if processors are present, logs an
* error. Called when a service loader is unavailable for some
* reason, either because a service loader class cannot be found
* or because a security policy prevents class loaders from being
* created.
*
* @param key The resource key to use to log an error message
* @param e If non-null, pass this exception to Abort
*/
private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) {
if (fileManager instanceof JavacFileManager) {
StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
Iterable<? extends Path> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
? standardFileManager.getLocationAsPaths(ANNOTATION_PROCESSOR_PATH)
: standardFileManager.getLocationAsPaths(CLASS_PATH);
if (needClassLoader(options.get(Option.PROCESSOR), workingPath) )
handleException(key, e);
} else {
handleException(key, e);
}
java.util.List<Processor> pl = Collections.emptyList();
return pl.iterator();
}
示例15: javac
import javax.tools.StandardJavaFileManager; //导入依赖的package包/类
static void javac(Path dest, List<Path> sourceFiles) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null)) {
List<File> files = sourceFiles.stream()
.map(p -> p.toFile())
.collect(Collectors.toList());
Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjectsFromFiles(files);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT,
Arrays.asList(dest.toFile()));
fileManager.setLocation(StandardLocation.CLASS_PATH,
Arrays.asList(TEST_CLASSES.toFile()));
JavaCompiler.CompilationTask task = compiler
.getTask(null, fileManager, null, null, null, compilationUnits);
boolean passed = task.call();
if (!passed)
throw new RuntimeException("Error compiling " + files);
}
}