Search in sources :

Example 46 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project checker-framework by typetools.

the class AnnotationUtils method getElementValueEnum.

/**
 * Get the element with the name {@code name} of the annotation {@code anno}. The result is an
 * enum of type {@code T}.
 *
 * <p>This method is intended only for use by the framework. A checker implementation should use
 * {@link #getElementValueEnum(AnnotationMirror, ExecutableElement, Class)} or {@link
 * #getElementValueEnum(AnnotationMirror, ExecutableElement, Class, Enum)}.
 *
 * @param anno the annotation to disassemble
 * @param elementName the name of the element to access
 * @param expectedType the type of the element and the return value, an enum
 * @param <T> the class of the type
 * @param useDefaults whether to apply default values to the element
 * @return the value of the element with the given name
 * @deprecated use {@link #getElementValueEnum(AnnotationMirror, ExecutableElement, Class)} or
 *     {@link #getElementValueEnum(AnnotationMirror, ExecutableElement, Class, Enum)}
 */
// 2021-03-29
@Deprecated
public static <T extends Enum<T>> T getElementValueEnum(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) {
    VarSymbol vs = getElementValue(anno, elementName, VarSymbol.class, useDefaults);
    T value = Enum.valueOf(expectedType, vs.getSimpleName().toString());
    return value;
}
Also used : VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 47 with VarSymbol

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

the class StringSplitter method buildFix.

public Optional<Fix> buildFix(MethodInvocationTree tree, VisitorState state) {
    Tree arg = getOnlyElement(tree.getArguments());
    String value = ASTHelpers.constValue(arg, String.class);
    boolean maybeRegex = false;
    if (value != null) {
        Optional<String> regexAsLiteral = convertRegexToLiteral(value);
        if (regexAsLiteral.isPresent()) {
            value = SourceCodeEscapers.javaCharEscaper().escape(regexAsLiteral.get());
            if (value.length() == 1) {
                value = String.format("'%s'", value.charAt(0));
            } else {
                value = String.format("\"%s\"", value);
            }
        } else {
            maybeRegex = true;
            value = state.getSourceForNode(arg);
        }
    } else {
        value = state.getSourceForNode(arg);
    }
    Tree parent = state.getPath().getParentPath().getLeaf();
    if (parent instanceof EnhancedForLoopTree && ((EnhancedForLoopTree) parent).getExpression().equals(tree)) {
        // fix for `for (... : s.split(...)) {}` -> `for (... : Splitter.on(...).split(s)) {}`
        return Optional.of(replaceWithSplitter(SuggestedFix.builder(), tree, value, state, "split", maybeRegex, /* mutableList= */
        false).build());
    }
    if (parent instanceof ArrayAccessTree) {
        ArrayAccessTree arrayAccessTree = (ArrayAccessTree) parent;
        if (!arrayAccessTree.getExpression().equals(tree)) {
            return Optional.empty();
        }
        SuggestedFix.Builder fix = SuggestedFix.builder().addImport("com.google.common.collect.Iterables").replace(((JCTree) arrayAccessTree).getStartPosition(), ((JCTree) arrayAccessTree).getStartPosition(), "Iterables.get(").replace(/* startPos= */
        state.getEndPosition(arrayAccessTree.getExpression()), /* endPos= */
        ((JCTree) arrayAccessTree.getIndex()).getStartPosition(), String.format(", ")).replace(state.getEndPosition(arrayAccessTree.getIndex()), state.getEndPosition(arrayAccessTree), ")");
        return Optional.of(replaceWithSplitter(fix, tree, value, state, "split", maybeRegex, /* mutableList= */
        false).build());
    }
    // enclosing method. If we don't know how to fix any of them, bail out.
    if (!(parent instanceof VariableTree)) {
        return Optional.empty();
    }
    VariableTree varTree = (VariableTree) parent;
    if (!varTree.getInitializer().equals(tree)) {
        return Optional.empty();
    }
    VarSymbol sym = ASTHelpers.getSymbol(varTree);
    TreePath enclosing = findEnclosing(state);
    if (enclosing == null) {
        return Optional.empty();
    }
    // find all uses of the variable in the enclosing method
    List<TreePath> uses = new ArrayList<>();
    new TreePathScanner<Void, Void>() {

        @Override
        public Void visitIdentifier(IdentifierTree tree, Void unused) {
            if (Objects.equals(sym, ASTHelpers.getSymbol(tree))) {
                uses.add(getCurrentPath());
            }
            return super.visitIdentifier(tree, null);
        }
    }.scan(enclosing, null);
    SuggestedFix.Builder fix = SuggestedFix.builder();
    // a mutable boolean to track whether we want split or splitToList
    boolean[] needsList = { false };
    boolean[] needsMutableList = { false };
    // try to fix all uses of the variable
    for (TreePath path : uses) {
        class UseFixer extends TreePathScanner<Boolean, Void> {

            @Override
            public Boolean visitEnhancedForLoop(EnhancedForLoopTree tree, Void unused) {
                // fix here.
                return sym.equals(ASTHelpers.getSymbol(tree.getExpression()));
            }

            @Override
            public Boolean visitArrayAccess(ArrayAccessTree tree, Void unused) {
                // replace `pieces[N]` with `pieces.get(N)`
                ExpressionTree expression = tree.getExpression();
                ExpressionTree index = tree.getIndex();
                if (!sym.equals(ASTHelpers.getSymbol(expression))) {
                    return false;
                }
                Tree parent = getCurrentPath().getParentPath().getLeaf();
                if (parent instanceof AssignmentTree && ((AssignmentTree) parent).getVariable() == tree) {
                    AssignmentTree assignmentTree = (AssignmentTree) parent;
                    fix.replace(/* startPos= */
                    state.getEndPosition(expression), /* endPos= */
                    ((JCTree) index).getStartPosition(), ".set(").replace(/* startPos= */
                    state.getEndPosition(index), /* endPos= */
                    ((JCTree) assignmentTree.getExpression()).getStartPosition(), ", ").postfixWith(assignmentTree, ")");
                    needsMutableList[0] = true;
                } else {
                    fix.replace(/* startPos= */
                    state.getEndPosition(expression), /* endPos= */
                    ((JCTree) index).getStartPosition(), ".get(").replace(state.getEndPosition(index), state.getEndPosition(tree), ")");
                }
                // we want a list for indexing
                needsList[0] = true;
                return true;
            }

            @Override
            public Boolean visitMemberSelect(MemberSelectTree tree, Void aVoid) {
                // replace `pieces.length` with `pieces.size`
                if (sym.equals(ASTHelpers.getSymbol(tree.getExpression())) && tree.getIdentifier().contentEquals("length")) {
                    fix.replace(state.getEndPosition(tree.getExpression()), state.getEndPosition(tree), ".size()");
                    needsList[0] = true;
                    return true;
                }
                return false;
            }
        }
        if (!firstNonNull(new UseFixer().scan(path.getParentPath(), null), false)) {
            return Optional.empty();
        }
    }
    if (needsList[0]) {
        fix.replace((varTree).getType(), "List<String>").addImport("java.util.List");
        replaceWithSplitter(fix, tree, value, state, "splitToList", maybeRegex, needsMutableList[0]);
    } else {
        fix.replace((varTree).getType(), "Iterable<String>");
        replaceWithSplitter(fix, tree, value, state, "split", maybeRegex, needsMutableList[0]);
    }
    return Optional.of(fix.build());
}
Also used : ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) IdentifierTree(com.sun.source.tree.IdentifierTree) TreePathScanner(com.sun.source.util.TreePathScanner) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TreePath(com.sun.source.util.TreePath) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree)

Example 48 with VarSymbol

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

the class FindIdentifiers method findAllIdents.

/**
 * Finds the set of all bare variable identifiers in scope at the current location. Identifiers
 * are ordered by ascending distance/scope count from the current location to match shadowing
 * rules. That is, if two variables with the same simple names appear in the set, the one that
 * appears first in iteration order is the one you get if you use the bare name in the source
 * code.
 *
 * <p>We do not report variables that would require a qualfied access. We also do not handle
 * wildcard imports.
 */
public static LinkedHashSet<VarSymbol> findAllIdents(VisitorState state) {
    ImmutableSet.Builder<VarSymbol> result = new ImmutableSet.Builder<>();
    Tree prev = state.getPath().getLeaf();
    for (Tree curr : state.getPath().getParentPath()) {
        switch(curr.getKind()) {
            case BLOCK:
                for (StatementTree stmt : ((BlockTree) curr).getStatements()) {
                    if (stmt.equals(prev)) {
                        break;
                    }
                    addIfVariable(stmt, result);
                }
                break;
            case METHOD:
                for (VariableTree param : ((MethodTree) curr).getParameters()) {
                    result.add(ASTHelpers.getSymbol(param));
                }
                break;
            case CATCH:
                result.add(ASTHelpers.getSymbol(((CatchTree) curr).getParameter()));
                break;
            case CLASS:
            case INTERFACE:
            case ENUM:
            case ANNOTATION_TYPE:
                // field is referred to by qualified name, but we don't support that.
                for (Tree member : ((ClassTree) curr).getMembers()) {
                    if (member.equals(prev)) {
                        break;
                    }
                    addIfVariable(member, result);
                }
                // Collect inherited fields.
                Type classType = ASTHelpers.getType(curr);
                List<Type> classTypeClosure = state.getTypes().closure(classType);
                List<Type> superTypes = classTypeClosure.size() <= 1 ? Collections.emptyList() : classTypeClosure.subList(1, classTypeClosure.size());
                for (Type type : superTypes) {
                    Scope scope = type.tsym.members();
                    ImmutableList.Builder<VarSymbol> varsList = ImmutableList.builder();
                    for (Symbol var : scope.getSymbols(VarSymbol.class::isInstance)) {
                        varsList.add((VarSymbol) var);
                    }
                    result.addAll(varsList.build().reverse());
                }
                break;
            case FOR_LOOP:
                addAllIfVariable(((ForLoopTree) curr).getInitializer(), result);
                break;
            case ENHANCED_FOR_LOOP:
                result.add(ASTHelpers.getSymbol(((EnhancedForLoopTree) curr).getVariable()));
                break;
            case TRY:
                TryTree tryTree = (TryTree) curr;
                boolean inResources = false;
                for (Tree resource : tryTree.getResources()) {
                    if (resource.equals(prev)) {
                        inResources = true;
                        break;
                    }
                }
                if (inResources) {
                    // Case 1: we're in one of the resource declarations
                    for (Tree resource : tryTree.getResources()) {
                        if (resource.equals(prev)) {
                            break;
                        }
                        addIfVariable(resource, result);
                    }
                } else if (tryTree.getBlock().equals(prev)) {
                    // Case 2: We're in the block (not a catch or finally)
                    addAllIfVariable(tryTree.getResources(), result);
                }
                break;
            case COMPILATION_UNIT:
                for (ImportTree importTree : ((CompilationUnitTree) curr).getImports()) {
                    if (importTree.isStatic() && importTree.getQualifiedIdentifier().getKind() == Kind.MEMBER_SELECT) {
                        MemberSelectTree memberSelectTree = (MemberSelectTree) importTree.getQualifiedIdentifier();
                        Scope scope = state.getTypes().membersClosure(ASTHelpers.getType(memberSelectTree.getExpression()), /* skipInterface= */
                        false);
                        for (Symbol var : scope.getSymbols(sym -> sym instanceof VarSymbol && sym.getSimpleName().equals(memberSelectTree.getIdentifier()))) {
                            result.add((VarSymbol) var);
                        }
                    }
                }
                break;
            default:
                // other node types don't introduce variables
                break;
        }
        prev = curr;
    }
    // TODO(eaftan): switch out collector for ImmutableSet.toImmutableSet()
    return result.build().stream().filter(var -> isVisible(var, state.getPath())).collect(Collectors.toCollection(LinkedHashSet::new));
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) Modifier(javax.lang.model.element.Modifier) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) ForLoopTree(com.sun.source.tree.ForLoopTree) Method(java.lang.reflect.Method) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) TreePath(com.sun.source.util.TreePath) ImmutableSet(com.google.common.collect.ImmutableSet) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) MemberSelectTree(com.sun.source.tree.MemberSelectTree) Env(com.sun.tools.javac.comp.Env) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) TreeScanner(com.sun.source.util.TreeScanner) Objects(java.util.Objects) List(java.util.List) WriteableScope(com.sun.tools.javac.code.Scope.WriteableScope) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) Flags(com.sun.tools.javac.code.Flags) Name(com.sun.tools.javac.util.Name) Scope(com.sun.tools.javac.code.Scope) Type(com.sun.tools.javac.code.Type) MethodTree(com.sun.source.tree.MethodTree) MemberEnter(com.sun.tools.javac.comp.MemberEnter) VariableTree(com.sun.source.tree.VariableTree) AttrContext(com.sun.tools.javac.comp.AttrContext) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) BiPredicate(java.util.function.BiPredicate) Kind(com.sun.source.tree.Tree.Kind) ImmutableList(com.google.common.collect.ImmutableList) NewClassTree(com.sun.source.tree.NewClassTree) StreamSupport(java.util.stream.StreamSupport) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Enter(com.sun.tools.javac.comp.Enter) ElementKind(javax.lang.model.element.ElementKind) KindSelector(com.sun.tools.javac.code.Kinds.KindSelector) TryTree(com.sun.source.tree.TryTree) Collections(java.util.Collections) Resolve(com.sun.tools.javac.comp.Resolve) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) CatchTree(com.sun.source.tree.CatchTree) ImmutableList(com.google.common.collect.ImmutableList) 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) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) StatementTree(com.sun.source.tree.StatementTree) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ImmutableSet(com.google.common.collect.ImmutableSet) WriteableScope(com.sun.tools.javac.code.Scope.WriteableScope) Scope(com.sun.tools.javac.code.Scope) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) ForLoopTree(com.sun.source.tree.ForLoopTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) TryTree(com.sun.source.tree.TryTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) TryTree(com.sun.source.tree.TryTree) ImportTree(com.sun.source.tree.ImportTree)

Example 49 with VarSymbol

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

the class ASTHelpers method enumValues.

/**
 * @return all values of the given enum type, in declaration order.
 */
public static LinkedHashSet<String> enumValues(TypeSymbol enumType) {
    if (enumType.getKind() != ElementKind.ENUM) {
        throw new IllegalStateException();
    }
    Scope scope = enumType.members();
    Deque<String> values = new ArrayDeque<>();
    for (Symbol sym : scope.getSymbols()) {
        if (sym instanceof VarSymbol) {
            VarSymbol var = (VarSymbol) sym;
            if ((var.flags() & Flags.ENUM) != 0) {
                /**
                 * Javac gives us the members backwards, apparently. It's worth making an effort to
                 * preserve declaration order because it's useful for diagnostics (e.g. in {@link
                 * MissingCasesInEnumSwitch}).
                 */
                values.push(sym.name.toString());
            }
        }
    }
    return new LinkedHashSet<>(values);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Scope(com.sun.tools.javac.code.Scope) 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) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ArrayDeque(java.util.ArrayDeque)

Example 50 with VarSymbol

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

the class AbstractToString method checkToString.

/**
 * Tests if the given expression is converted to a String by its parent (i.e. its parent is a
 * string concat expression, {@code String.format}, or {@code println(Object)}).
 */
private Description checkToString(ExpressionTree tree, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (!(sym instanceof VarSymbol || sym instanceof MethodSymbol)) {
        return NO_MATCH;
    }
    Type type = ASTHelpers.getType(tree);
    if (type instanceof MethodType) {
        type = type.getReturnType();
    }
    Tree parent = state.getPath().getParentPath().getLeaf();
    ToStringKind toStringKind = isToString(parent, tree, state);
    if (toStringKind == ToStringKind.NONE) {
        return NO_MATCH;
    }
    Optional<Fix> fix;
    switch(toStringKind) {
        case IMPLICIT:
            fix = implicitToStringFix(tree, state);
            break;
        case EXPLICIT:
            fix = toStringFix(parent, tree, state);
            break;
        default:
            throw new AssertionError(toStringKind);
    }
    if (!typePredicate().apply(type, state)) {
        return NO_MATCH;
    }
    return maybeFix(tree, fix);
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Fix(com.google.errorprone.fixes.Fix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Aggregations

VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)68 Symbol (com.sun.tools.javac.code.Symbol)29 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)23 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)22 Type (com.sun.tools.javac.code.Type)20 ExpressionTree (com.sun.source.tree.ExpressionTree)15 Tree (com.sun.source.tree.Tree)15 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)11 VariableTree (com.sun.source.tree.VariableTree)11 ClassTree (com.sun.source.tree.ClassTree)10 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MemberSelectTree (com.sun.source.tree.MemberSelectTree)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)10 NewClassTree (com.sun.source.tree.NewClassTree)9 ArrayList (java.util.ArrayList)9 VisitorState (com.google.errorprone.VisitorState)7 MethodTree (com.sun.source.tree.MethodTree)7 TreePath (com.sun.source.util.TreePath)7 ClassType (com.sun.tools.javac.code.Type.ClassType)7