Search in sources :

Example 31 with NewClassTree

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

the class ImmediateReverseBoxingCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
        visitMethodInvocationTree((MethodInvocationTree) tree);
    } else if (tree.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) tree;
        ExpressionTree initializer = variableTree.initializer();
        if (initializer != null) {
            checkExpression(initializer, variableTree.type().symbolType());
        }
    } else if (tree.is(Tree.Kind.ASSIGNMENT)) {
        AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) tree;
        checkExpression(assignmentTree.expression(), assignmentTree.symbolType());
    } else {
        NewClassTree newClassTree = (NewClassTree) tree;
        Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
        if (classSymbol != null) {
            ExpressionTree arg0 = newClassTree.arguments().get(0);
            checkForUnboxing(arg0);
            checkForUselessUnboxing(newClassTree.symbolType(), newClassTree.identifier(), arg0);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 32 with NewClassTree

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

the class ImmediateReverseBoxingCheck method checkForBoxing.

private void checkForBoxing(ExpressionTree expression) {
    if (expression.is(Tree.Kind.NEW_CLASS)) {
        NewClassTree newClassTree = (NewClassTree) expression;
        Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
        if (classSymbol != null) {
            ExpressionTree boxingArg = newClassTree.arguments().get(0);
            if (boxingArg.symbolType().isPrimitive()) {
                addBoxingIssue(newClassTree, classSymbol, boxingArg);
            }
        }
    } else if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expression;
        if (isValueOfInvocation(methodInvocationTree)) {
            ExpressionTree boxingArg = methodInvocationTree.arguments().get(0);
            addBoxingIssue(expression, methodInvocationTree.symbol().owner(), boxingArg);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 33 with NewClassTree

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

the class Measurer method visitNode.

@Override
public void visitNode(Tree tree) {
    if (isClassTree(tree)) {
        classes++;
        classTrees.push((ClassTree) tree);
    }
    if (tree.is(Tree.Kind.NEW_CLASS) && ((NewClassTree) tree).classBody() != null) {
        classes--;
    }
    if (tree.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && classTrees.peek().simpleName() != null) {
        // don't count methods in anonymous classes.
        MethodTree methodTree = (MethodTree) tree;
        methods++;
        int methodComplexity = context.getComplexityNodes(methodTree).size();
        methodComplexityDistribution.add(methodComplexity);
        complexityInMethods += methodComplexity;
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Aggregations

NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)33 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)14 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)12 Tree (org.sonar.plugins.java.api.tree.Tree)11 Symbol (org.sonar.plugins.java.api.semantic.Symbol)10 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)10 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)8 Test (org.junit.Test)7 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)7 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)6 Type (org.sonar.plugins.java.api.semantic.Type)5 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)4 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)4 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)4 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 MethodReferenceTree (org.sonar.plugins.java.api.tree.MethodReferenceTree)4 List (java.util.List)3