Java Code Examples for com.sun.tools.javac.main.JavaCompiler#parse()

The following examples show how to use com.sun.tools.javac.main.JavaCompiler#parse() . 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: T4910483.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String... args) {
    JavaCompiler compiler = JavaCompiler.instance(new Context());
    compiler.keepComments = true;

    String testSrc = System.getProperty("test.src");
    JavacFileManager fm = new JavacFileManager(new Context(), false, null);
    JavaFileObject f = fm.getJavaFileObject(testSrc + File.separatorChar + "T4910483.java");

    JCTree.JCCompilationUnit cu = compiler.parse(f);
    JCTree classDef = cu.getTypeDecls().head;
    String commentText = cu.docComments.getCommentText(classDef);

    String expected = "Test comment abc*\\\\def"; // 4 '\' escapes to 2 in a string literal
    if (!expected.equals(commentText)) {
        throw new AssertionError("Incorrect comment text: [" + commentText + "], expected [" + expected + "]");
    }
}
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static Scope constructScope(CompilationInfo info, Map<String, TypeMirror> constraints, Iterable<? extends String> auxiliaryImports) {
        ScopeDescription desc = new ScopeDescription(constraints, auxiliaryImports);
        Scope result = (Scope) info.getCachedValue(desc);

        if (result != null) return result;
        
        StringBuilder clazz = new StringBuilder();

        clazz.append("package $$;");

        for (String i : auxiliaryImports) {
            clazz.append(i);
        }

        long count = inc++;

        String classname = "$$scopeclass$constraints$" + count;

        clazz.append("public class " + classname + "{");

        for (Entry<String, TypeMirror> e : constraints.entrySet()) {
            if (e.getValue() != null) {
                clazz.append("private ");
                clazz.append(e.getValue().toString()); //XXX
                clazz.append(" ");
                clazz.append(e.getKey());
                clazz.append(";\n");
            }
        }

        clazz.append("private void test() {\n");
        clazz.append("}\n");
        clazz.append("}\n");

        JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info);
        Context context = jti.getContext();
        JavaCompiler compiler = JavaCompiler.instance(context);
        Modules modules = Modules.instance(context);
        Log log = Log.instance(context);
        NBResolve resolve = NBResolve.instance(context);
        Annotate annotate = Annotate.instance(context);
        Names names = Names.instance(context);
        Symtab syms = Symtab.instance(context);
        Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log);

        JavaFileObject jfo = FileObjects.memoryFileObject("$", "$", new File("/tmp/$$scopeclass$constraints$" + count + ".java").toURI(), System.currentTimeMillis(), clazz.toString());

        try {
            resolve.disableAccessibilityChecks();
            if (compiler.isEnterDone()) {
                annotate.blockAnnotations();
//                try {
//                    Field f = compiler.getClass().getDeclaredField("enterDone");
//                    f.setAccessible(true);
//                    f.set(compiler, false);
//                } catch (Throwable t) {
//                    Logger.getLogger(Utilities.class.getName()).log(Level.FINE, null, t);
//                }
                //was:
//                compiler.resetEnterDone();
            }
            
            JCCompilationUnit cut = compiler.parse(jfo);
            ClassSymbol enteredClass = syms.enterClass(modules.getDefaultModule(), names.fromString("$$." + classname));
            modules.enter(com.sun.tools.javac.util.List.of(cut), enteredClass);
            compiler.enterTrees(com.sun.tools.javac.util.List.of(cut));

            Todo todo = compiler.todo;
            ListBuffer<Env<AttrContext>> defer = new ListBuffer<Env<AttrContext>>();
            
            while (todo.peek() != null) {
                Env<AttrContext> env = todo.remove();

                if (env.toplevel == cut)
                    compiler.attribute(env);
                else
                    defer = defer.append(env);
            }

            todo.addAll(defer);

            Scope res = new ScannerImpl().scan(cut, info);

            info.putCachedValue(desc, res, CacheClearPolicy.ON_SIGNATURE_CHANGE);

            return res;
        } finally {
            resolve.restoreAccessbilityChecks();
            log.popDiagnosticHandler(discardHandler);
        }
    }