Search in sources :

Example 66 with ExpressionTree

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

the class ParameterReassignedToCheck method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    ExpressionTree variable = tree.variable();
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) variable;
        Symbol reference = identifier.symbol();
        if (reference.isVariableSymbol() && variables.contains(reference)) {
            context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\".");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 67 with ExpressionTree

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

the class OSCommandInjectionCheck method isDynamicArray.

private boolean isDynamicArray(ExpressionTree argument, Tree mit) {
    ExpressionTree arg = getDeclaration(argument);
    if (arg.is(Tree.Kind.NEW_ARRAY)) {
        NewArrayTree nat = (NewArrayTree) arg;
        for (ExpressionTree expressionTree : nat.initializers()) {
            if (isDynamicString(mit, expressionTree, null)) {
                return true;
            }
        }
        return false;
    }
    setParameterNameFromArgument(arg);
    boolean argIsString = arg.symbolType().is("java.lang.String");
    return !argIsString || isDynamicString(mit, arg, null);
}
Also used : NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 68 with ExpressionTree

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

the class TreeFactory method annotationIdentifier.

public TypeTree annotationIdentifier(InternalSyntaxToken firstIdentifier, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> rests) {
    List<InternalSyntaxToken> children = Lists.newArrayList();
    children.add(firstIdentifier);
    if (rests.isPresent()) {
        for (Tuple<InternalSyntaxToken, InternalSyntaxToken> rest : rests.get()) {
            children.add(rest.first());
            children.add(rest.second());
        }
    }
    JavaTree result = null;
    InternalSyntaxToken dotToken = null;
    for (InternalSyntaxToken child : children) {
        if (!child.getGrammarRuleKey().equals(JavaTokenType.IDENTIFIER)) {
            dotToken = child;
        } else {
            InternalSyntaxToken identifierToken = child;
            if (result == null) {
                result = new IdentifierTreeImpl(identifierToken);
            } else {
                IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
                result = new MemberSelectExpressionTreeImpl((ExpressionTree) result, dotToken, identifier);
            }
        }
    }
    return (TypeTree) result;
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) MemberSelectExpressionTreeImpl(org.sonar.java.model.expression.MemberSelectExpressionTreeImpl) JavaTree(org.sonar.java.model.JavaTree)

Example 69 with ExpressionTree

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

the class TreeFactory method applySelectors.

private static ExpressionTree applySelectors(ExpressionTree primary, Optional<List<Tuple<Optional<InternalSyntaxToken>, ExpressionTree>>> selectors) {
    ExpressionTree result = primary;
    if (selectors.isPresent()) {
        for (Tuple<Optional<InternalSyntaxToken>, ExpressionTree> tuple : selectors.get()) {
            Optional<InternalSyntaxToken> dotTokenOptional = tuple.first();
            ExpressionTree selector = tuple.second();
            if (dotTokenOptional.isPresent()) {
                InternalSyntaxToken dotToken = dotTokenOptional.get();
                if (selector.is(Kind.IDENTIFIER)) {
                    IdentifierTreeImpl identifier = (IdentifierTreeImpl) selector;
                    result = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
                } else {
                    MethodInvocationTreeImpl methodInvocation = (MethodInvocationTreeImpl) selector;
                    IdentifierTreeImpl identifier = (IdentifierTreeImpl) methodInvocation.methodSelect();
                    MemberSelectExpressionTreeImpl memberSelect = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
                    result = new MethodInvocationTreeImpl(memberSelect, methodInvocation.typeArguments(), (ArgumentListTreeImpl) methodInvocation.arguments());
                }
            } else if (selector.is(Kind.NEW_CLASS)) {
                NewClassTreeImpl newClass = (NewClassTreeImpl) selector;
                result = newClass.completeWithEnclosingExpression(result);
            } else if (selector.is(Kind.ARRAY_ACCESS_EXPRESSION)) {
                ArrayAccessExpressionTreeImpl arrayAccess = (ArrayAccessExpressionTreeImpl) selector;
                result = arrayAccess.complete(result);
            } else if (selector.is(Kind.MEMBER_SELECT)) {
                MemberSelectExpressionTreeImpl memberSelect = (MemberSelectExpressionTreeImpl) selector;
                result = memberSelect.completeWithExpression(result);
            } else {
                throw new IllegalStateException();
            }
        }
    }
    return result;
}
Also used : Optional(com.sonar.sslr.api.typed.Optional) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) ArrayAccessExpressionTreeImpl(org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl) TypeArgumentListTreeImpl(org.sonar.java.model.expression.TypeArgumentListTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) MemberSelectExpressionTreeImpl(org.sonar.java.model.expression.MemberSelectExpressionTreeImpl) MethodInvocationTreeImpl(org.sonar.java.model.expression.MethodInvocationTreeImpl)

Example 70 with ExpressionTree

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

the class TreeFactory method newAnnotatedParameterizedIdentifier.

public ExpressionTree newAnnotatedParameterizedIdentifier(Optional<List<AnnotationTreeImpl>> annotations, InternalSyntaxToken identifierToken, Optional<TypeArgumentListTreeImpl> typeArguments) {
    List<AnnotationTree> annotationList = ImmutableList.copyOf(annotations.or(ImmutableList.of()));
    ExpressionTree result = new IdentifierTreeImpl(identifierToken);
    if (typeArguments.isPresent()) {
        result = new ParameterizedTypeTreeImpl((TypeTree) result, typeArguments.get()).complete(annotationList);
    } else {
        result = ((IdentifierTreeImpl) result).complete(annotationList);
    }
    return result;
}
Also used : IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) ParameterizedTypeTreeImpl(org.sonar.java.model.JavaTree.ParameterizedTypeTreeImpl)

Aggregations

ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)189 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)88 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)80 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)66 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)50 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)44 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)35 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)30 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)30 Test (org.junit.Test)25 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)24 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)23 Type (org.sonar.plugins.java.api.semantic.Type)21 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)20 Tree (org.sonar.plugins.java.api.tree.Tree)20 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)14 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)13