use of org.sonar.plugins.java.api.semantic.Symbol 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.semantic.Symbol 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.semantic.Symbol in project sonar-java by SonarSource.
the class AnnotationArgumentOrderCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
if (annotationSymbol.isUnknown()) {
return;
}
List<String> declarationNames = new ArrayList<>();
for (Symbol symbol : annotationSymbol.memberSymbols()) {
declarationNames.add(symbol.name());
}
List<String> annotationArguments = new ArrayList<>();
for (ExpressionTree argument : annotationTree.arguments()) {
if (argument.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
annotationArguments.add(nameTree.name());
}
}
declarationNames.retainAll(annotationArguments);
if (!declarationNames.equals(annotationArguments)) {
reportIssue(annotationTree.annotationType(), "Reorder annotation arguments to match the order of declaration.");
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class KeySetInsteadOfEntrySetCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
ForEachStatement forEachTree = (ForEachStatement) tree;
ExpressionTree expressionTree = forEachTree.expression();
if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodTree = (MethodInvocationTree) expressionTree;
Symbol ownerSymbol = getOwnerSymbol(methodTree);
if (ownerSymbol != null && MAP_KEYSET_METHOD.matches(methodTree)) {
new GetUsageVisitor().isCallingGetWithSymbol(forEachTree, forEachTree.variable().symbol(), ownerSymbol);
}
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class InnerStaticClassesCheck method visitVariable.
@Override
public void visitVariable(VariableTree tree) {
Symbol symbol = tree.symbol();
if (symbol != null && !symbol.isStatic()) {
scan(tree.modifiers());
scan(tree.type());
// skip the simple name
scan(tree.initializer());
}
}
Aggregations