Search in sources :

Example 46 with VariableTree

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

the class JavaTreeModelTest method array_field.

@Test
public void array_field() {
    VariableTree field;
    ArrayTypeTree arrayTypeTree, childArrayTypeTree;
    field = (VariableTree) firstTypeMember("class T { int[] a; }");
    assertThatChildrenIteratorHasSize(field, 4);
    assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
    arrayTypeTree = (ArrayTypeTree) field.type();
    assertThatArrayTypeHasBrackets(arrayTypeTree);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
    assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
    field = (VariableTree) firstTypeMember("class T { int[][] a; }");
    assertThatChildrenIteratorHasSize(field, 4);
    assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
    arrayTypeTree = (ArrayTypeTree) field.type();
    assertThatArrayTypeHasBrackets(arrayTypeTree);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
    assertThat(arrayTypeTree.type()).isInstanceOf(ArrayTypeTree.class);
    childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
    assertThatArrayTypeHasBrackets(childArrayTypeTree);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
    assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
    assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
    field = (VariableTree) firstTypeMember("class T { int @Foo [] a; }");
    assertThatChildrenIteratorHasSize(field, 4);
    assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
    arrayTypeTree = (ArrayTypeTree) field.type();
    assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
    assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
    field = (VariableTree) firstTypeMember("class T { int @Foo @bar [] a; }");
    assertThatChildrenIteratorHasSize(field, 4);
    assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
    arrayTypeTree = (ArrayTypeTree) field.type();
    assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
    assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 2);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 5);
    field = (VariableTree) firstTypeMember("class T { int[] @Foo [] a; }");
    assertThatChildrenIteratorHasSize(field, 4);
    assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
    arrayTypeTree = (ArrayTypeTree) field.type();
    assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
    childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
    assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
    assertThatArrayTypeHasBrackets(childArrayTypeTree);
    assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
    assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
    assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
    field = (VariableTree) firstTypeMember("class T { int[] a[]; }");
    assertThatChildrenIteratorHasSize(field, 4);
    assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
    arrayTypeTree = (ArrayTypeTree) field.type();
    assertThatArrayTypeHasBrackets(arrayTypeTree);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
    childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
    assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
    assertThatArrayTypeHasBrackets(childArrayTypeTree);
    assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
    assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
    assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
    field = (VariableTree) firstTypeMember("class T { int[] a @Foo []; }");
    assertThatChildrenIteratorHasSize(field, 4);
    assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
    arrayTypeTree = (ArrayTypeTree) field.type();
    assertThat(arrayTypeTree).isInstanceOf(ArrayTypeTree.class);
    assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
    assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
    childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
    assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
    assertThatArrayTypeHasBrackets(childArrayTypeTree);
    assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
    assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
    assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) Test(org.junit.Test)

Example 47 with VariableTree

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

the class JavaTreeModelTest method annotation_constant.

@Test
public void annotation_constant() {
    List<Tree> members = firstType("@interface T { int c1 = 1, c2[] = { 2 }; }").members();
    assertThat(members).hasSize(2);
    VariableTree tree = (VariableTree) members.get(0);
    assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
    assertThat(tree.simpleName().name()).isEqualTo("c1");
    assertThat(tree.initializer()).isNotNull();
    // 1+5, as empty modifiers are always included
    assertThatChildrenIteratorHasSize(tree, 6);
    tree = (VariableTree) members.get(1);
    assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
    assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
    assertThat(tree.simpleName().name()).isEqualTo("c2");
    assertThat(tree.initializer()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 6);
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) StaticInitializerTree(org.sonar.plugins.java.api.tree.StaticInitializerTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) EmptyStatementTree(org.sonar.plugins.java.api.tree.EmptyStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 48 with VariableTree

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

the class AbstractInjectionChecker method isIdentifierDynamicString.

protected boolean isIdentifierDynamicString(Tree methodTree, IdentifierTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) {
    Symbol symbol = arg.symbol();
    if (isExcluded(currentlyChecking, symbol)) {
        return false;
    }
    boolean isLocalVar = symbol.owner().isMethodSymbol() && !((JavaSymbol.MethodJavaSymbol) symbol.owner()).getParameters().scopeSymbols().contains(symbol);
    if (isLocalVar) {
        // symbol is a local variable, check it is not a dynamic string.
        // Check declaration
        VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration();
        ExpressionTree initializer = declaration.initializer();
        if (initializer != null && isDynamicString(methodTree, initializer, currentlyChecking)) {
            return true;
        }
        // check usages by revisiting the enclosing tree.
        Collection<IdentifierTree> usages = symbol.usages();
        LocalVariableDynamicStringVisitor visitor = new LocalVariableDynamicStringVisitor(symbol, usages, methodTree);
        Tree argEnclosingDeclarationTree = semanticModel.getTree(semanticModel.getEnv(symbol));
        argEnclosingDeclarationTree.accept(visitor);
        return visitor.dynamicString;
    }
    // arg is not a local variable nor a constant, so it is a parameter or a field.
    parameterName = "\"" + arg.name() + "\"";
    return symbol.owner().isMethodSymbol() && !firstLevel;
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) 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) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) 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) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 49 with VariableTree

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

the class ChildClassShadowFieldCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    TypeTree superClass = ((ClassTree) tree).superClass();
    if (superClass != null) {
        Symbol.TypeSymbol superclassSymbol = superClass.symbolType().symbol();
        ((ClassTree) tree).members().stream().filter(m -> m.is(Kind.VARIABLE)).map(VariableTree.class::cast).map(VariableTree::simpleName).forEach(fieldSimpleName -> {
            if (!IGNORED_FIELDS.contains(fieldSimpleName.name())) {
                checkForIssue(superclassSymbol, fieldSimpleName);
            }
        });
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ImmutableSet(com.google.common.collect.ImmutableSet) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 50 with VariableTree

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

the class ClassFieldCountCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    long fieldCount = classTree.members().stream().filter(member -> member.is(Tree.Kind.VARIABLE) && shouldBeCounted((VariableTree) member)).count();
    if (fieldCount > threshold) {
        String message = String.format("Refactor this class so it has no more than %d %sfields, rather than the %d it currently has.", threshold, countNonPublicFields ? "" : "public ", fieldCount);
        reportIssue(reportOnClassTree(classTree), message);
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RuleProperty(org.sonar.check.RuleProperty) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) ExpressionsHelper.reportOnClassTree(org.sonar.java.checks.helpers.ExpressionsHelper.reportOnClassTree) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionsHelper.reportOnClassTree(org.sonar.java.checks.helpers.ExpressionsHelper.reportOnClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Aggregations

VariableTree (org.sonar.plugins.java.api.tree.VariableTree)86 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)32 Tree (org.sonar.plugins.java.api.tree.Tree)32 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)27 Test (org.junit.Test)25 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)22 Symbol (org.sonar.plugins.java.api.semantic.Symbol)18 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)16 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 List (java.util.List)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)12 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)11 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9