use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class TooManyMethodsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
List<Tree> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && (countNonPublic || ((MethodTree) member).symbol().isPublic())).collect(Collectors.toList());
if (shouldNotReportIssue(classTree, methods)) {
return;
}
List<JavaFileScannerContext.Location> secondary = methods.stream().map(element -> new JavaFileScannerContext.Location("Method + 1", element)).collect(Collectors.toList());
String classDescription;
if (classTree.simpleName() == null) {
classDescription = "Anonymous class \"" + ((NewClassTree) classTree.parent()).identifier().symbolType().name() + "\"";
} else {
classDescription = classTree.declarationKeyword().text() + " \"" + classTree.simpleName() + "\"";
}
reportIssue(ExpressionsHelper.reportOnClassTree(classTree), String.format("%s has %d%s methods, which is greater than the %d authorized. Split it into smaller classes.", classDescription, methods.size(), countNonPublic ? "" : " public", maximumMethodThreshold), secondary, null);
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class ThreadAsRunnableArgumentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
List<ExpressionTree> arguments;
Symbol methodSymbol;
if (tree.is(Kind.NEW_CLASS)) {
NewClassTree nct = (NewClassTree) tree;
methodSymbol = nct.constructorSymbol();
arguments = nct.arguments();
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
methodSymbol = mit.symbol();
arguments = mit.arguments();
}
if (!arguments.isEmpty() && methodSymbol.isMethodSymbol()) {
checkArgumentsTypes(arguments, (MethodJavaSymbol) methodSymbol);
}
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class ThreadLocalWithInitialCheck method onConstructorFound.
@Override
protected void onConstructorFound(NewClassTree newClassTree) {
ClassTree classTree = newClassTree.classBody();
if (classTree == null) {
return;
}
List<Tree> members = classTree.members();
if (members.size() != 1) {
return;
}
members.stream().filter(tree -> tree.is(Tree.Kind.METHOD)).map(t -> ((MethodTree) t)).filter(t -> "initialValue".equals(t.simpleName().name())).filter(t -> t.parameters().isEmpty()).findFirst().ifPresent(t -> reportIssue(newClassTree.identifier(), "Replace this anonymous class with a call to \"ThreadLocal.withInitial\"." + context.getJavaVersion().java8CompatibilityMessage()));
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class ThreadOverridesRunCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
Symbol.TypeSymbol classSymbol = classTree.symbol();
if (classSymbol != null && isDirectSubtypeOfThread(classSymbol) && !overridesRunMethod(classSymbol) && !hasConstructorCallingSuperWithRunnable(classTree)) {
Tree report = classTree.simpleName();
Tree parent = classTree.parent();
if (parent.is(Tree.Kind.NEW_CLASS)) {
NewClassTree newClassTree = (NewClassTree) parent;
if (hasRunnableArgument(newClassTree.arguments())) {
// will call the super constructor setting a runnable which will be executed by the run() method
return;
}
report = newClassTree.identifier();
}
reportIssue(report, "Don't extend \"Thread\", since the \"run\" method is not overridden.");
}
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class ClassWithoutHashCodeInHashStructureCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
Type type = ((NewClassTree) tree).symbolType();
if (type instanceof ParametrizedTypeJavaType && useHashDataStructure(type)) {
ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) type;
Symbol.TypeSymbol symbol = ptt.substitution(ptt.typeParameters().get(0)).symbol();
if (implementsEquals(symbol) && !implementsHashCode(symbol)) {
reportIssue(tree, "Add a \"hashCode()\" method to \"" + symbol.name() + "\" or remove it from this hash.");
}
}
}
Aggregations