下面列出了javax.tools.StandardJavaFileManager#getJavaFileForInput ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
private void doTestTypeSimplifier(
AbstractTestProcessor testProcessor, File tmpDir, ImmutableMap<String, String> classToSource)
throws IOException {
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticCollector =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager =
javac.getStandardFileManager(diagnosticCollector, null, null);
StringWriter compilerOut = new StringWriter();
List<String> options = ImmutableList.of(
"-sourcepath", tmpDir.getPath(),
"-d", tmpDir.getPath(),
"-Xlint");
javac.getTask(compilerOut, fileManager, diagnosticCollector, options, null, null);
// This doesn't compile anything but communicates the paths to the JavaFileManager.
ImmutableList.Builder<JavaFileObject> javaFilesBuilder = ImmutableList.builder();
for (String className : classToSource.keySet()) {
JavaFileObject sourceFile = fileManager.getJavaFileForInput(
StandardLocation.SOURCE_PATH, className, Kind.SOURCE);
javaFilesBuilder.add(sourceFile);
}
// Compile the empty source file to trigger the annotation processor.
// (Annotation processors are somewhat misnamed because they run even on classes with no
// annotations.)
JavaCompiler.CompilationTask javacTask = javac.getTask(
compilerOut, fileManager, diagnosticCollector, options,
classToSource.keySet(), javaFilesBuilder.build());
javacTask.setProcessors(ImmutableList.of(testProcessor));
javacTask.call();
List<Diagnostic<? extends JavaFileObject>> diagnostics =
new ArrayList<Diagnostic<? extends JavaFileObject>>(diagnosticCollector.getDiagnostics());
// In the ErrorTestProcessor case, the code being compiled contains a deliberate reference to an
// undefined type, so that we can capture an instance of ErrorType. (Synthesizing one ourselves
// leads to ClassCastException inside javac.) So remove any errors for that from the output, and
// only fail if there were other errors.
for (Iterator<Diagnostic<? extends JavaFileObject>> it = diagnostics.iterator();
it.hasNext(); ) {
Diagnostic<? extends JavaFileObject> diagnostic = it.next();
if (diagnostic.getSource() != null
&& diagnostic.getSource().getName().contains("ExtendsUndefinedType")) {
it.remove();
}
}
// In the ErrorTestProcessor case, compilerOut.toString() will include the error for
// ExtendsUndefinedType, which can safely be ignored, as well as stack traces for any failing
// assertion.
assertEquals(compilerOut.toString() + diagnosticCollector.getDiagnostics(),
ImmutableList.of(), diagnostics);
}
private void doTestTypeSimplifier(
AbstractTestProcessor testProcessor, File tmpDir, ImmutableMap<String, String> classToSource)
throws IOException {
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticCollector =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager =
javac.getStandardFileManager(diagnosticCollector, null, null);
StringWriter compilerOut = new StringWriter();
List<String> options = ImmutableList.of(
"-sourcepath", tmpDir.getPath(),
"-d", tmpDir.getPath(),
"-Xlint");
javac.getTask(compilerOut, fileManager, diagnosticCollector, options, null, null);
// This doesn't compile anything but communicates the paths to the JavaFileManager.
ImmutableList.Builder<JavaFileObject> javaFilesBuilder = ImmutableList.builder();
for (String className : classToSource.keySet()) {
JavaFileObject sourceFile = fileManager.getJavaFileForInput(
StandardLocation.SOURCE_PATH, className, Kind.SOURCE);
javaFilesBuilder.add(sourceFile);
}
// Compile the empty source file to trigger the annotation processor.
// (Annotation processors are somewhat misnamed because they run even on classes with no
// annotations.)
JavaCompiler.CompilationTask javacTask = javac.getTask(
compilerOut, fileManager, diagnosticCollector, options,
classToSource.keySet(), javaFilesBuilder.build());
javacTask.setProcessors(ImmutableList.of(testProcessor));
javacTask.call();
List<Diagnostic<? extends JavaFileObject>> diagnostics =
new ArrayList<Diagnostic<? extends JavaFileObject>>(diagnosticCollector.getDiagnostics());
// In the ErrorTestProcessor case, the code being compiled contains a deliberate reference to an
// undefined type, so that we can capture an instance of ErrorType. (Synthesizing one ourselves
// leads to ClassCastException inside javac.) So remove any errors for that from the output, and
// only fail if there were other errors.
for (Iterator<Diagnostic<? extends JavaFileObject>> it = diagnostics.iterator();
it.hasNext(); ) {
Diagnostic<? extends JavaFileObject> diagnostic = it.next();
if (diagnostic.getSource() != null
&& diagnostic.getSource().getName().contains("ExtendsUndefinedType")) {
it.remove();
}
}
// In the ErrorTestProcessor case, compilerOut.toString() will include the error for
// ExtendsUndefinedType, which can safely be ignored, as well as stack traces for any failing
// assertion.
assertEquals(compilerOut.toString() + diagnosticCollector.getDiagnostics(),
ImmutableList.of(), diagnostics);
}
private void doTestTypeSimplifier(
AbstractTestProcessor testProcessor, File tmpDir, ImmutableMap<String, String> classToSource)
throws IOException {
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticCollector =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager =
javac.getStandardFileManager(diagnosticCollector, null, null);
StringWriter compilerOut = new StringWriter();
List<String> options = ImmutableList.of(
"-sourcepath", tmpDir.getPath(),
"-d", tmpDir.getPath(),
"-Xlint");
javac.getTask(compilerOut, fileManager, diagnosticCollector, options, null, null);
// This doesn't compile anything but communicates the paths to the JavaFileManager.
ImmutableList.Builder<JavaFileObject> javaFilesBuilder = ImmutableList.builder();
for (String className : classToSource.keySet()) {
JavaFileObject sourceFile = fileManager.getJavaFileForInput(
StandardLocation.SOURCE_PATH, className, Kind.SOURCE);
javaFilesBuilder.add(sourceFile);
}
// Compile the empty source file to trigger the annotation processor.
// (Annotation processors are somewhat misnamed because they run even on classes with no
// annotations.)
JavaCompiler.CompilationTask javacTask = javac.getTask(
compilerOut, fileManager, diagnosticCollector, options,
classToSource.keySet(), javaFilesBuilder.build());
javacTask.setProcessors(ImmutableList.of(testProcessor));
javacTask.call();
List<Diagnostic<? extends JavaFileObject>> diagnostics =
new ArrayList<Diagnostic<? extends JavaFileObject>>(diagnosticCollector.getDiagnostics());
// In the ErrorTestProcessor case, the code being compiled contains a deliberate reference to an
// undefined type, so that we can capture an instance of ErrorType. (Synthesizing one ourselves
// leads to ClassCastException inside javac.) So remove any errors for that from the output, and
// only fail if there were other errors.
for (Iterator<Diagnostic<? extends JavaFileObject>> it = diagnostics.iterator();
it.hasNext(); ) {
Diagnostic<? extends JavaFileObject> diagnostic = it.next();
if (diagnostic.getSource() != null
&& diagnostic.getSource().getName().contains("ExtendsUndefinedType")) {
it.remove();
}
}
// In the ErrorTestProcessor case, compilerOut.toString() will include the error for
// ExtendsUndefinedType, which can safely be ignored, as well as stack traces for any failing
// assertion.
assertEquals(compilerOut.toString() + diagnosticCollector.getDiagnostics(),
ImmutableList.of(), diagnostics);
}