Search in sources :

Example 81 with VariableTree

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

the class SpringComponentWithNonAutowiredMembersCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree clazzTree = (ClassTree) tree;
    SymbolMetadata clazzMeta = clazzTree.symbol().metadata();
    if (isSpringComponent(clazzMeta) && !hasUniqueConstructor(clazzTree)) {
        clazzTree.members().stream().filter(v -> v.is(Kind.VARIABLE)).map(m -> (VariableTree) m).filter(v -> !v.symbol().isStatic()).filter(v -> !isSpringInjectionAnnotated(v.symbol().metadata())).forEach(v -> reportIssue(v.simpleName(), "Annotate this member with \"@Autowired\", \"@Resource\", \"@Inject\", or \"@Value\", or remove it."));
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) ImmutableList(com.google.common.collect.ImmutableList) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 82 with VariableTree

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

the class SerialVersionUidCheck method checkModifiers.

private void checkModifiers(Symbol.VariableSymbol serialVersionUidSymbol) {
    List<String> missingModifiers = Lists.newArrayList();
    if (!serialVersionUidSymbol.isStatic()) {
        missingModifiers.add("static");
    }
    if (!serialVersionUidSymbol.isFinal()) {
        missingModifiers.add("final");
    }
    if (!serialVersionUidSymbol.type().is("long")) {
        missingModifiers.add("long");
    }
    VariableTree variableTree = serialVersionUidSymbol.declaration();
    if (variableTree != null && !missingModifiers.isEmpty()) {
        reportIssue(variableTree.simpleName(), "Make this \"serialVersionUID\" field \"" + Joiner.on(' ').join(missingModifiers) + "\".");
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 83 with VariableTree

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

the class VariableDeclarationScopeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    BlockTree block = (BlockTree) tree;
    List<StatementTree> body = block.body();
    int bodySize = body.size();
    for (int i = 0; i < bodySize; i++) {
        StatementTree statement = body.get(i);
        if (statement.is(Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) statement;
            if (!variableTree.symbol().usages().isEmpty()) {
                check(variableTree, body, bodySize, i + 1);
            }
        }
    }
}
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) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 84 with VariableTree

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

the class BadLocalConstantNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    VariableTree variableTree = (VariableTree) tree;
    Symbol symbol = variableTree.symbol();
    if (!symbol.owner().isMethodSymbol() || !symbol.isFinal()) {
        return;
    }
    if (!hasLiteralInitializer(variableTree.initializer())) {
        return;
    }
    IdentifierTree simpleName = variableTree.simpleName();
    if (!pattern.matcher(simpleName.name()).matches()) {
        reportIssue(simpleName, "Rename this constant name to match the regular expression '" + format + "'.");
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 85 with VariableTree

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

the class UCFGJavaVisitor method buildCall.

private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
    if (isStringVarDecl(element)) {
        VariableTree variableTree = (VariableTree) element;
        String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
        if (!idGenerator.isConst(lhs)) {
            ExpressionTree initializer = variableTree.initializer();
            String source = idGenerator.lookupIdFor(initializer);
            blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
        }
        return;
    }
    if (element.is(METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
        buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
    } else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
        if (element.is(PLUS)) {
            BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
            Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
            Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
            Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
            blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
        } else if (element.is(PLUS_ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                idGenerator.varForExpression(element, ((Expression.Variable) var).id());
                blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
            }
        } else if (element.is(ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
            }
        }
    }
}
Also used : Expression(org.sonar.ucfg.Expression) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

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