Java Code Examples for javax.tools.StandardJavaFileManager#inferBinaryName()

The following examples show how to use javax.tools.StandardJavaFileManager#inferBinaryName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DetectMutableStaticFields.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 2
Source File: DetectMutableStaticFields.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 3
Source File: DetectMutableStaticFields.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 4
Source File: DetectMutableStaticFields.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 5
Source File: DetectMutableStaticFields.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 6
Source File: DetectMutableStaticFields.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 7
Source File: DetectMutableStaticFields.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 8
Source File: DetectMutableStaticFields.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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()));
        }
    }
}
 
Example 9
Source File: GenStubs.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 10
Source File: GenStubs.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 11
Source File: GenStubs.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 12
Source File: GenStubs.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 13
Source File: GenStubs.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 14
Source File: GenStubs.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 15
Source File: GenStubs.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 16
Source File: GenStubs.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 17
Source File: DumpPlatformClassPath.java    From bazel with Apache License 2.0 4 votes vote down vote up
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;
}