Search in sources :

Example 1 with SyntaxToken

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

the class LambdaOptionalParenthesisCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LambdaExpressionTree let = (LambdaExpressionTree) tree;
    SyntaxToken openParenToken = let.openParenToken();
    if (openParenToken != null && let.parameters().size() == 1) {
        VariableTree param = let.parameters().get(0);
        String identifier = param.simpleName().name();
        if (param.type().is(Tree.Kind.INFERED_TYPE)) {
            reportIssue(openParenToken, "Remove the parentheses around the \"" + identifier + "\" parameter" + context.getJavaVersion().java8CompatibilityMessage());
        }
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 2 with SyntaxToken

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

the class MethodInvocationTreeImpl method firstToken.

@Override
public SyntaxToken firstToken() {
    if (typeArguments() != null && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        ExpressionTree expression = ((MemberSelectExpressionTree) methodSelect).expression();
        SyntaxToken firstToken = expression.firstToken();
        if (firstToken != null) {
            return firstToken;
        }
    }
    return super.firstToken();
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 3 with SyntaxToken

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

the class TryStatementTreeImpl method filterVariableTreeResources.

private static ListTree filterVariableTreeResources(ResourceListTreeImpl resources) {
    ImmutableList.Builder<Tree> filteredResources = ImmutableList.builder();
    ImmutableList.Builder<SyntaxToken> filteredSeparators = ImmutableList.builder();
    Iterator<SyntaxToken> separators = resources.separators().iterator();
    for (Tree resource : resources) {
        SyntaxToken separator = null;
        if (separators.hasNext()) {
            separator = separators.next();
        }
        if (resource.is(Kind.VARIABLE)) {
            filteredResources.add(resource);
            if (separator != null) {
                filteredSeparators.add(separator);
            }
        }
    }
    return new ResourceListTreeImpl(filteredResources.build(), filteredSeparators.build());
}
Also used : InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) ImmutableList(com.google.common.collect.ImmutableList) ListTree(org.sonar.plugins.java.api.tree.ListTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) ResourceListTreeImpl(org.sonar.java.ast.parser.ResourceListTreeImpl)

Example 4 with SyntaxToken

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

the class ReassignmentFinder method isBefore.

private static int isBefore(Tree t1, Tree t2) {
    SyntaxToken firstTokenT1 = t1.firstToken();
    SyntaxToken firstTokenT2 = t2.firstToken();
    int line = Integer.compare(firstTokenT1.line(), firstTokenT2.line());
    return line != 0 ? line : Integer.compare(firstTokenT1.column(), firstTokenT2.column());
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 5 with SyntaxToken

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

the class ReassignmentFinder method getClosestReassignmentOrDeclarationExpression.

@CheckForNull
public static ExpressionTree getClosestReassignmentOrDeclarationExpression(Tree startingPoint, Symbol referenceSymbol) {
    Tree result = referenceSymbol.declaration();
    List<IdentifierTree> usages = referenceSymbol.usages();
    if (usages.size() != 1) {
        List<AssignmentExpressionTree> reassignments = getReassignments(referenceSymbol.owner().declaration(), usages);
        SyntaxToken startPointToken = startingPoint.firstToken();
        Tree lastReassignment = getClosestReassignment(startPointToken, reassignments);
        if (lastReassignment != null) {
            result = lastReassignment;
        }
    }
    ExpressionTree initializerOrExpression = getInitializerOrExpression(result);
    if (initializerOrExpression == startingPoint) {
        return getClosestReassignmentOrDeclarationExpression(result, referenceSymbol);
    }
    return initializerOrExpression;
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) CheckForNull(javax.annotation.CheckForNull)

Aggregations

SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)47 Tree (org.sonar.plugins.java.api.tree.Tree)9 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)7 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)7 Test (org.junit.Test)6 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)6 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)5 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)5 ImmutableList (com.google.common.collect.ImmutableList)4 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 CheckForNull (javax.annotation.CheckForNull)2