Search in sources :

Example 6 with MethodReferenceTree

use of org.sonar.plugins.java.api.tree.MethodReferenceTree 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 7 with MethodReferenceTree

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

the class ConstantsShouldBeStaticFinalCheck method hasConstantInitializer.

private static boolean hasConstantInitializer(VariableTree variableTree) {
    ExpressionTree init = variableTree.initializer();
    if (init != null) {
        if (ExpressionUtils.skipParentheses(init).is(Tree.Kind.METHOD_REFERENCE)) {
            MethodReferenceTree methodRef = (MethodReferenceTree) ExpressionUtils.skipParentheses(init);
            if (isInstanceIdentifier(methodRef.expression())) {
                return false;
            }
        }
        boolean arrayWithInitializer = true;
        if (init.is(Tree.Kind.NEW_ARRAY)) {
            // exclude allocations : new int[6] but allow initialization new int[]{1,2};
            NewArrayTree newArrayTree = (NewArrayTree) init;
            arrayWithInitializer = newArrayTree.dimensions().isEmpty() || newArrayTree.openBraceToken() != null;
        }
        return arrayWithInitializer && !containsChildrenOfKind((JavaTree) init, Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS);
    }
    return false;
}
Also used : MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Aggregations

MethodReferenceTree (org.sonar.plugins.java.api.tree.MethodReferenceTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)4 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)4 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 Test (org.junit.Test)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 HashSet (java.util.HashSet)1 AbstractTypedTree (org.sonar.java.model.AbstractTypedTree)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)1 NewArrayTree (org.sonar.plugins.java.api.tree.NewArrayTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1