Search in sources :

Example 46 with MemberSelectExpressionTree

use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.

the class SyntaxTreeNameFinder method visitMemberSelectExpression.

@Override
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
    ExpressionTree expression = ExpressionUtils.skipParentheses(tree.expression());
    if (expression.is(Tree.Kind.IDENTIFIER)) {
        String identifierName = ((IdentifierTree) expression).name();
        if ("this".equals(identifierName) || "super".equals(identifierName)) {
            tree.identifier().accept(this);
            return;
        }
    }
    expression.accept(this);
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 47 with MemberSelectExpressionTree

use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.

the class ModulusEqualityCheck method isSizeAccessor.

private static boolean isSizeAccessor(ExpressionTree expressionTree) {
    if (expressionTree.is(Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) expressionTree;
        Type type = memberSelectExpressionTree.expression().symbolType();
        String memberName = memberSelectExpressionTree.identifier().name();
        return isCollectionSize(type, memberName) || isStringLength(type, memberName) || isArrayLength(type, memberName);
    } else if (expressionTree.is(Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expressionTree;
        return isSizeAccessor(methodInvocationTree.methodSelect());
    }
    return false;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree)

Example 48 with MemberSelectExpressionTree

use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.

the class ModulusEqualityCheck method isMethodParameter.

private boolean isMethodParameter(ExpressionTree expressionTree) {
    if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) expressionTree;
        Symbol symbol = identifier.symbol();
        return methodParams.contains(symbol);
    } else if (expressionTree.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) expressionTree;
        return isMethodParameter(memberSelectExpressionTree.expression());
    } else if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expressionTree;
        return isMethodParameter(methodInvocationTree.methodSelect());
    }
    return false;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 49 with MemberSelectExpressionTree

use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.

the class LiveVariables method processBlockElements.

private void processBlockElements(CFG.Block block, Set<Symbol> blockKill, Set<Symbol> blockGen) {
    // process elements from bottom to top
    Set<Tree> assignmentLHS = new HashSet<>();
    for (Tree element : Lists.reverse(block.elements())) {
        switch(element.kind()) {
            case ASSIGNMENT:
                processAssignment((AssignmentExpressionTree) element, blockKill, blockGen, assignmentLHS);
                break;
            case IDENTIFIER:
                processIdentifier((IdentifierTree) element, blockGen, assignmentLHS);
                break;
            case MEMBER_SELECT:
                processMemberSelect((MemberSelectExpressionTree) element, assignmentLHS, blockGen);
                break;
            case VARIABLE:
                blockKill.add(((VariableTree) element).symbol());
                blockGen.remove(((VariableTree) element).symbol());
                break;
            case LAMBDA_EXPRESSION:
                blockGen.addAll(getUsedVariables(((LambdaExpressionTree) element).body(), cfg.methodSymbol()));
                break;
            case METHOD_REFERENCE:
                blockGen.addAll(getUsedVariables(((MethodReferenceTree) element).expression(), cfg.methodSymbol()));
                break;
            case NEW_CLASS:
                blockGen.addAll(getUsedVariables(((NewClassTree) element).classBody(), cfg.methodSymbol()));
                break;
            default:
        }
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) HashSet(java.util.HashSet)

Example 50 with MemberSelectExpressionTree

use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.

the class LiveVariables method processAssignment.

private void processAssignment(AssignmentExpressionTree element, Set<Symbol> blockKill, Set<Symbol> blockGen, Set<Tree> assignmentLHS) {
    Symbol symbol = null;
    ExpressionTree lhs = element.variable();
    if (lhs.is(Kind.IDENTIFIER)) {
        symbol = ((IdentifierTree) lhs).symbol();
    } else if (includeFields && lhs.is(Kind.MEMBER_SELECT)) {
        symbol = getField((MemberSelectExpressionTree) lhs);
    }
    if (symbol != null && includeSymbol(symbol)) {
        assignmentLHS.add(lhs);
        blockGen.remove(symbol);
        blockKill.add(symbol);
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Aggregations

MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)60 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)36 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)36 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 Symbol (org.sonar.plugins.java.api.semantic.Symbol)13 Test (org.junit.Test)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)8 Tree (org.sonar.plugins.java.api.tree.Tree)7 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)5 CheckForNull (javax.annotation.CheckForNull)4 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)4 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)4 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)4 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)4 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 Resolution (org.sonar.java.resolve.Resolve.Resolution)3