本文整理汇总了Java中javax.tools.StandardJavaFileManager.setLocation方法的典型用法代码示例。如果您正苦于以下问题:Java StandardJavaFileManager.setLocation方法的具体用法?Java StandardJavaFileManager.setLocation怎么用?Java StandardJavaFileManager.setLocation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.tools.StandardJavaFileManager
的用法示例。
在下文中一共展示了StandardJavaFileManager.setLocation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createGoldenJFM
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
/** Crates the default javac file managare tro have something to comare
* our file managers against
*/
public static JavaFileManager createGoldenJFM( File[] classpath, File[] sourcpath ) throws IOException {
JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = jc.getStandardFileManager (null, null, null);
if ( classpath != null ) {
fm.setLocation(StandardLocation.CLASS_PATH,Arrays.asList(classpath));
}
if ( sourcpath != null ) {
fm.setLocation(StandardLocation.SOURCE_PATH,Arrays.asList(sourcpath));
}
return fm;
}
示例2: JdkCompiler
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
public JdkCompiler(){
options = new ArrayList<String>();
options.add("-target");
options.add("1.6");
StandardJavaFileManager manager = compiler.getStandardFileManager(diagnosticCollector, null, null);
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader instanceof URLClassLoader
&& (! loader.getClass().getName().equals("sun.misc.Launcher$AppClassLoader"))) {
try {
URLClassLoader urlClassLoader = (URLClassLoader) loader;
List<File> files = new ArrayList<File>();
for (URL url : urlClassLoader.getURLs()) {
files.add(new File(url.getFile()));
}
manager.setLocation(StandardLocation.CLASS_PATH, files);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
classLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoaderImpl>() {
public ClassLoaderImpl run() {
return new ClassLoaderImpl(loader);
}
});
javaFileManager = new JavaFileManagerImpl(manager, classLoader);
}
示例3: testFileManager
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
/**
* Verify that an alternate file manager can be specified:
* in this case, a TestFileManager.
*/
@Test
public void testFileManager() throws Exception {
JavaFileObject srcFile = createSimpleJavaFileObject();
DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
StandardJavaFileManager fm = new TestFileManager();
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, null, Arrays.asList("-verbose"), files);
if (t.call()) {
System.err.println("task succeeded");
checkFiles(outDir, standardExpectFiles);
} else {
throw new Exception("task failed");
}
}
示例4: run
import javax.tools.StandardJavaFileManager; //导入方法依赖的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;
}
示例5: 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();
}
示例6: compileDependencyClass
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
@BeforeClass
public static void compileDependencyClass() throws IOException, ClassNotFoundException {
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
Assume.assumeNotNull(javaCompiler);
classes = temporaryFolder.newFolder("classes");;
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, Locale.ROOT, UTF_8);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(classes));
SimpleJavaFileObject compilationUnit = new SimpleJavaFileObject(URI.create("FooTest.java"), Kind.SOURCE) {
String fooTestSource = Resources.toString(Resources.getResource("com/dremio/exec/compile/FooTest.java"), UTF_8);
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return fooTestSource;
}
};
CompilationTask task = javaCompiler.getTask(null, fileManager, null, Collections.<String>emptyList(), null, ImmutableList.of(compilationUnit));
assertTrue(task.call());
}
示例7: compile
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
public void compile(JavaCompiler javaCompiler, Writer out, File sourceDirectory, File outputDirectory, List<File> classpath, DiagnosticListener<? super JavaFileObject> diagnosticListener, String targetVersion ) {
targetVersion = targetVersion == null ? "1.6" : targetVersion;
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null);
if (outputDirectory != null) {
try {
fileManager.setLocation(StandardLocation.CLASS_OUTPUT,
Arrays.asList(outputDirectory));
fileManager.setLocation(StandardLocation.CLASS_PATH, classpath);
} catch (IOException e) {
throw new RuntimeException("could not set output directory", e);
}
}
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(findAllSourceFiles(sourceDirectory));
ArrayList<String> options = new ArrayList<String>();
options.add("-source");
options.add(targetVersion);
options.add("-target");
options.add(targetVersion);
options.add("-encoding");
options.add("UTF8");
options.add("-Xlint:-options");
options.add("-Xlint:unchecked");
if (compilationUnits.iterator().hasNext()) {
Boolean success = javaCompiler.getTask(out, fileManager, diagnosticListener, options, null, compilationUnits).call();
assertThat("Compilation was not successful, check stdout for errors", success, is(true));
}
}
示例8: init
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
private void init()
{
if( _fm != null )
{
return;
}
synchronized( this )
{
if( _fm != null )
{
return;
}
_javacTool = JavacTool.create();
StandardJavaFileManager fm = _javacTool.getStandardFileManager( null, null, Charset.forName( "UTF-8" ) );
try
{
fm.setLocation( StandardLocation.SOURCE_PATH, _module.getCollectiveSourcePath().stream().map( IResource::toJavaFile ).collect( Collectors.toList() ) );
fm.setLocation( StandardLocation.CLASS_PATH, _module.getCollectiveJavaClassPath().stream().map( IResource::toJavaFile ).collect( Collectors.toList() ) );
_fm = fm;
}
catch( IOException e )
{
throw new RuntimeException( e );
}
}
}
示例9: JdkCompileTask
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
public JdkCompileTask(JdkCompilerClassLoader classLoader, Iterable<String> options){
compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new IllegalStateException("Cannot find the system Java compiler. "
+ "Check that your class path includes tools.jar");
}
this.classLoader = classLoader;
ClassLoader loader = classLoader.getParent();
diagnostics = new DiagnosticCollector<JavaFileObject>();
final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
if (loader instanceof URLClassLoader
&& (!loader.getClass().getName().equalsIgnoreCase("sun.misc.Launcher$AppClassLoader"))) {
try {
@SuppressWarnings("resource")
URLClassLoader urlClassLoader = (URLClassLoader) loader;
List<File> path = new ArrayList<File>();
for (URL url : urlClassLoader.getURLs()) {
File file = new File(url.getFile());
path.add(file);
}
fileManager.setLocation(StandardLocation.CLASS_PATH, path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
javaFileManager = new JavaFileManagerImpl(fileManager, classLoader);
this.options = new ArrayList<String>();
if (options != null) { // make a save copy of input options
for (String option : options) {
this.options.add(option);
}
}
}
示例10: run
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
public boolean run(String sourcepath, File outdir, List<String> classes) {
//System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
if (sourcepath == null)
throw new IllegalArgumentException("sourcepath not set");
if (outdir == null)
throw new IllegalArgumentException("source output dir not set");
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
try {
fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
List<JavaFileObject> files = new ArrayList<JavaFileObject>();
for (String c: classes) {
JavaFileObject fo = fm.getJavaFileForInput(
StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
if (fo == null)
error("class not found: " + c);
else
files.add(fo);
}
JavacTask t = tool.getTask(null, fm, null, null, null, files);
Iterable<? extends CompilationUnitTree> trees = t.parse();
for (CompilationUnitTree tree: trees) {
makeStub(fm, tree);
}
} catch (IOException e) {
error("IO error " + e, e);
}
return (errors == 0);
}
示例11: testBadFileManager
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
/**
* Verify that exceptions from a bad file manager are thrown as expected.
*/
@Test
public void testBadFileManager() throws Exception {
JavaFileObject srcFile = createSimpleJavaFileObject();
DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
StandardJavaFileManager fm = new TestFileManager() {
@Override
public Iterable<JavaFileObject> list(Location location,
String packageName,
Set<Kind> kinds,
boolean recurse)
throws IOException {
throw new UnexpectedError();
}
};
fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(getOutDir()));
Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
try {
t.call();
error("call completed without exception");
} catch (RuntimeException e) {
Throwable c = e.getCause();
if (c.getClass() == UnexpectedError.class)
System.err.println("exception caught as expected: " + c);
else
throw e;
}
}
示例12: test_setFiles_getFiles
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
void test_setFiles_getFiles(StandardJavaFileManager fm, List<File> inFiles) throws IOException {
System.err.println("test_setFiles_getFiles");
JavaFileManager.Location l = newLocation();
fm.setLocation(l, inFiles);
Iterable<? extends File> outFiles = fm.getLocation(l);
compare(inFiles, outFiles);
}
示例13: test_setFiles_getPaths
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
void test_setFiles_getPaths(StandardJavaFileManager fm, List<File> inFiles) throws IOException {
System.err.println("test_setFiles_getPaths");
JavaFileManager.Location l = newLocation();
fm.setLocation(l, inFiles);
Iterable<? extends Path> outPaths = fm.getLocationAsPaths(l);
compare(inFiles, outPaths);
}
示例14: compile
import javax.tools.StandardJavaFileManager; //导入方法依赖的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
* @throws UnsupportedOperationException
* if there is no system java compiler
*/
public static boolean compile(Path source, Path destination, String... options)
throws IOException
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
// no compiler available
throw new UnsupportedOperationException("Unable to get system java compiler. "
+ "Perhaps, jdk.compiler module is not available.");
}
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.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
Collections.singletonList(destination));
List<String> opts = Arrays.asList(options);
JavaCompiler.CompilationTask task
= compiler.getTask(null, jfm, null, opts, null,
jfm.getJavaFileObjectsFromPaths(sources));
return task.call();
}
示例15: run
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
void run() throws IOException {
File testSrc = new File(System.getProperty("test.src"));
File testClasses = new File(System.getProperty("test.classes"));
JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
final ClassLoader cl = getClass().getClassLoader();
Context c = new Context();
StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
@Override
protected ClassLoader getClassLoader(URL[] urls) {
return new URLClassLoader(urls, cl) {
@Override
public void close() throws IOException {
System.err.println(getClass().getName() + " closing");
TestClose2.this.closedCount++;
TestClose2.this.closedIsLast = true;
super.close();
}
};
}
};
fm.setLocation(StandardLocation.CLASS_OUTPUT,
Collections.singleton(new File(".")));
fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
Collections.singleton(testClasses));
Iterable<? extends JavaFileObject> files =
fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
List<String> options = Arrays.asList(
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
"-processor", TestClose2.class.getName());
JavacTask task = tool.getTask(null, fm, null, options, null, files);
task.setTaskListener(this);
if (!task.call())
throw new Error("compilation failed");
if (closedCount == 0)
throw new Error("no closing message");
else if (closedCount > 1)
throw new Error(closedCount + " closed messages");
if (!closedIsLast)
throw new Error("closing message not last");
}