use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class AbstractClassWithoutAbstractMethodCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
Symbol.TypeSymbol typeSymbol = classTree.symbol();
if (typeSymbol.isAbstract()) {
Collection<Symbol> members = typeSymbol.memberSymbols();
int nbAbstractMethod = countAbstractMethods(members);
// don't count this and super as members
int nbOfMembers = members.size() - 2;
if (hasDefaultConstructor(members)) {
// remove default constructor from members
nbOfMembers -= 1;
}
if (isExtendingObject(classTree) && nbAbstractMethod == nbOfMembers) {
// emtpy abstract class or only abstract method
context.reportIssue(this, classTree.simpleName(), "Convert this \"" + typeSymbol + "\" class to an interface");
}
if (nbOfMembers > 0 && nbAbstractMethod == 0 && !isPartialImplementation(classTree)) {
// Not empty abstract class with no abstract method
context.reportIssue(this, classTree.simpleName(), "Convert this \"" + typeSymbol + "\" class to a concrete class with a private constructor");
}
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class AbstractSerializableInnerClassRule method visitClassTree.
private void visitClassTree(ClassTree classTree) {
Symbol.TypeSymbol symbol = classTree.symbol();
if (isInnerClass(symbol) && directlyImplementsSerializable(symbol)) {
Tree reportTree = ExpressionsHelper.reportOnClassTree(classTree);
Symbol owner = symbol.owner();
if (owner.isTypeSymbol()) {
Symbol.TypeSymbol ownerType = (Symbol.TypeSymbol) owner;
if (isMatchingOuterClass(ownerType.type()) && !symbol.isStatic()) {
reportIssue(reportTree, "Make this inner class static");
}
} else if (owner.isMethodSymbol()) {
Symbol.TypeSymbol methodOwner = (Symbol.TypeSymbol) owner.owner();
if (isMatchingOuterClass(methodOwner.type()) && !owner.isStatic()) {
String methodName = owner.name();
reportIssue(reportTree, "Make \"" + methodName + "\" static");
}
}
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class InterfaceOrSuperclassShadowingCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
if (hasSemantic()) {
Symbol.TypeSymbol classSymbol = classTree.symbol();
checkSuperType(classTree, classSymbol.superClass());
for (Type interfaceType : classSymbol.interfaces()) {
checkSuperType(classTree, interfaceType);
}
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class HiddenFieldCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (isClassTree(tree)) {
ClassTree classTree = (ClassTree) tree;
ImmutableMap.Builder<String, VariableTree> builder = ImmutableMap.builder();
for (Tree member : classTree.members()) {
if (member.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) member;
builder.put(variableTree.simpleName().name(), variableTree);
}
}
fields.push(builder.build());
excludedVariables.push(Lists.<VariableTree>newArrayList());
} else if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
isVariableHidingField(variableTree);
} else if (tree.is(Tree.Kind.STATIC_INITIALIZER)) {
excludeVariablesFromBlock((BlockTree) tree);
} else {
MethodTree methodTree = (MethodTree) tree;
excludedVariables.peek().addAll(methodTree.parameters());
flattenExcludedVariables.addAll(methodTree.parameters());
if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.STATIC)) {
excludeVariablesFromBlock(methodTree.block());
}
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class EmptyMethodsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
if (!ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT)) {
List<Tree> members = classTree.members();
checkMethods(members);
checkSingleNoArgPublicConstructor(members);
}
}
Aggregations