本文整理汇总了Java中javax.tools.StandardLocation类的典型用法代码示例。如果您正苦于以下问题:Java StandardLocation类的具体用法?Java StandardLocation怎么用?Java StandardLocation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StandardLocation类属于javax.tools包,在下文中一共展示了StandardLocation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testJavac
import javax.tools.StandardLocation; //导入依赖的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());
}
示例2: write
import javax.tools.StandardLocation; //导入依赖的package包/类
/** Emit a class file for a given class.
* @param c The class from which a class file is generated.
*/
public FileObject write(ClassSymbol c)
throws IOException
{
String className = c.flatName().toString();
FileObject outFile
= fileManager.getFileForOutput(StandardLocation.NATIVE_HEADER_OUTPUT,
"", className.replaceAll("[.$]", "_") + ".h", null);
Writer out = outFile.openWriter();
try {
write(out, c);
if (verbose)
log.printVerbose("wrote.file", outFile);
out.close();
out = null;
} finally {
if (out != null) {
// if we are propogating an exception, delete the file
out.close();
outFile.delete();
outFile = null;
}
}
return outFile; // may be null if write failed
}
示例3: testAPI
import javax.tools.StandardLocation; //导入依赖的package包/类
void testAPI(String opt, List<String> ref) throws Exception {
File identifiers = new File(testSrc, "Identifiers.java");
fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, Arrays.asList(pluginJar));
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
List<String> options = Arrays.asList(opt);
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
System.err.println("test api: " + options + " " + files);
Task.Result result = new JavacTask(tb, Task.Mode.API)
.fileManager(fm)
.options(opt)
.files(identifiers.toPath())
.run(Task.Expect.SUCCESS)
.writeAll();
String out = result.getOutput(Task.OutputKind.DIRECT);
checkOutput(out, ref);
}
示例4: identicalFileIsAlreadyGenerated
import javax.tools.StandardLocation; //导入依赖的package包/类
private boolean identicalFileIsAlreadyGenerated(CharSequence sourceCode) {
try {
String existingContent = new CharSource() {
final String packagePath = !key.packageName.isEmpty() ? (key.packageName.replace('.', '/') + '/') : "";
final String filename = key.relativeName + ".java";
@Override
public Reader openStream() throws IOException {
return getFiler()
.getResource(StandardLocation.SOURCE_OUTPUT,
"", packagePath + filename)
.openReader(true);
}
}.read();
if (existingContent.contentEquals(sourceCode)) {
// We are ok, for some reason the same file is already generated,
// happens in Eclipse for example.
return true;
}
} catch (Exception ignoredAttemptToGetExistingFile) {
// we have some other problem, not an existing file
}
return false;
}
示例5: getLocation_ISA
import javax.tools.StandardLocation; //导入依赖的package包/类
@Test
public void getLocation_ISA(Path base) throws Exception {
Path src1 = base.resolve("src1");
tb.writeJavaFiles(src1.resolve("m1x"), "module m1x { }", "package a; class A { }");
Path src2 = base.resolve("src2");
tb.writeJavaFiles(src2.resolve("m2x").resolve("extra"), "module m2x { }", "package b; class B { }");
Path modules = base.resolve("modules");
tb.createDirectories(modules);
String FS = File.separator;
String PS = File.pathSeparator;
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
fm.handleOption("--module-source-path",
List.of(src1 + PS + src2 + FS + "*" + FS + "extra").iterator());
try {
Iterable<? extends Path> paths = fm.getLocationAsPaths(StandardLocation.MODULE_SOURCE_PATH);
out.println("result: " + asList(paths));
throw new Exception("expected IllegalStateException not thrown");
} catch (IllegalStateException e) {
out.println("Exception thrown, as expected: " + e);
}
// even if we can't do getLocation for the MODULE_SOURCE_PATH, we should be able
// to do getLocation for the modules, which will additionally confirm the option
// was effective as intended.
Location locn1 = fm.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, "m1x");
checkLocation(fm.getLocationAsPaths(locn1), List.of(src1.resolve("m1x")));
Location locn2 = fm.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, "m2x");
checkLocation(fm.getLocationAsPaths(locn2), List.of(src2.resolve("m2x").resolve("extra")));
}
}
示例6: test_simple_interface_no_package
import javax.tools.StandardLocation; //导入依赖的package包/类
@Test
public void test_simple_interface_no_package() {
JavaFileObject[] files = {JavaFileObjects.forResource("InterFaceTestNoPackage.java")};
Compilation c = javac()
.withProcessors(new TsGenProcessor())
.compile(files);
assertEquals(0, c.errors().size());
// using default packages issues a warning
assertEquals(c.diagnostics().asList().stream().filter(x -> x.getKind().equals(Diagnostic.Kind.WARNING)).count(), 1);
// module name has to be is unknown
assertTrue(c.generatedFile(StandardLocation.SOURCE_OUTPUT, JTSGEN_UNKNOWN, PACKAGE_JSON).isPresent());
assertTrue(c.generatedFile(StandardLocation.SOURCE_OUTPUT, JTSGEN_UNKNOWN, "unknown.d.ts").isPresent());
}
示例7: compile
import javax.tools.StandardLocation; //导入依赖的package包/类
/**
* Compile all the java sources in {@code <source>/**} to
* {@code <destination>/**}. The destination directory will be created if
* it doesn't exist.
*
* All warnings/errors emitted by the compiler are output to System.out/err.
*
* @return true if the compilation is successful
*
* @throws IOException if there is an I/O error scanning the source tree or
* creating the destination directory
*/
public static boolean compile(Path source, Path destination, String ... options)
throws IOException
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
List<Path> sources
= Files.find(source, Integer.MAX_VALUE,
(file, attrs) -> (file.toString().endsWith(".java")))
.collect(Collectors.toList());
Files.createDirectories(destination);
jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
Arrays.asList(destination));
List<String> opts = Arrays.asList(options);
JavaCompiler.CompilationTask task
= compiler.getTask(null, jfm, null, opts, null,
jfm.getJavaFileObjectsFromPaths(sources));
return task.call();
}
示例8: testBasic
import javax.tools.StandardLocation; //导入依赖的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)));
}
}
}
示例9: run
import javax.tools.StandardLocation; //导入依赖的package包/类
int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {
System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);
File testDir = new File(ck + "-" + gk + "-" + sk);
testDir.mkdirs();
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));
JavaSource js = new JavaSource();
System.err.println(js.getCharContent(false));
CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
if (!t.call())
throw new Error("compilation failed");
File testClass = new File(testDir, "Test.class");
String out = javap(testClass);
// Extract class sig from first line of Java source
String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");
// Extract class sig from line from javap output
String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");
checkEqual("class signature", expect, found);
return errors;
}
示例10: compileWithJSR199
import javax.tools.StandardLocation; //导入依赖的package包/类
void compileWithJSR199() throws IOException {
String cpath = "C2.jar";
File clientJarFile = new File(cpath);
File sourceFileToCompile = new File("C3.java");
javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
try (StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null)) {
List<File> files = new ArrayList<>();
files.add(clientJarFile);
stdFileManager.setLocation(StandardLocation.CLASS_PATH, files);
Iterable<? extends JavaFileObject> sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile);
if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) {
throw new AssertionError("compilation failed");
}
}
}
示例11: getLocationForModule
import javax.tools.StandardLocation; //导入依赖的package包/类
@Override
@CheckForNull
public Location getLocationForModule(Location location, JavaFileObject jfo) throws IOException {
if (location != StandardLocation.MODULE_SOURCE_PATH) {
throw new IllegalStateException(String.format("Unsupported location: %s", location));
}
final FileObject fo = URLMapper.findFileObject(jfo.toUri().toURL());
if (fo != null) {
for (ModuleLocation.WithExcludes moduleLocation : sourceModuleLocationsRemovedPatches()) {
for (ClassPath.Entry moduleEntry : moduleLocation.getModuleEntries()) {
final FileObject root = moduleEntry.getRoot();
if (root != null && FileUtil.isParentOf(root, fo)) {
return moduleLocation;
}
}
}
}
return null;
}
示例12: getFileForOutput
import javax.tools.StandardLocation; //导入依赖的package包/类
@Override
@CheckForNull
public FileObject getFileForOutput(
@NonNull final Location l,
@NonNull final String packageName,
@NonNull final String relativeName,
@NullAllowed final FileObject sibling)
throws IOException, UnsupportedOperationException, IllegalArgumentException {
checkSingleOwnerThread();
try {
JavaFileManager[] fms = cfg.getFileManagers(
l == StandardLocation.SOURCE_PATH ?
SOURCE_PATH_WRITE : l,
null);
if (fms.length == 0) {
throw new UnsupportedOperationException("No JavaFileManager for location: " + l); //NOI18N
} else {
return mark(
fms[0].getFileForOutput(l, packageName, relativeName, sibling),
l);
}
} finally {
clearOwnerThread();
}
}
示例13: list
import javax.tools.StandardLocation; //导入依赖的package包/类
@Override
Iterable<DocFile> list(Location location, DocPath path) {
Location l = ((location == StandardLocation.SOURCE_PATH)
&& !fileManager.hasLocation(StandardLocation.SOURCE_PATH))
? StandardLocation.CLASS_PATH
: location;
Set<DocFile> files = new LinkedHashSet<>();
for (Path f: fileManager.getLocationAsPaths(l)) {
if (Files.isDirectory(f)) {
f = f.resolve(path.getPath());
if (Files.exists(f))
files.add(new StandardDocFile(f));
}
}
return files;
}
示例14: runTest
import javax.tools.StandardLocation; //导入依赖的package包/类
void runTest(String aJava, String bJava) throws Exception {
try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) {
ToolBox tb = new ToolBox();
ToolBox.MemoryFileManager memoryFM1 = new ToolBox.MemoryFileManager(fm);
new JavacTask(tb).fileManager(memoryFM1)
.sources(aJava, bJava)
.run();
ToolBox.MemoryFileManager memoryFM2 = new ToolBox.MemoryFileManager(fm);
new JavacTask(tb).fileManager(memoryFM2)
.sources(bJava, aJava)
.run();
Assert.check(Arrays.equals(memoryFM1.getFileBytes(StandardLocation.CLASS_OUTPUT, "B"),
memoryFM2.getFileBytes(StandardLocation.CLASS_OUTPUT, "B")));
}
}
示例15: testOutput_nested
import javax.tools.StandardLocation; //导入依赖的package包/类
@Test
public void testOutput_nested(Path base) throws IOException {
try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
Location locn = StandardLocation.CLASS_OUTPUT;
Path out1 = Files.createDirectories(base.resolve("out1"));
fm.setLocationForModule(locn, "m", List.of(out1));
Location m = fm.getLocationForModule(locn, "m");
checkEqual("initial setting",
fm.getLocationAsPaths(m), out1);
Path out2 = Files.createDirectories(base.resolve("out2"));
checkException("create nested module",
UnsupportedOperationException.class, "not supported for CLASS_OUTPUT[m]",
() -> fm.setLocationForModule(m, "x", List.of(out2)));
}
}