Search in sources :

Example 16 with Symbol

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

the class GuardedBySymbolResolver method resolveTypeLiteral.

@Override
public Symbol resolveTypeLiteral(ExpressionTree expr) {
    checkGuardedBy(expr instanceof IdentifierTree, "bad type literal: %s", expr);
    IdentifierTree ident = (IdentifierTree) expr;
    Symbol type = resolveType(ident.getName().toString(), SearchSuperTypes.YES);
    if (type instanceof Symbol.ClassSymbol) {
        return type;
    }
    return null;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 17 with Symbol

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

the class ImmutableAnalysis method areFieldsImmutable.

/**
   * Check a single class' fields for immutability.
   *
   * @param immutableTyParams the in-scope immutable type parameters
   * @param classType the type to check the fields of
   */
Violation areFieldsImmutable(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType classType) {
    ClassSymbol classSym = (ClassSymbol) classType.tsym;
    if (classSym.members() == null) {
        return Violation.absent();
    }
    Filter<Symbol> instanceFieldFilter = new Filter<Symbol>() {

        @Override
        public boolean accepts(Symbol symbol) {
            return symbol.getKind() == ElementKind.FIELD && !symbol.isStatic();
        }
    };
    Map<Symbol, Tree> declarations = new HashMap<>();
    if (tree.isPresent()) {
        for (Tree member : tree.get().getMembers()) {
            Symbol sym = ASTHelpers.getSymbol(member);
            if (sym != null) {
                declarations.put(sym, member);
            }
        }
    }
    // javac gives us members in reverse declaration order
    // handling them in declaration order leads to marginally better diagnostics
    List<Symbol> members = ImmutableList.copyOf(classSym.members().getSymbols(instanceFieldFilter)).reverse();
    for (Symbol member : members) {
        Optional<Tree> memberTree = Optional.fromNullable(declarations.get(member));
        Violation info = isFieldImmutable(memberTree, immutableTyParams, classSym, classType, (VarSymbol) member);
        if (info.isPresent()) {
            return info;
        }
    }
    return Violation.absent();
}
Also used : Filter(com.sun.tools.javac.util.Filter) HashMap(java.util.HashMap) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 18 with Symbol

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

the class WildcardImport method qualifiedNameFix.

/**
   * Add an import for {@code owner}, and qualify all on demand imported references to members of
   * owner by owner's simple name.
   */
private static void qualifiedNameFix(final SuggestedFix.Builder fix, final Symbol owner, VisitorState state) {
    fix.addImport(owner.getQualifiedName().toString());
    final JCCompilationUnit unit = (JCCompilationUnit) state.getPath().getCompilationUnit();
    new TreePathScanner<Void, Void>() {

        @Override
        public Void visitIdentifier(IdentifierTree tree, Void unused) {
            Symbol sym = ASTHelpers.getSymbol(tree);
            if (sym == null) {
                return null;
            }
            Tree parent = getCurrentPath().getParentPath().getLeaf();
            if (parent.getKind() == Tree.Kind.CASE && ((CaseTree) parent).getExpression().equals(tree) && sym.owner.getKind() == ElementKind.ENUM) {
                // switch cases can refer to enum constants by simple name without importing them
                return null;
            }
            if (sym.owner.equals(owner) && unit.starImportScope.includes(sym)) {
                fix.prefixWith(tree, owner.getSimpleName() + ".");
            }
            return null;
        }
    }.scan(unit, null);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CaseTree(com.sun.source.tree.CaseTree) Symbol(com.sun.tools.javac.code.Symbol) IdentifierTree(com.sun.source.tree.IdentifierTree) IdentifierTree(com.sun.source.tree.IdentifierTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) CaseTree(com.sun.source.tree.CaseTree) JCTree(com.sun.tools.javac.tree.JCTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree)

Example 19 with Symbol

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

the class WrongParameterPackage method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol method = ASTHelpers.getSymbol(tree);
    if (method == null) {
        return Description.NO_MATCH;
    }
    ClassSymbol classSym = method.enclClass();
    if (classSym == null) {
        return Description.NO_MATCH;
    }
    TypeSymbol superClass = classSym.getSuperclass().tsym;
    if (superClass == null) {
        return Description.NO_MATCH;
    }
    for (Symbol s : superClass.members().getSymbols()) {
        if (s.name.contentEquals(method.name) && s.getKind() == ElementKind.METHOD) {
            MethodSymbol supermethod = (MethodSymbol) s;
            // if this method actually overrides the supermethod, then it's correct and not a match.
            if (method.overrides(supermethod, superClass, state.getTypes(), true)) {
                return Description.NO_MATCH;
            }
            // if this doesn't have the right number of parameters, look at other ones.
            if (supermethod.params().size() != method.params().size()) {
                continue;
            }
            for (int x = 0; x < method.params().size(); x++) {
                Type methodParamType = method.params().get(x).type;
                Type supermethodParamType = supermethod.params().get(x).type;
                if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) && !state.getTypes().isSameType(methodParamType, supermethodParamType)) {
                    this.supermethod = supermethod;
                    return describe(tree, state);
                }
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 20 with Symbol

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

the class ASTHelpersTest method testHasDirectAnnotationWithSimpleName.

@Test
public void testHasDirectAnnotationWithSimpleName() {
    writeFile(//
    "A.java", "public class A {", "  @Deprecated public void doIt() {}", "}");
    TestScanner scanner = new TestScanner() {

        @Override
        public Void visitMethod(MethodTree tree, VisitorState state) {
            if (tree.getName().contentEquals("doIt")) {
                setAssertionsComplete();
                Symbol sym = ASTHelpers.getSymbol(tree);
                assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Deprecated")).isTrue();
                assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Nullable")).isFalse();
            }
            return super.visitMethod(tree, state);
        }
    };
    tests.add(scanner);
    assertCompiles(scanner);
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VisitorState(com.google.errorprone.VisitorState) Symbol(com.sun.tools.javac.code.Symbol) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest) Test(org.junit.Test)

Aggregations

Symbol (com.sun.tools.javac.code.Symbol)195 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)56 Type (com.sun.tools.javac.code.Type)54 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)53 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)36 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)29 JCTree (com.sun.tools.javac.tree.JCTree)28 ClassType (com.sun.tools.javac.code.Type.ClassType)18 Tree (com.sun.source.tree.Tree)17 ExpressionTree (com.sun.source.tree.ExpressionTree)15 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)15 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)15 ClassTree (com.sun.source.tree.ClassTree)14 MethodTree (com.sun.source.tree.MethodTree)14 Name (com.sun.tools.javac.util.Name)14 IdentifierTree (com.sun.source.tree.IdentifierTree)13 ArrayType (com.sun.tools.javac.code.Type.ArrayType)12 MethodType (com.sun.tools.javac.code.Type.MethodType)12 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)12