Search in sources :

Example 21 with Symbol

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

the class FilesLinesLeak method inTWR.

private boolean inTWR(VisitorState state) {
    TreePath path = state.getPath().getParentPath();
    while (path.getLeaf().getKind() == Tree.Kind.CONDITIONAL_EXPRESSION) {
        path = path.getParentPath();
    }
    Symbol sym = ASTHelpers.getSymbol(path.getLeaf());
    return sym != null && sym.getKind() == ElementKind.RESOURCE_VARIABLE;
}
Also used : TreePath(com.sun.source.util.TreePath) Symbol(com.sun.tools.javac.code.Symbol)

Example 22 with Symbol

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

the class CompileTimeConstantChecker method handleMatch.

/**
   * If the non-constant variable is annotated with @CompileTimeConstant, it must have been
   * non-final. Suggest making it final in the error message.
   */
private Description handleMatch(ExpressionTree actualParam, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(actualParam);
    if (!(sym instanceof VarSymbol)) {
        return describeMatch(actualParam);
    }
    VarSymbol var = (VarSymbol) sym;
    if (!hasCompileTimeConstantAnnotation(state, var)) {
        return describeMatch(actualParam);
    }
    return buildDescription(actualParam).setMessage(this.message() + String.format(DID_YOU_MEAN_FINAL_FMT_MESSAGE, var.getSimpleName())).build();
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 23 with Symbol

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

the class DefaultCharset method variableTypeFix.

private void variableTypeFix(SuggestedFix.Builder fix, VisitorState state, Class<?> original, Class<?> replacement) {
    Tree parent = state.getPath().getParentPath().getLeaf();
    Symbol sym;
    switch(parent.getKind()) {
        case VARIABLE:
            sym = ASTHelpers.getSymbol((VariableTree) parent);
            break;
        case ASSIGNMENT:
            sym = ASTHelpers.getSymbol(((AssignmentTree) parent).getVariable());
            break;
        default:
            return;
    }
    if (!ASTHelpers.isSameType(sym.type, state.getTypeFromString(original.getCanonicalName()), state)) {
        return;
    }
    state.getPath().getCompilationUnit().accept(new TreeScanner<Void, Void>() {

        @Override
        public Void visitVariable(VariableTree node, Void aVoid) {
            if (sym.equals(ASTHelpers.getSymbol(node))) {
                fix.replace(node.getType(), replacement.getSimpleName()).addImport(replacement.getCanonicalName());
            }
            return null;
        }
    }, null);
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) VariableTree(com.sun.source.tree.VariableTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 24 with Symbol

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

the class PreconditionsCheckNotNullPrimitive method hasMethodParameter.

/**
   * Determines whether the expression contains a reference to one of the
   * enclosing method's parameters.
   *
   * TODO(eaftan): Extract this to ASTHelpers.
   *
   * @param path the path to the current tree node
   * @param tree the node to compare against the parameters
   * @return whether the argument is a parameter to the enclosing method
   */
private static boolean hasMethodParameter(TreePath path, ExpressionTree tree) {
    Set<Symbol> symbols = new HashSet<>();
    for (IdentifierTree ident : getVariableUses(tree)) {
        Symbol sym = ASTHelpers.getSymbol(ident);
        if (sym.isLocal()) {
            symbols.add(sym);
        }
    }
    // Find enclosing method declaration.
    while (path != null && !(path.getLeaf() instanceof MethodTree)) {
        path = path.getParentPath();
    }
    if (path == null) {
        throw new IllegalStateException("Should have an enclosing method declaration");
    }
    MethodTree methodDecl = (MethodTree) path.getLeaf();
    for (VariableTree param : methodDecl.getParameters()) {
        if (symbols.contains(ASTHelpers.getSymbol(param))) {
            return true;
        }
    }
    return false;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) VariableTree(com.sun.source.tree.VariableTree) IdentifierTree(com.sun.source.tree.IdentifierTree) HashSet(java.util.HashSet)

Example 25 with Symbol

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

the class RemoveUnusedImports method matchCompilationUnit.

@Override
public Description matchCompilationUnit(CompilationUnitTree compilationUnitTree, VisitorState state) {
    final ImmutableSetMultimap<ImportTree, Symbol> importedSymbols = getImportedSymbols(compilationUnitTree, state);
    if (importedSymbols.isEmpty()) {
        return NO_MATCH;
    }
    final Set<ImportTree> unusedImports = new HashSet<>(importedSymbols.keySet());
    new TreeSymbolScanner(JavacTrees.instance(state.context), state.getTypes()).scan(compilationUnitTree, new SymbolSink() {

        @Override
        public boolean keepScanning() {
            return !unusedImports.isEmpty();
        }

        @Override
        public void accept(Symbol symbol) {
            unusedImports.removeAll(importedSymbols.inverse().get(symbol));
        }
    });
    if (unusedImports.isEmpty()) {
        return NO_MATCH;
    }
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    for (ImportTree unusedImport : unusedImports) {
        fixBuilder.delete(unusedImport);
    }
    return describeMatch(unusedImports.iterator().next(), fixBuilder.build());
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) ImportTree(com.sun.source.tree.ImportTree) HashSet(java.util.HashSet)

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