Java Code Examples for com.sun.tools.javac.api.JavacTaskImpl#enter()

The following examples show how to use com.sun.tools.javac.api.JavacTaskImpl#enter() . 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: T6358786.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
        String srcdir = System.getProperty("test.src");
        File file = new File(srcdir, args[0]);
        List<String> options = Arrays.asList(
            "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED"
        );
        JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, options, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
        Elements elements = task.getElements();
        for (Element clazz : task.enter(task.parse())) {
            String doc = elements.getDocComment(clazz);
            if (doc == null)
                throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
            System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
        }
    }
}
 
Example 2
Source File: JavadocHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Pair<JavacTask, CompilationUnitTree> findSource(String moduleName,
                                                        String binaryName) throws IOException {
    JavaFileObject jfo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH,
                                                binaryName,
                                                JavaFileObject.Kind.SOURCE);

    if (jfo == null)
        return null;

    List<JavaFileObject> jfos = Arrays.asList(jfo);
    JavaFileManager patchFM = moduleName != null
            ? new PatchModuleFileManager(baseFileManager, jfo, moduleName)
            : baseFileManager;
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, patchFM, d -> {}, null, null, jfos);
    Iterable<? extends CompilationUnitTree> cuts = task.parse();

    task.enter();

    return Pair.of(task, cuts.iterator().next());
}
 
Example 3
Source File: ClasspathInfoTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testGetTypeDeclaration() throws Exception {
        ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null);
        JavacTaskImpl jTask = JavacParser.createJavacTask(ci,  (DiagnosticListener) null, (String) null, null, null, null, null, null, Collections.emptyList());
        jTask.enter(); 
	
        List<String> notFound = new LinkedList<String>();
        JarFile jf = new JarFile( rtJar );       
        for( Enumeration entries = jf.entries(); entries.hasMoreElements(); ) {
            JarEntry je = (JarEntry)entries.nextElement();
            String jeName = je.getName();
            if ( !je.isDirectory() && jeName.endsWith( ".class" ) ) {
                String typeName = jeName.substring( 0, jeName.length() - ".class".length() );

                typeName = typeName.replace( "/", "." ); //.replace( "$", "." );
                TypeElement te = ElementUtils.getTypeElementByBinaryName(jTask, typeName );
//                assertNotNull( "Declaration for " + typeName + " should not be null.", td );
                if ( te == null ) {
                    if (!typeName.endsWith("package-info")) {
                        notFound.add( typeName );
                    }
                }
            }
        }
        
        assertTrue( "Should be empty " + notFound, notFound.isEmpty() );
        
    }
 
Example 4
Source File: T6358786.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 5
Source File: TestJavacTask.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void basicTest(String... args) throws IOException {
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = getTask(file);
    for (TypeElement clazz : task.enter(task.parse()))
        System.out.println(clazz.getSimpleName());
}
 
Example 6
Source File: TestJavacTask.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void basicTest(String... args) throws IOException {
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = getTask(file);
    for (Element clazz : task.enter(task.parse()))
        System.out.println(clazz.getSimpleName());
}
 
Example 7
Source File: T6358786.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 8
Source File: T6358786.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 9
Source File: TestJavacTask.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void basicTest(String... args) throws IOException {
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = getTask(file);
    for (TypeElement clazz : task.enter(task.parse()))
        System.out.println(clazz.getSimpleName());
}
 
Example 10
Source File: TestJavacTask.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void basicTest(String... args) throws IOException {
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = getTask(file);
    for (TypeElement clazz : task.enter(task.parse()))
        System.out.println(clazz.getSimpleName());
}
 
Example 11
Source File: T6358786.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 12
Source File: TestJavacTask.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static void basicTest(String... args) throws IOException {
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = getTask(file);
    for (TypeElement clazz : task.enter(task.parse()))
        System.out.println(clazz.getSimpleName());
}
 
Example 13
Source File: T6358786.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 14
Source File: TestJavacTask.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void basicTest(String... args) throws IOException {
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = getTask(file);
    for (TypeElement clazz : task.enter(task.parse()))
        System.out.println(clazz.getSimpleName());
}
 
Example 15
Source File: T6358786.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 16
Source File: TestJavacTask.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void basicTest(String... args) throws IOException {
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = getTask(file);
    for (TypeElement clazz : task.enter(task.parse()))
        System.out.println(clazz.getSimpleName());
}
 
Example 17
Source File: T6358786.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    String srcdir = System.getProperty("test.src");
    File file = new File(srcdir, args[0]);
    JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
    Elements elements = task.getElements();
    for (TypeElement clazz : task.enter(task.parse())) {
        String doc = elements.getDocComment(clazz);
        if (doc == null)
            throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
        System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
    }
}
 
Example 18
Source File: SourceAnalyzerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testBrokenReference() throws Exception {
    final FileObject javaFile = FileUtil.toFileObject(TestFileUtils.writeFile(
         new File(FileUtil.toFile(src),"Test.java"),        //NOI18N
         "public class Test {   \n" +                       //NOI18N
         "    public static void main(String[] args) {\n" + //NOI18N
         "        Runnable r = Lib::foo;\n" +               //NOI18N
         "    }\n" +                                        //NOI18N
         "}"));                                             //NOI18N

    final DiagnosticListener<JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {

        private final Queue<Diagnostic<? extends JavaFileObject>> problems = new ArrayDeque<Diagnostic<? extends JavaFileObject>>();

        @Override
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            problems.offer(diagnostic);
        }
    };
    TransactionContext.beginStandardTransaction(src.toURL(), true, ()->false, true);
    try {
        final ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
            src,
            null,
            true,
            true,
            false,
            true);
        final JavaFileObject jfo = FileObjects.sourceFileObject(javaFile, src);
        final JavacTaskImpl jt = JavacParser.createJavacTask(
            cpInfo,
            diag,
            SourceLevelQuery.getSourceLevel(src),  //NOI18N
            SourceLevelQuery.Profile.DEFAULT,
            null, null, null, null, Arrays.asList(jfo));
        final Iterable<? extends CompilationUnitTree> trees = jt.parse();
        jt.enter();
        jt.analyze();
        final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer();
        List<Pair<Pair<BinaryName, String>, Object[]>> data = sa.analyseUnit(trees.iterator().next(), jt);
        assertEquals(1, data.size());
        assertTrue(((Collection)data.iterator().next().second()[0]).contains(
            DocumentUtil.encodeUsage("Lib", EnumSet.<ClassIndexImpl.UsageType>of(   //NOI18N
                ClassIndexImpl.UsageType.TYPE_REFERENCE))));
    } finally {
        TransactionContext.get().rollBack();
    }
}
 
Example 19
Source File: TransitiveDependencies.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws IOException {
    if (args.length < 1) {
        help();
        return ;
    }

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    List<String> options = Arrays.asList("-source", "9",
                                         "-target", "9",
                                         "--system", "none",
                                         "--module-source-path", args[0],
                                         "--add-modules", Arrays.stream(args)
                                                                .skip(1)
                                                                .collect(Collectors.joining(",")));
    List<String> jlObjectList = Arrays.asList("java.lang.Object");
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, d -> {}, options, jlObjectList, null);
    task.enter();
    Elements elements = task.getElements();
    List<String> todo = new LinkedList<>();
    Arrays.stream(args).skip(1).forEach(todo::add);
    Set<String> allModules = new HashSet<>();

    while (!todo.isEmpty()) {
        String current = todo.remove(0);

        if (!allModules.add(current))
            continue;

        ModuleSymbol mod = (ModuleSymbol) elements.getModuleElement(current);

        if (mod == null) {
            throw new IllegalStateException("Missing: " + current);
        }

         //use the internal structure to avoid unnecesarily completing the symbol using the UsesProvidesVisitor:
        for (RequiresDirective rd : mod.requires) {
            if (rd.isTransitive()) {
                todo.add(rd.getDependency().getQualifiedName().toString());
            }
        }
    }

    allModules.add("java.base");
    allModules.add("jdk.unsupported");

    allModules.stream()
              .sorted()
              .forEach(System.out::println);
}
 
Example 20
Source File: JavaBinaryIndexer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Pre builds argument names for {@link javax.swing.JComponent} to speed up first
 * call of code completion on swing classes. Has no semantic impact only improves performance,
 * so it's can be safely disabled.
 * @param archiveFile the archive
 * @param archiveUrl URL of an archive
 */
private static void preBuildArgs (
        @NonNull final String fqn,
        @NonNull final URL... archiveUrls) {
    class DevNullDiagnosticListener implements DiagnosticListener<JavaFileObject> {
        @Override
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "Diagnostic reported during prebuilding args: {0}", diagnostic.toString()); //NOI18N
            }
        }
    }
    ClasspathInfo cpInfo = ClasspathInfoAccessor.getINSTANCE().create(
        ClassPathSupport.createClassPath(archiveUrls),
        ClassPathSupport.createClassPath(archiveUrls),
        ClassPath.EMPTY,
        ClassPath.EMPTY,
        ClassPath.EMPTY,
        ClassPath.EMPTY,
        ClassPath.EMPTY,
        null,
        true,
        true,
        false,
        false,
        false,
        null);
    final JavacTaskImpl jt = JavacParser.createJavacTask(cpInfo, new DevNullDiagnosticListener(), null, null, null, null, null, null, Collections.emptyList());
    //Force JTImpl.prepareCompiler to get JTImpl into Context
    jt.enter();
    TypeElement jc = ElementUtils.getTypeElementByBinaryName(jt, fqn);
    if (jc != null) {
        List<ExecutableElement> methods = ElementFilter.methodsIn(jt.getElements().getAllMembers(jc));
        for (ExecutableElement method : methods) {
            List<? extends VariableElement> params = method.getParameters();
            if (!params.isEmpty()) {
                params.get(0).getSimpleName();
            }
        }
    }
}