Search in sources :

Example 6 with PackageSymbol

use of com.sun.tools.javac.code.Symbol.PackageSymbol in project error-prone by google.

the class JavaLangClash method check.

private Description check(Tree tree, Name simpleName, VisitorState state) {
    Symtab symtab = state.getSymtab();
    PackageSymbol javaLang = symtab.enterPackage(symtab.java_base, Names.instance(state.context).java_lang);
    Symbol other = getFirst(javaLang.members().getSymbolsByName(simpleName, s -> s.getModifiers().contains(PUBLIC)), null);
    Symbol symbol = ASTHelpers.getSymbol(tree);
    if (other == null || other.equals(symbol)) {
        return NO_MATCH;
    }
    return buildDescription(tree).setMessage(String.format("%s clashes with %s\n", symbol, other)).build();
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) Symtab(com.sun.tools.javac.code.Symtab) ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) PUBLIC(javax.lang.model.element.Modifier.PUBLIC) Symbol(com.sun.tools.javac.code.Symbol) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) Names(com.sun.tools.javac.util.Names) TypeParameterTree(com.sun.source.tree.TypeParameterTree) Iterables.getFirst(com.google.common.collect.Iterables.getFirst) VisitorState(com.google.errorprone.VisitorState) TypeParameterTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.TypeParameterTreeMatcher) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) Description(com.google.errorprone.matchers.Description) StandardTags(com.google.errorprone.BugPattern.StandardTags) BugPattern(com.google.errorprone.BugPattern) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) JDK(com.google.errorprone.BugPattern.Category.JDK) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Tree(com.sun.source.tree.Tree) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) Name(com.sun.tools.javac.util.Name) ASTHelpers(com.google.errorprone.util.ASTHelpers) ClassTree(com.sun.source.tree.ClassTree) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol)

Example 7 with PackageSymbol

use of com.sun.tools.javac.code.Symbol.PackageSymbol in project ceylon-compiler by ceylon.

the class CeylonEnter method resetAndRunEnterAgain.

private void resetAndRunEnterAgain(List<JCCompilationUnit> trees) {
    timer.startTask("Resetting all trees for bootstrap");
    // get rid of some caches and state
    chk.compiled.clear();
    types.reset();
    annotate.reset();
    super.reset();
    // reset all class symbols
    for (ClassSymbol classSymbol : symtab.classes.values()) {
        if (Util.isLoadedFromSource(classSymbol) || (classSymbol.sourcefile != null && classSymbol.sourcefile.getKind() == Kind.SOURCE)) {
            PackageSymbol pkg = classSymbol.packge();
            String name = pkg.getQualifiedName().toString();
            if (name.startsWith(AbstractModelLoader.CEYLON_LANGUAGE) || name.startsWith("com.redhat.ceylon.compiler.java"))
                resetClassSymbol(classSymbol);
        }
    }
    // reset the trees
    JCTypeResetter jcTypeResetter = new JCTypeResetter();
    for (JCCompilationUnit tree : trees) {
        tree.accept(jcTypeResetter);
    }
    // and reset the list of things to compile, because we got rid of the Env key we used to look them up
    // so they'd appear as extra things to compile when we do Enter
    todo.reset();
    timer.endTask();
    timer.startTask("Enter on Java+Ceylon trees");
    // now do Enter on all the java+ceylon code
    super.main(trees);
    timer.endTask();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Example 8 with PackageSymbol

use of com.sun.tools.javac.code.Symbol.PackageSymbol in project ceylon-compiler by ceylon.

the class CeylonModelLoader method lookupNewClassMirror.

private ClassMirror lookupNewClassMirror(String name) {
    ClassSymbol classSymbol = null;
    String outerName = name;
    /*
         * This madness here tries to look for a class, and if it fails, tries to resolve it 
         * from its parent class. This is required because a.b.C.D (where D is an inner class
         * of C) is not found in symtab.classes but in C's ClassSymbol.enclosedElements.
         */
    // make sure we load the class file, since we no longer complete packages unless we absolutely must
    loadClass(outerName);
    do {
        // we must first try with no postfix, because we can have a valid class foo.bar in Java,
        // when in Ceylon it would be foo.bar_
        classSymbol = symtab.classes.get(names.fromString(Util.quoteJavaKeywords(outerName)));
        // try again with a postfix "_"
        if (classSymbol == null && lastPartHasLowerInitial(outerName) && !outerName.endsWith("_")) {
            classSymbol = symtab.classes.get(names.fromString(Util.quoteJavaKeywords(outerName + "_")));
        }
        if (classSymbol != null) {
            // if we got a source symbol for something non-Java it's a slipery slope
            if (Util.isLoadedFromSource(classSymbol) && !Util.isJavaSource(classSymbol))
                return null;
            if (outerName.length() != name.length()) {
                try {
                    classSymbol = lookupInnerClass(classSymbol, name.substring(outerName.length() + 1).split("\\."));
                } catch (CompletionFailure x) {
                    // something wrong, we will report it properly elsewhere
                    classSymbol = null;
                }
            }
            if (classSymbol != null && classSymbol.classfile == null && classSymbol.sourcefile == null) {
                // try to complete it if that changes anything
                try {
                    classSymbol.complete();
                } catch (CompletionFailure x) {
                // if we can't complete it it doesn't exist, its classfile will remain null
                }
                if (classSymbol.classfile == null) {
                    PackageSymbol pkg = classSymbol.packge();
                    // do not log an error for missing oracle jdk stuff
                    if (pkg == null || !JDKUtils.isOracleJDKAnyPackage(pkg.getQualifiedName().toString())) {
                        // do not log an error because it will be logged elsewhere
                        logVerbose("Unable to find required class file for " + name);
                    }
                    return null;
                }
            }
            return classSymbol != null ? new JavacClass(classSymbol) : null;
        }
        int lastDot = outerName.lastIndexOf(".");
        if (lastDot == -1 || lastDot == 0)
            return null;
        outerName = outerName.substring(0, lastDot);
    } while (classSymbol == null);
    return null;
}
Also used : PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) JavacClass(com.redhat.ceylon.compiler.java.loader.mirror.JavacClass) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure)

Example 9 with PackageSymbol

use of com.sun.tools.javac.code.Symbol.PackageSymbol in project ceylon-compiler by ceylon.

the class CeylonModelLoader method loadPackage.

@Override
public boolean loadPackage(Module module, String packageName, boolean loadDeclarations) {
    synchronized (getLock()) {
        // abort if we already loaded it, but only record that we loaded it if we want
        // to load the declarations, because merely calling complete() on the package
        // is OK
        packageName = Util.quoteJavaKeywords(packageName);
        String cacheKey = cacheKeyByModule(module, packageName);
        if (loadDeclarations) {
            if (!loadedPackages.add(cacheKey)) {
                return true;
            }
        } else {
            Boolean exists = packageExistence.get(cacheKey);
            if (exists != null)
                return exists.booleanValue();
        }
        PackageSymbol ceylonPkg = packageName.equals("") ? syms().unnamedPackage : reader.enterPackage(names.fromString(packageName));
        if (loadDeclarations) {
            logVerbose("load package " + packageName + " full");
            ceylonPkg.complete();
            /*
                 * Eventually this will go away as we get a hook from the typechecker to load on demand, but
                 * for now the typechecker requires at least ceylon.language to be loaded 
                 */
            for (Symbol m : ceylonPkg.members().getElements()) {
                // skip things that are not classes (perhaps package-info?)
                if (!(m instanceof ClassSymbol))
                    continue;
                ClassSymbol enclosingClass = getEnclosing((ClassSymbol) m);
                if (enclosingClass == m && !Util.isLoadedFromSource(enclosingClass)) {
                    m.complete();
                    // avoid anonymous and local classes
                    if (isAnonymousOrLocal((ClassSymbol) m))
                        continue;
                    // avoid member classes
                    if (((ClassSymbol) m).getNestingKind() != NestingKind.TOP_LEVEL)
                        continue;
                    // skip module and package descriptors
                    if (isModuleOrPackageDescriptorName(m.name.toString()))
                        continue;
                    ClassMirror classMirror = lookupClassMirror(module, m.getQualifiedName().toString());
                    // So ATM we just avoid it, presumably we don't support what it does anyways
                    if (classMirror != null)
                        convertToDeclaration(module, classMirror, DeclarationType.VALUE);
                }
            }
            if (module.getNameAsString().equals(JAVA_BASE_MODULE_NAME) && packageName.equals("java.lang"))
                loadJavaBaseArrays();
            // might be too late
            return ceylonPkg.members().getElements().iterator().hasNext();
        } else {
            logVerbose("load package " + packageName + " light");
            try {
                // it is cheaper to verify that we have a class file somewhere than to complete the whole package
                // just to check for its existence
                Iterable<JavaFileObject> list = fileManager.list(PLATFORM_CLASS_PATH, packageName, EnumSet.of(JavaFileObject.Kind.CLASS), false);
                if (list.iterator().hasNext()) {
                    packageExistence.put(cacheKey, Boolean.TRUE);
                    return true;
                }
                list = fileManager.list(CLASS_PATH, packageName, EnumSet.of(JavaFileObject.Kind.CLASS), false);
                if (list.iterator().hasNext()) {
                    packageExistence.put(cacheKey, Boolean.TRUE);
                    return true;
                } else {
                    packageExistence.put(cacheKey, Boolean.FALSE);
                    return false;
                }
            } catch (IOException e) {
                return false;
            }
        }
    }
}
Also used : PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) JavaFileObject(javax.tools.JavaFileObject) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) IOException(java.io.IOException) ClassMirror(com.redhat.ceylon.model.loader.mirror.ClassMirror)

Aggregations

PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)9 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 Symbol (com.sun.tools.javac.code.Symbol)3 VisitorState (com.google.errorprone.VisitorState)2 ClassTree (com.sun.source.tree.ClassTree)2 Tree (com.sun.source.tree.Tree)2 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)2 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)2 AttrContext (com.sun.tools.javac.comp.AttrContext)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getFirst (com.google.common.collect.Iterables.getFirst)1 Sets (com.google.common.collect.Sets)1 PackageDeclaration (com.google.devtools.j2objc.ast.PackageDeclaration)1 BugPattern (com.google.errorprone.BugPattern)1 JDK (com.google.errorprone.BugPattern.Category.JDK)1 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)1 StandardTags (com.google.errorprone.BugPattern.StandardTags)1 ClassTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher)1 TypeParameterTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.TypeParameterTreeMatcher)1