Search in sources :

Example 76 with VariableTree

use of com.sun.source.tree.VariableTree in project error-prone by google.

the class EqualsReference method matchMethod.

@Override
public Description matchMethod(MethodTree methodTree, VisitorState visitorState) {
    if (EQUALS_MATCHER.matches(methodTree, visitorState)) {
        VariableTree variableTree = methodTree.getParameters().get(0);
        VarSymbol varSymbol = ASTHelpers.getSymbol(variableTree);
        TreeScannerEquals treeScannerEquals = new TreeScannerEquals(methodTree);
        treeScannerEquals.scan(methodTree.getBody(), varSymbol);
        if (treeScannerEquals.hasIllegalEquals) {
            return describeMatch(methodTree);
        }
    }
    return Description.NO_MATCH;
}
Also used : VariableTree(com.sun.source.tree.VariableTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 77 with VariableTree

use of com.sun.source.tree.VariableTree in project error-prone by google.

the class ChainingConstructorIgnoresParameter method evaluateCallers.

private Description evaluateCallers(MethodSymbol symbol) {
    List<VariableTree> paramTypes = paramTypesForMethod.get(symbol);
    if (paramTypes == null) {
        // We haven't seen the declaration yet. We'll evaluate the call when we do.
        return NO_MATCH;
    }
    for (Caller caller : callersToEvaluate.removeAll(symbol)) {
        VisitorState state = caller.state;
        MethodInvocationTree invocation = caller.tree;
        MethodTree callerConstructor = state.findEnclosing(MethodTree.class);
        if (callerConstructor == null) {
            // impossible, at least in compilable code?
            continue;
        }
        Map<String, Type> availableParams = indexTypeByName(callerConstructor.getParameters());
        /*
       * TODO(cpovirk): Better handling of varargs: If the last parameter type is varargs and it is
       * called as varargs (rather than by passing an array), then rewrite the parameter types to
       * (p0, p1, ..., p[n-2], p[n-1] = element type of varargs parameter if an argument is
       * supplied, p[n] = ditto, etc.). For now, we settle for not crashing in the face of a
       * mismatch between the number of parameters declared and the number supplied.
       *
       * (Use MethodSymbol.isVarArgs.)
       */
        for (int i = 0; i < paramTypes.size() && i < invocation.getArguments().size(); i++) {
            VariableTree formalParam = paramTypes.get(i);
            String formalParamName = formalParam.getName().toString();
            Type formalParamType = getType(formalParam.getType());
            Type availableParamType = availableParams.get(formalParamName);
            ExpressionTree actualParam = invocation.getArguments().get(i);
            if (/*
         * The caller has no param of this type. (Or if it did, we couldn't determine the type.
         * Does that ever happen?) If the param doesn't exist, the caller can't be failing to
         * pass it.
         */
            availableParamType == null || /*
             * We couldn't determine the type of the formal parameter. (Does this ever happen?)
             */
            formalParamType == null || /*
             * The caller is passing the expected parameter (or "ImmutableList.copyOf(parameter),"
             * "new File(parameter)," etc.).
             */
            referencesIdentifierWithName(formalParamName, actualParam, state)) {
                continue;
            }
            if (state.getTypes().isAssignable(availableParamType, formalParamType)) {
                reportMatch(invocation, state, actualParam, formalParamName);
            }
        /*
         * If formal parameter is of an incompatible type, the caller might in theory still intend
         * to pass a dervied expression. For example, "Foo(String file)" might intend to call
         * "Foo(File file)" by passing "new File(file)." If this comes up in practice, we could
         * provide the dummy suggested fix "someExpression(formalParamName)." However, my research
         * suggests that this will rarely if ever be what the user wants.
         */
        }
    }
    // All matches are reported through reportMatch calls instead of return values.
    return NO_MATCH;
}
Also used : ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Type(com.sun.tools.javac.code.Type) VisitorState(com.google.errorprone.VisitorState) MethodTree(com.sun.source.tree.MethodTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 78 with VariableTree

use of com.sun.source.tree.VariableTree in project error-prone by google.

the class HidingField method checkForHiddenFields.

private void checkForHiddenFields(List<VariableTree> originalClassMembers, Map<Name, VarSymbol> parentMembers, Name parentClassName, ClassTree classTree, VisitorState visitorState) {
    Iterator<VariableTree> origVariableIterator = originalClassMembers.iterator();
    VariableTree origVariable = null;
    while (origVariableIterator.hasNext()) {
        origVariable = origVariableIterator.next();
        if (parentMembers.containsKey(origVariable.getName())) {
            if (isPackagePrivateAndInDiffPackage(parentMembers.get(origVariable.getName()), classTree)) {
                continue;
            }
            Builder matchDesc = buildDescription(origVariable);
            matchDesc.setMessage("Hiding fields of superclasses may cause confusion and errors. " + "This field is hiding a field of the same name in superclass: " + parentClassName);
            visitorState.reportMatch(matchDesc.build());
            origVariableIterator.remove();
        }
    }
}
Also used : Builder(com.google.errorprone.matchers.Description.Builder) VariableTree(com.sun.source.tree.VariableTree)

Example 79 with VariableTree

use of com.sun.source.tree.VariableTree in project error-prone by google.

the class HidingField method matchClass.

@Override
public Description matchClass(ClassTree classTree, VisitorState visitorState) {
    List<VariableTree> originalClassMembers = classTree.getMembers().stream().filter(mem -> mem instanceof VariableTree).map(mem -> (VariableTree) mem).filter(mem -> !isSuppressed(ASTHelpers.getSymbol(mem)) && !isIgnoredType(mem) && !isStatic(mem)).collect(toCollection(ArrayList::new));
    ClassSymbol classSymbol = ASTHelpers.getSymbol(classTree);
    while (!Objects.equals(classSymbol.getSuperclass(), Type.noType)) {
        TypeSymbol parentSymbol = classSymbol.getSuperclass().asElement();
        List<Symbol> parentElements = parentSymbol.getEnclosedElements();
        Map<Name, VarSymbol> parentMembers = parentElements.stream().filter(mem -> (mem instanceof VarSymbol)).map(mem -> (VarSymbol) mem).filter(mem -> (!mem.isPrivate() && !mem.getModifiers().contains(Modifier.STATIC))).collect(Collectors.toMap(Symbol::getSimpleName, mem -> mem));
        checkForHiddenFields(originalClassMembers, parentMembers, parentSymbol.getSimpleName(), classTree, visitorState);
        classSymbol = (ClassSymbol) parentSymbol;
    }
    return Description.NO_MATCH;
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) Modifier(javax.lang.model.element.Modifier) VariableTree(com.sun.source.tree.VariableTree) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ArrayList(java.util.ArrayList) Collectors.toCollection(java.util.stream.Collectors.toCollection) VisitorState(com.google.errorprone.VisitorState) Map(java.util.Map) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) ClassTree(com.sun.source.tree.ClassTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Name(javax.lang.model.element.Name) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) Symbol(com.sun.tools.javac.code.Symbol) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Description(com.google.errorprone.matchers.Description) Builder(com.google.errorprone.matchers.Description.Builder) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) VariableTree(com.sun.source.tree.VariableTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Name(javax.lang.model.element.Name)

Example 80 with VariableTree

use of com.sun.source.tree.VariableTree in project ceylon by eclipse.

the class T6993301 method testExceptionParameterCorrectKind.

public void testExceptionParameterCorrectKind() throws IOException {
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
    final JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ct.analyze();
    new TreePathScanner<Void, Void>() {

        @Override
        public Void visitVariable(VariableTree node, Void p) {
            Element el = Trees.instance(ct).getElement(getCurrentPath());
            assertNotNull(el);
            assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
            return super.visitVariable(node, p);
        }
    }.scan(cut, null);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Element(javax.lang.model.element.Element) VariableTree(com.sun.source.tree.VariableTree) JavaCompiler(javax.tools.JavaCompiler)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)86 Tree (com.sun.source.tree.Tree)43 ClassTree (com.sun.source.tree.ClassTree)37 MethodTree (com.sun.source.tree.MethodTree)37 ExpressionTree (com.sun.source.tree.ExpressionTree)34 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)25 NewClassTree (com.sun.source.tree.NewClassTree)23 TreePath (com.sun.source.util.TreePath)23 IdentifierTree (com.sun.source.tree.IdentifierTree)22 JCTree (com.sun.tools.javac.tree.JCTree)21 MemberSelectTree (com.sun.source.tree.MemberSelectTree)19 AssignmentTree (com.sun.source.tree.AssignmentTree)17 ArrayList (java.util.ArrayList)17 BlockTree (com.sun.source.tree.BlockTree)16 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)13 Type (com.sun.tools.javac.code.Type)13 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)12 ReturnTree (com.sun.source.tree.ReturnTree)12 StatementTree (com.sun.source.tree.StatementTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)12