Search in sources :

Example 6 with VariableTree

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

the class ExceptionsShouldBeImmutableCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    if (isException(classTree)) {
        for (Tree member : classTree.members()) {
            if (member.is(Tree.Kind.VARIABLE) && !isFinal((VariableTree) member)) {
                IdentifierTree simpleName = ((VariableTree) member).simpleName();
                reportIssue(simpleName, "Make this \"" + simpleName.name() + "\" field final.");
            }
        }
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 7 with VariableTree

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

the class DefaultEncodingUsageCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!excluded.contains(tree)) {
        super.visitNode(tree);
        if (tree.is(Tree.Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) tree;
            boolean foundIssue = checkForbiddenTypes(variableTree.simpleName(), variableTree.type().symbolType());
            if (foundIssue) {
                excluded.add(variableTree.initializer());
            }
        } else if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            checkForbiddenTypes(ExpressionUtils.methodName(mit), mit.symbolType());
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 8 with VariableTree

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

the class ImmediatelyReturnedVariableCheck method visitBlock.

@Override
public void visitBlock(BlockTree tree) {
    super.visitBlock(tree);
    List<StatementTree> statements = tree.body();
    int size = statements.size();
    if (size < 2) {
        return;
    }
    StatementTree butLastStatement = statements.get(size - 2);
    if (butLastStatement.is(Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) butLastStatement;
        if (!variableTree.modifiers().annotations().isEmpty()) {
            return;
        }
        StatementTree lastStatement = statements.get(size - 1);
        String lastStatementIdentifier = getReturnOrThrowIdentifier(lastStatement);
        if (lastStatementIdentifier != null) {
            String identifier = variableTree.simpleName().name();
            if (StringUtils.equals(lastStatementIdentifier, identifier)) {
                context.reportIssue(this, variableTree.initializer(), "Immediately " + lastTypeForMessage + " this expression instead of assigning it to the temporary variable \"" + identifier + "\".");
            }
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 9 with VariableTree

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

the class MethodTreeImpl method isParameterStringArray.

private boolean isParameterStringArray() {
    VariableTree variableTree = parameters.get(0);
    boolean result = false;
    if (variableTree.type().is(Tree.Kind.ARRAY_TYPE)) {
        ArrayTypeTree arrayTypeTree = (ArrayTypeTree) variableTree.type();
        result = arrayTypeTree.type().symbolType().isClass() && "String".equals(arrayTypeTree.type().symbolType().name());
    }
    return result;
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree)

Example 10 with VariableTree

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

the class SymbolTableTest method resolve_return_type_after_inference.

@Test
public void resolve_return_type_after_inference() throws Exception {
    Result res = Result.createFor("VarInitializerInference");
    VariableTree mySet = (VariableTree) res.symbol("mySet").declaration();
    assertThat(mySet.initializer().symbolType().is("VarInitializer$ImmutableSet")).isTrue();
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

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