本文整理汇总了Java中javax.tools.StandardJavaFileManager.getJavaFileObjects方法的典型用法代码示例。如果您正苦于以下问题:Java StandardJavaFileManager.getJavaFileObjects方法的具体用法?Java StandardJavaFileManager.getJavaFileObjects怎么用?Java StandardJavaFileManager.getJavaFileObjects使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.tools.StandardJavaFileManager
的用法示例。
在下文中一共展示了StandardJavaFileManager.getJavaFileObjects方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
static List<? extends Tree> parse(String srcfile) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(srcfile);
String classPath = System.getProperty("java.class.path");
List<String> options = Arrays.asList("-classpath", classPath);
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
Context context = new Context();
JavacTaskImpl task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, null,
diagnostics, options, null, fileObjects, context);
TrialParserFactory.instance(context);
Iterable<? extends CompilationUnitTree> asts = task.parse();
Iterator<? extends CompilationUnitTree> it = asts.iterator();
if (it.hasNext()) {
CompilationUnitTree cut = it.next();
return cut.getTypeDecls();
} else {
throw new AssertionError("Expected compilation unit");
}
}
示例2: compileFiles
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
public static void compileFiles(File projectRoot, List<String> javaFiles) {
DiagnosticCollector diagnosticCollector = new DiagnosticCollector();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8"));
Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(javaFiles.toArray(new String[0]));
File outputFolder = new File(projectRoot, "bin");
if (!outputFolder.exists()) {
outputFolder.mkdir();
}
String[] options = new String[] { "-d", outputFolder.getAbsolutePath() , "-g", "-proc:none"};
final StringWriter output = new StringWriter();
CompilationTask task = compiler.getTask(output, fileManager, diagnosticCollector, Arrays.asList(options), null, javaFileObjects);
boolean result = task.call();
if (!result) {
throw new IllegalArgumentException(
"Compilation failed:\n" + output);
}
List list = diagnosticCollector.getDiagnostics();
for (Object object : list) {
Diagnostic d = (Diagnostic) object;
System.out.println(d.getMessage(Locale.ENGLISH));
}
}
示例3: 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());
}
}
示例4: 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");
}
示例5: 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);
}
}
示例6: buildJar
import javax.tools.StandardJavaFileManager; //导入方法依赖的package包/类
/**
* Create a test jar for testing purpose for a given class
* name with specified code string.
*
* @param testDir the folder under which to store the test class
* @param className the test class name
* @param code the optional test class code, which can be null.
* If null, an empty class will be used
* @param folder the folder under which to store the generated jar
* @return the test jar file generated
*/
public static File buildJar(String testDir,
String className, String code, String folder) throws Exception {
String javaCode = code != null ? code : "public class " + className + " {}";
Path srcDir = new Path(testDir, "src");
File srcDirPath = new File(srcDir.toString());
srcDirPath.mkdirs();
File sourceCodeFile = new File(srcDir.toString(), className + ".java");
BufferedWriter bw = new BufferedWriter(new FileWriter(sourceCodeFile));
bw.write(javaCode);
bw.close();
// compile it by JavaCompiler
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
ArrayList<String> srcFileNames = new ArrayList<String>();
srcFileNames.add(sourceCodeFile.toString());
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null,
null);
Iterable<? extends JavaFileObject> cu =
fm.getJavaFileObjects(sourceCodeFile);
List<String> options = new ArrayList<String>();
options.add("-classpath");
// only add hbase classes to classpath. This is a little bit tricky: assume
// the classpath is {hbaseSrc}/target/classes.
String currentDir = new File(".").getAbsolutePath();
String classpath = currentDir + File.separator + "target"+ File.separator
+ "classes" + System.getProperty("path.separator")
+ System.getProperty("java.class.path") + System.getProperty("path.separator")
+ System.getProperty("surefire.test.class.path");
options.add(classpath);
LOG.debug("Setting classpath to: " + classpath);
JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null,
options, null, cu);
assertTrue("Compile file " + sourceCodeFile + " failed.", task.call());
// build a jar file by the classes files
String jarFileName = className + ".jar";
File jarFile = new File(folder, jarFileName);
jarFile.getParentFile().mkdirs();
if (!createJarArchive(jarFile,
new File[]{new File(srcDir.toString(), className + ".class")})){
assertTrue("Build jar file failed.", false);
}
return jarFile;
}