Java Code Examples for com.sun.tools.javac.code.Symtab#enterClass()

The following examples show how to use com.sun.tools.javac.code.Symtab#enterClass() . 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: ElementUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static TypeElement getTypeElementByBinaryName(JavacTask task, ModuleElement mod, String name) {
    Context ctx = ((JavacTaskImpl) task).getContext();
    Names names = Names.instance(ctx);
    Symtab syms = Symtab.instance(ctx);
    Check chk = Check.instance(ctx);
    final Name wrappedName = names.fromString(name);
    ClassSymbol clazz = chk.getCompiled((ModuleSymbol) mod, wrappedName);
    if (clazz != null) {
        return clazz;
    }
    clazz = syms.enterClass((ModuleSymbol) mod, wrappedName);
    
    try {
        clazz.complete();
        
        if (clazz.kind == Kind.TYP &&
            clazz.flatName() == wrappedName) {
            return clazz;
        }
    } catch (CompletionFailure cf) {
    }

    return null;
}
 
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);
        }
    }
 
Example 3
Source File: T6889255.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void test(String testName, boolean expectNames, String... opts) throws Exception {
    System.err.println("Test " + testName
            + ": expectNames:" + expectNames
            + " javacOpts:" + Arrays.asList(opts));

    File outDir = new File(testName);
    outDir.mkdirs();
    compile(outDir, opts);

    Context ctx = new Context();
    JavacFileManager fm = new JavacFileManager(ctx, true, null);
    fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(outDir));
    Symtab syms = Symtab.instance(ctx);
    ClassReader cr = ClassReader.instance(ctx);
    cr.saveParameterNames = true;
    Names names = Names.instance(ctx);

    Set<String> classes = getTopLevelClasses(outDir);
    Deque<String> work = new LinkedList<String>(classes);
    String classname;
    while ((classname = work.poll()) != null) {
        System.err.println("Checking class " + classname);
        ClassSymbol sym = syms.enterClass(syms.noModule, names.table.fromString(classname));
        sym.complete();

        if ((sym.flags() & Flags.INTERFACE) != 0 && !testInterfaces)
            continue;

        for (Symbol s : sym.members_field.getSymbols(NON_RECURSIVE)) {
            System.err.println("Checking member " + s);
            switch (s.kind) {
                case TYP: {
                    String name = s.flatName().toString();
                    if (!classes.contains(name)) {
                        classes.add(name);
                        work.add(name);
                    }
                    break;
                }
                case MTH:
                    verify((MethodSymbol) s, expectNames);
                    break;
            }

        }
    }
}