下面列出了javax.tools.StandardJavaFileManager#inferBinaryName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void analyzeResource(URI resource)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
JavaFileManager.Location location =
StandardLocation.locationFor(resource.getPath());
fm.setLocation(location, com.sun.tools.javac.util.List.of(
new File(resource.getPath())));
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void analyzeResource(URI resource)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
JavaFileManager.Location location =
StandardLocation.locationFor(resource.getPath());
fm.setLocation(location, com.sun.tools.javac.util.List.of(
new File(resource.getPath())));
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void analyzeResource(URI resource)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
JavaFileManager.Location location =
StandardLocation.locationFor(resource.getPath());
fm.setLocation(location, com.sun.tools.javac.util.List.of(
new File(resource.getPath())));
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void analyzeResource(URI resource)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
JavaFileManager.Location location =
StandardLocation.locationFor(resource.getPath());
fm.setLocation(location, com.sun.tools.javac.util.List.of(
new File(resource.getPath())));
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void analyzeModule(StandardJavaFileManager fm, String moduleName)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaFileManager.Location location =
fm.getLocationForModule(StandardLocation.SYSTEM_MODULES, moduleName);
if (location == null)
throw new AssertionError("can't find module " + moduleName);
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void analyzeResource(URI resource)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
JavaFileManager.Location location =
StandardLocation.locationFor(resource.getPath());
fm.setLocation(location, com.sun.tools.javac.util.List.of(
new File(resource.getPath())));
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void analyzeResource(URI resource)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
JavaFileManager.Location location =
StandardLocation.locationFor(resource.getPath());
fm.setLocation(location, com.sun.tools.javac.util.List.of(
new File(resource.getPath())));
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void analyzeResource(URI resource)
throws
IOException,
ConstantPoolException,
InvalidDescriptor {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
JavaFileManager.Location location =
StandardLocation.locationFor(resource.getPath());
fm.setLocation(location, com.sun.tools.javac.util.List.of(
new File(resource.getPath())));
for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
String className = fm.inferBinaryName(location, file);
int index = className.lastIndexOf('.');
String pckName = index == -1 ? "" : className.substring(0, index);
if (shouldAnalyzePackage(pckName)) {
analyzeClassFile(ClassFile.read(file.openInputStream()));
}
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
static boolean dumpJDK9AndNewerBootClassPath(
int hostMajorVersion, Path output, Path targetJavabase) throws IOException {
// JDK 9 and newer support cross-compiling to older platform versions using the --system
// and --release flags.
// * --system takes the path to a JDK root for JDK 9 and up, and causes the compilation
// to target the APIs from that JDK.
// * --release takes a language level (e.g. '9') and uses the API information baked in to
// the host JDK (in lib/ct.sym).
// Since --system only supports JDK >= 9, first check of the target JDK defines a JDK 8
// bootclasspath.
List<Path> bootClassPathJars = getBootClassPathJars(targetJavabase);
if (!bootClassPathJars.isEmpty()) {
writeClassPathJars(output, bootClassPathJars);
return true;
}
// Initialize a FileManager to process the --system argument, and then read the
// initialized bootclasspath data back out.
Context context = new Context();
JavacTool.create()
.getTask(
/* out = */ null,
/* fileManager = */ null,
/* diagnosticListener = */ null,
/* options = */ Arrays.asList("--system", String.valueOf(targetJavabase)),
/* classes = */ null,
/* compilationUnits = */ null,
context);
StandardJavaFileManager fileManager =
(StandardJavaFileManager) context.get(JavaFileManager.class);
SortedMap<String, InputStream> entries = new TreeMap<>();
for (JavaFileObject fileObject :
fileManager.list(
StandardLocation.PLATFORM_CLASS_PATH,
"",
EnumSet.of(Kind.CLASS),
/* recurse= */ true)) {
String binaryName =
fileManager.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fileObject);
entries.put(binaryName.replace('.', '/') + ".class", fileObject.openInputStream());
}
writeEntries(output, entries);
return true;
}