Search in sources :

Example 16 with SyntaxToken

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

the class TreeFactory method newArrayInitializer.

public NewArrayTreeImpl newArrayInitializer(InternalSyntaxToken openBraceToken, Optional<InternalSyntaxToken> optionalComma, Optional<List<Tuple<ExpressionTree, Optional<InternalSyntaxToken>>>> rests, InternalSyntaxToken closeBraceToken) {
    ImmutableList.Builder<ExpressionTree> initializers = ImmutableList.builder();
    ImmutableList.Builder<SyntaxToken> separators = ImmutableList.builder();
    if (optionalComma.isPresent()) {
        separators.add(optionalComma.get());
    }
    if (rests.isPresent()) {
        for (Tuple<ExpressionTree, Optional<InternalSyntaxToken>> rest : rests.get()) {
            initializers.add(rest.first());
            if (rest.second().isPresent()) {
                separators.add(rest.second().get());
            }
        }
    }
    return new NewArrayTreeImpl(ImmutableList.<ArrayDimensionTree>of(), new InitializerListTreeImpl(initializers.build(), separators.build())).completeWithCurlyBraces(openBraceToken, closeBraceToken);
}
Also used : NewArrayTreeImpl(org.sonar.java.model.expression.NewArrayTreeImpl) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) Optional(com.sonar.sslr.api.typed.Optional) ImmutableList(com.google.common.collect.ImmutableList) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 17 with SyntaxToken

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

the class AnalyzerMessage method textSpanBetween.

public static AnalyzerMessage.TextSpan textSpanBetween(Tree startTree, Tree endTree) {
    SyntaxToken firstSyntaxToken = startTree.firstToken();
    SyntaxToken lastSyntaxToken = endTree.lastToken();
    return textSpanBetween(firstSyntaxToken, lastSyntaxToken);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 18 with SyntaxToken

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

the class SonarSymbolTableVisitor method createSymbol.

private void createSymbol(IdentifierTree declaration, List<IdentifierTree> usages) {
    SyntaxToken syntaxToken = declaration.identifierToken();
    NewSymbol newSymbol = newSymbolTable.newSymbol(syntaxToken.line(), syntaxToken.column(), syntaxToken.line(), syntaxToken.text().length() + syntaxToken.column());
    for (IdentifierTree usage : usages) {
        syntaxToken = usage.identifierToken();
        newSymbol.newReference(syntaxToken.line(), syntaxToken.column(), syntaxToken.line(), syntaxToken.text().length() + syntaxToken.column());
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) NewSymbol(org.sonar.api.batch.sensor.symbol.NewSymbol)

Example 19 with SyntaxToken

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

the class AnonymousClassesTooBigCheck method visitLambdaExpression.

@Override
public void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree) {
    int lines = getNumberOfLines(lambdaExpressionTree);
    if (lines > max) {
        SyntaxToken firstToken = lambdaExpressionTree.firstToken();
        SyntaxToken lastSyntaxToken = lambdaExpressionTree.lastToken();
        JavaFileScannerContext.Location lastTokenLocation = new JavaFileScannerContext.Location(lines + " lines", lastSyntaxToken);
        context.reportIssue(this, firstToken, lambdaExpressionTree.arrowToken(), "Reduce this lambda expression number of lines from " + lines + " to at most " + max + ".", Lists.newArrayList(lastTokenLocation), null);
    }
    super.visitLambdaExpression(lambdaExpressionTree);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext)

Example 20 with SyntaxToken

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

the class DataStoredInSessionCheck method usedBetween.

private boolean usedBetween(Symbol variable, Tree start, Tree end) {
    SyntaxToken startToken = start.lastToken();
    SyntaxToken endToken = end.firstToken();
    for (IdentifierTree identifier : variable.usages()) {
        SyntaxToken identifierToken = identifier.identifierToken();
        if (isAfterFirstToken(identifierToken, startToken) && isBeforeLastToken(identifierToken, endToken) && !identifiersUsedToSetAttribute.contains(identifier)) {
            return true;
        }
    }
    return false;
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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