Search in sources :

Example 1 with NewClassTree

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

the class DITCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    if (!isBodyOfEnumConstantTree(tree)) {
        Type superClass = tree.symbol().superClass();
        int dit = 0;
        while (superClass != null) {
            String fullyQualifiedName = superClass.fullyQualifiedName();
            if (getPatterns().stream().anyMatch(wp -> wp.match(fullyQualifiedName))) {
                break;
            }
            dit++;
            superClass = superClass.symbol().superClass();
        }
        if (dit > max) {
            Tree reportTree = tree.simpleName();
            if (tree.parent().is(Tree.Kind.NEW_CLASS)) {
                reportTree = ((NewClassTree) tree.parent()).newKeyword();
            }
            context.reportIssue(this, reportTree, "This class has " + dit + " parents which is greater than " + max + " authorized.", new ArrayList<>(), dit - max);
        }
    }
    super.visitClass(tree);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 2 with NewClassTree

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

the class DisallowedClassCheck method visitNewClass.

@Override
public void visitNewClass(NewClassTree newClassTree) {
    String newClassTypeName = newClassTree.identifier().symbolType().fullyQualifiedName();
    Tree parent = newClassTree.parent();
    if (parent != null && !parent.is(Tree.Kind.VARIABLE)) {
        checkIfDisallowed(newClassTypeName, newClassTree);
    }
    super.visitNewClass(newClassTree);
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 3 with NewClassTree

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

the class SymbolTableTest method Lambdas.

@Test
public void Lambdas() throws Exception {
    Result result = Result.createFor("Lambdas");
    JavaSymbol barMethod = result.symbol("bar");
    assertThat(barMethod.usages()).hasSize(1);
    MethodTree methodTree = (MethodTree) barMethod.declaration();
    Type Ftype = result.symbol("F").type();
    assertThat(((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType()).isSameAs(Ftype);
    JavaSymbol qixMethod = result.symbol("qix");
    LambdaExpressionTree lamdba = ((LambdaExpressionTree) ((ReturnStatementTree) ((MethodTree) qixMethod.declaration()).block().body().get(0)).expression());
    assertThat(((ReturnStatementTree) ((BlockTree) lamdba.body()).body().get(0)).expression().symbolType()).isSameAs(result.symbol("F2").type());
    JavaSymbol fieldSymbol = result.symbol("field");
    assertThat(((VariableTree) fieldSymbol.declaration()).initializer().symbolType()).isSameAs(fieldSymbol.type());
    assertThat(((AssignmentExpressionTree) fieldSymbol.usages().get(0).parent()).expression().symbolType()).isSameAs(fieldSymbol.type());
    JavaSymbol bSymbol = result.symbol("b");
    assertThat(((NewClassTree) ((VariableTree) bSymbol.declaration()).initializer()).arguments().get(0).symbolType()).isSameAs(Ftype);
    JavaSymbol condMethod = result.symbol("cond");
    ConditionalExpressionTree conditionalExpression = (ConditionalExpressionTree) ((ReturnStatementTree) ((MethodTree) condMethod.declaration()).block().body().get(0)).expression();
    assertThat(conditionalExpression.symbolType()).isSameAs(Ftype);
    JavaSymbol parenthMethod = result.symbol("parenth");
    ParenthesizedTree parenthesizedTree = (ParenthesizedTree) ((ReturnStatementTree) ((MethodTree) parenthMethod.declaration()).block().body().get(0)).expression();
    assertThat(parenthesizedTree.symbolType()).isSameAs(Ftype);
    assertThat(result.symbol("s", 33).type().is("java.lang.String")).isTrue();
    JavaSymbol sym = result.symbol("o");
    assertThat(sym.type.is("java.lang.Object")).isTrue();
    assertThat(result.reference(8, 16)).isEqualTo(result.symbol("v", 8));
    assertThat(result.reference(9, 16)).isEqualTo(result.symbol("v", 9));
    JavaSymbol operations = result.symbol("operations");
    MethodInvocationTree mit = (MethodInvocationTree) operations.usages().get(0).parent().parent();
    assertThat(((ParametrizedTypeJavaType) operations.type).typeSubstitution.substitutedTypes().get(0)).isSameAs(mit.arguments().get(0).symbolType());
    JavaSymbol myStringParam = result.symbol("myStringParam");
    Symbol.MethodSymbol stringParamMethod = (Symbol.MethodSymbol) result.symbol("stringParamMethod");
    assertThat(stringParamMethod.usages()).hasSize(1);
    assertThat(myStringParam.type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("s1").type.is("java.lang.String")).as(result.symbol("s1").type.name()).isTrue();
    assertThat(result.symbol("s2").type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("foo", 95).usages()).hasSize(1);
    assertThat(result.symbol("x", 103).type.is("java.lang.Integer")).as(result.symbol("x", 103).type.name()).isTrue();
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Example 4 with NewClassTree

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

the class ExplodedGraphWalker method executeNewClass.

private void executeNewClass(NewClassTree tree) {
    NewClassTree newClassTree = tree;
    programState = programState.unstackValue(newClassTree.arguments().size()).state;
    // Enqueue exceptional paths
    ((CFG.Block) node.programPoint.block).exceptions().forEach(b -> enqueue(new ProgramPoint(b), programState, !b.isCatchBlock()));
    SymbolicValue svNewClass = constraintManager.createSymbolicValue(newClassTree);
    programState = programState.stackValue(svNewClass);
    programState = svNewClass.setSingleConstraint(programState, ObjectConstraint.NOT_NULL);
}
Also used : CFG(org.sonar.java.cfg.CFG) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 5 with NewClassTree

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

the class ParameterNullnessCheck method reportIssue.

private void reportIssue(Tree syntaxNode, ExpressionTree argument, JavaSymbol.MethodJavaSymbol methodSymbol) {
    String declarationMessage = "constructor declaration";
    if (!methodSymbol.isConstructor()) {
        declarationMessage = "method '" + methodSymbol.getName() + "' declaration";
    }
    String message = String.format("Annotate the parameter with @javax.annotation.Nullable in %s, or make sure that null can not be passed as argument.", declarationMessage);
    Tree reportTree;
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        reportTree = ExpressionUtils.methodName((MethodInvocationTree) syntaxNode);
    } else {
        reportTree = ((NewClassTree) syntaxNode).identifier();
    }
    Flow.Builder secondaryBuilder = Flow.builder();
    MethodTree declarationTree = methodSymbol.declaration();
    if (declarationTree != null) {
        secondaryBuilder.add(new JavaFileScannerContext.Location(StringUtils.capitalize(declarationMessage) + ".", declarationTree.simpleName()));
    }
    secondaryBuilder.add(new JavaFileScannerContext.Location("Argument can be null.", argument));
    reportIssue(reportTree, message, Collections.singleton(secondaryBuilder.build()));
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Flow(org.sonar.java.se.Flow)

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