Search in sources :

Example 56 with VariableTree

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

the class NonNullSetToNullCheck method checkEndOfExecutionPath.

@Override
public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager constraintManager) {
    if (methodTree.is(Tree.Kind.CONSTRUCTOR) && !isDefaultConstructorForJpa(methodTree) && !callsThisConstructor(methodTree) && !exitingWithException(context)) {
        ClassTree classTree = (ClassTree) methodTree.parent();
        classTree.members().stream().filter(m -> m.is(Tree.Kind.VARIABLE)).map(m -> (VariableTree) m).filter(v -> v.initializer() == null).forEach(v -> checkVariable(context, methodTree, v.symbol()));
    }
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ProgramState(org.sonar.java.se.ProgramState) ExpressionUtils(org.sonar.java.model.ExpressionUtils) NullableAnnotationUtils.nonNullAnnotation(org.sonar.java.se.NullableAnnotationUtils.nonNullAnnotation) MessageFormat(java.text.MessageFormat) CheckerContext(org.sonar.java.se.CheckerContext) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) Lists(com.google.common.collect.Lists) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Nullable(javax.annotation.Nullable) Arguments(org.sonar.plugins.java.api.tree.Arguments) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) List(java.util.List) Stream(java.util.stream.Stream) CFG(org.sonar.java.cfg.CFG) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 57 with VariableTree

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

the class ArrayDesignatorOnVariableCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    VariableTree variableTree = (VariableTree) tree;
    TypeTree type = variableTree.type();
    SyntaxToken identifierToken = variableTree.simpleName().identifierToken();
    while (type.is(Tree.Kind.ARRAY_TYPE)) {
        ArrayTypeTree arrayTypeTree = (ArrayTypeTree) type;
        SyntaxToken arrayDesignatorToken = arrayTypeTree.ellipsisToken();
        if (arrayDesignatorToken == null) {
            arrayDesignatorToken = arrayTypeTree.openBracketToken();
        }
        if (isInvalidPosition(arrayDesignatorToken, identifierToken)) {
            reportIssue(arrayDesignatorToken, "Move the array designator from the variable to the type.");
            break;
        }
        type = arrayTypeTree.type();
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree)

Example 58 with VariableTree

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

the class MembersDifferOnlyByCapitalizationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    List<Symbol> allMembers = retrieveMembers(classTree.symbol());
    Multimap<String, Symbol> membersByName = sortByName(allMembers);
    for (Tree member : classTree.members()) {
        if (member.is(Tree.Kind.METHOD)) {
            MethodTree methodTree = (MethodTree) member;
            checkForIssue(methodTree.symbol(), methodTree.simpleName(), membersByName);
        } else if (member.is(Tree.Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) member;
            checkForIssue(variableTree.symbol(), variableTree.simpleName(), membersByName);
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree)

Example 59 with VariableTree

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

the class ParameterReassignedToCheck method visitMethod.

@Override
public void visitMethod(MethodTree tree) {
    BlockTree block = tree.block();
    if (block == null) {
        return;
    }
    CFG cfg = CFG.build(tree);
    LiveVariables analyze = LiveVariables.analyze(cfg);
    Set<Symbol> live = analyze.getIn(cfg.entry());
    for (VariableTree parameterTree : tree.parameters()) {
        if (!live.contains(parameterTree.symbol())) {
            variables.add(parameterTree.symbol());
        }
    }
    super.visitMethod(tree);
    for (VariableTree parameterTree : tree.parameters()) {
        if (!live.contains(parameterTree.symbol())) {
            variables.remove(parameterTree.symbol());
        }
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 60 with VariableTree

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

the class LambdaTypeParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree;
    List<VariableTree> parameters = lambdaExpressionTree.parameters();
    if (parameters.size() <= 2 && !lambdaExpressionTree.body().is(Tree.Kind.BLOCK)) {
        // ignore lambdas with one or two params and a non-block body
        return;
    }
    String missingTypeParameters = parameters.stream().filter(variable -> variable.type().is(Tree.Kind.INFERED_TYPE)).map(VariableTree::simpleName).map(IdentifierTree::name).map(parameterName -> "'" + parameterName + "'").collect(Collectors.joining(", "));
    if (!missingTypeParameters.isEmpty()) {
        reportIssue(parameters.get(0), Iterables.getLast(parameters), String.format("Specify a type for: %s", missingTypeParameters));
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) List(java.util.List) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collectors(java.util.stream.Collectors) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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