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);
}
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);
}
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();
}
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);
}
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()));
}
Aggregations