Search in sources :

Example 11 with NewClassTree

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

the class RawExceptionCheck method visitThrowStatement.

@Override
public void visitThrowStatement(ThrowStatementTree tree) {
    if (tree.expression().is(Tree.Kind.NEW_CLASS)) {
        TypeTree exception = ((NewClassTree) tree.expression()).identifier();
        Type symbolType = exception.symbolType();
        if (symbolType instanceof MethodJavaType) {
            symbolType = ((MethodJavaType) exception.symbolType()).resultType();
        }
        if (isRawException(symbolType)) {
            reportIssue(exception);
        }
    }
    super.visitThrowStatement(tree);
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 12 with NewClassTree

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

the class OSCommandInjectionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            Arguments arguments = mit.arguments();
            if (RUNTIME_EXEC_MATCHER.matches(mit)) {
                checkForIssue(tree, arguments.get(0));
            } else if (PROCESS_BUILDER_COMMAND_MATCHER.matches(mit) && !arguments.isEmpty()) {
                checkForIssue(tree, arguments);
            }
        } else if (((NewClassTree) tree).symbolType().is("java.lang.ProcessBuilder")) {
            checkForIssue(tree, ((NewClassTree) tree).arguments());
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 13 with NewClassTree

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

the class FileLinesVisitor method computeExecutableLines.

private void computeExecutableLines(List<? extends Tree> trees) {
    if (trees.isEmpty()) {
        return;
    }
    // rely on cfg to get every instructions and get most of the token.
    CFG cfg = CFG.buildCFG(trees);
    cfg.blocks().stream().flatMap(b -> b.elements().stream()).forEach(t -> {
        if (t.is(NEW_CLASS)) {
            NewClassTree newClassTree = (NewClassTree) t;
            new ExecutableLinesTokenVisitor().scanTree(newClassTree.identifier());
            executableLines.add(newClassTree.newKeyword().line());
        } else if (t.is(TRY_STATEMENT)) {
            // add last token of try statements
            executableLines.add(t.lastToken().line());
        } else {
            executableLines.add(t.firstToken().line());
        }
    });
}
Also used : NEW_CLASS(org.sonar.plugins.java.api.tree.Tree.Kind.NEW_CLASS) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) FOR_EACH_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.FOR_EACH_STATEMENT) STATIC_INITIALIZER(org.sonar.plugins.java.api.tree.Tree.Kind.STATIC_INITIALIZER) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CATCH(org.sonar.plugins.java.api.tree.Tree.Kind.CATCH) LAMBDA_EXPRESSION(org.sonar.plugins.java.api.tree.Tree.Kind.LAMBDA_EXPRESSION) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) FileLinesContext(org.sonar.api.measures.FileLinesContext) HashSet(java.util.HashSet) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) DO_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.DO_STATEMENT) TOKEN(org.sonar.plugins.java.api.tree.Tree.Kind.TOKEN) VARIABLE(org.sonar.plugins.java.api.tree.Tree.Kind.VARIABLE) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) SonarComponents(org.sonar.java.SonarComponents) FLOAT_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.FLOAT_LITERAL) Modifier(org.sonar.plugins.java.api.tree.Modifier) STRING_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.STRING_LITERAL) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) INITIALIZER(org.sonar.plugins.java.api.tree.Tree.Kind.INITIALIZER) Set(java.util.Set) INT_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.INT_LITERAL) Tree(org.sonar.plugins.java.api.tree.Tree) LONG_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.LONG_LITERAL) METHOD(org.sonar.plugins.java.api.tree.Tree.Kind.METHOD) ModifiersUtils(org.sonar.java.model.ModifiersUtils) FOR_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.FOR_STATEMENT) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) CoreMetrics(org.sonar.api.measures.CoreMetrics) File(java.io.File) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) NULL_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.NULL_LITERAL) List(java.util.List) CFG(org.sonar.java.cfg.CFG) DOUBLE_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.DOUBLE_LITERAL) BOOLEAN_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.BOOLEAN_LITERAL) CONSTRUCTOR(org.sonar.plugins.java.api.tree.Tree.Kind.CONSTRUCTOR) BLOCK(org.sonar.plugins.java.api.tree.Tree.Kind.BLOCK) CHAR_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.CHAR_LITERAL) WHILE_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.WHILE_STATEMENT) Collections(java.util.Collections) TRY_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.TRY_STATEMENT) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) CFG(org.sonar.java.cfg.CFG) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 14 with NewClassTree

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

the class DeadStoreCheck method checkElement.

private Set<Symbol> checkElement(Symbol.MethodSymbol methodSymbol, Set<Symbol> outVar, Set<Tree> assignmentLHS, Tree element) {
    Set<Symbol> out = outVar;
    switch(element.kind()) {
        case PLUS_ASSIGNMENT:
        case DIVIDE_ASSIGNMENT:
        case MINUS_ASSIGNMENT:
        case MULTIPLY_ASSIGNMENT:
        case OR_ASSIGNMENT:
        case XOR_ASSIGNMENT:
        case AND_ASSIGNMENT:
        case LEFT_SHIFT_ASSIGNMENT:
        case RIGHT_SHIFT_ASSIGNMENT:
        case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
        case REMAINDER_ASSIGNMENT:
        case ASSIGNMENT:
            handleAssignment(out, assignmentLHS, (AssignmentExpressionTree) element);
            break;
        case IDENTIFIER:
            handleIdentifier(out, assignmentLHS, (IdentifierTree) element);
            break;
        case VARIABLE:
            handleVariable(out, (VariableTree) element);
            break;
        case NEW_CLASS:
            handleNewClass(out, methodSymbol, (NewClassTree) element);
            break;
        case LAMBDA_EXPRESSION:
            LambdaExpressionTree lambda = (LambdaExpressionTree) element;
            out.addAll(getUsedLocalVarInSubTree(lambda.body(), methodSymbol));
            break;
        case METHOD_REFERENCE:
            MethodReferenceTree methodRef = (MethodReferenceTree) element;
            out.addAll(getUsedLocalVarInSubTree(methodRef.expression(), methodSymbol));
            break;
        case TRY_STATEMENT:
            handleTryStatement(out, methodSymbol, (TryStatementTree) element);
            break;
        case PREFIX_DECREMENT:
        case PREFIX_INCREMENT:
            handlePrefixExpression(out, (UnaryExpressionTree) element);
            break;
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
            handlePostfixExpression(out, (UnaryExpressionTree) element);
            break;
        case CLASS:
        case ENUM:
        case ANNOTATION_TYPE:
        case INTERFACE:
            ClassTree classTree = (ClassTree) element;
            out.addAll(getUsedLocalVarInSubTree(classTree, methodSymbol));
            break;
        default:
    }
    return out;
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 15 with NewClassTree

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

the class DiamondOperatorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    NewClassTree newClassTree = (NewClassTree) tree;
    TypeTree newTypeTree = newClassTree.identifier();
    if (newClassTree.classBody() == null && isParameterizedType(newTypeTree)) {
        TypeTree type = getTypeFromExpression(tree.parent(), expressionKindsToCheck);
        if (type != null && isParameterizedType(type)) {
            reportIssue(((ParameterizedTypeTree) newTypeTree).typeArguments(), "Replace the type specification in this constructor call with the diamond operator (\"<>\")." + context.getJavaVersion().java7CompatibilityMessage());
        }
    }
}
Also used : ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Aggregations

NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)33 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)14 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)12 Tree (org.sonar.plugins.java.api.tree.Tree)11 Symbol (org.sonar.plugins.java.api.semantic.Symbol)10 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)10 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)8 Test (org.junit.Test)7 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)7 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)6 Type (org.sonar.plugins.java.api.semantic.Type)5 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)4 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)4 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)4 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 MethodReferenceTree (org.sonar.plugins.java.api.tree.MethodReferenceTree)4 List (java.util.List)3