Search in sources :

Example 61 with VariableTree

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

the class MethodOnlyCallsSuperCheck method callsWithSameParameters.

private static boolean callsWithSameParameters(List<ExpressionTree> arguments, List<VariableTree> parameters) {
    if (arguments.size() != parameters.size()) {
        return false;
    }
    for (int i = 0; i < arguments.size(); i++) {
        ExpressionTree arg = arguments.get(i);
        VariableTree param = parameters.get(i);
        if (!(arg.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) arg).name().equals(param.simpleName().name()))) {
            return false;
        }
    }
    return true;
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 62 with VariableTree

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

the class PrivateFieldUsedLocallyCheck method checkPrivateField.

private void checkPrivateField(Symbol privateFieldSymbol, TypeSymbol classSymbol) {
    MethodTree methodWhereUsed = usedInOneMethodOnly(privateFieldSymbol, classSymbol);
    if (methodWhereUsed != null && !isLiveInMethodEntry(privateFieldSymbol, methodWhereUsed)) {
        IdentifierTree declarationIdentifier = ((VariableTree) privateFieldSymbol.declaration()).simpleName();
        String message = String.format(MESSAGE, privateFieldSymbol.name());
        reportIssue(declarationIdentifier, message);
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 63 with VariableTree

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

the class OptionalAsParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    for (VariableTree parameter : ((MethodTree) tree).parameters()) {
        TypeTree typeTree = parameter.type();
        Optional<String> msg = expectedTypeInsteadOfOptional(typeTree.symbolType());
        if (msg.isPresent()) {
            reportIssue(typeTree, msg.get());
        }
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 64 with VariableTree

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

the class MutableMembersUsageCheck method visitMethod.

@Override
public void visitMethod(MethodTree tree) {
    ArrayList<Symbol> parameters = new ArrayList<>();
    for (VariableTree variableTree : tree.parameters()) {
        parameters.add(variableTree.symbol());
    }
    parametersStack.push(parameters);
    super.visitMethod(tree);
    parametersStack.pop();
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 65 with VariableTree

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

the class MutableMembersUsageCheck method isImmutableConstant.

private static boolean isImmutableConstant(Symbol.VariableSymbol symbol) {
    if (symbol.isStatic() && symbol.isFinal()) {
        VariableTree declaration = symbol.declaration();
        // symbol is private, so declaration can only be null if assignment is done in static block
        ExpressionTree initializer = declaration.initializer();
        if (initializer != null) {
            return !isMutableType(initializer) || isEmptyArray(initializer);
        }
        return !assignementsOfMutableType(symbol.usages());
    }
    return false;
}
Also used : 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)

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