Skip to content

Commit 37c9185

Browse files
committed
Fix quality flaw: Migrate to precise issue location
1 parent 3769316 commit 37c9185

17 files changed

Lines changed: 69 additions & 26 deletions

java-checks/src/main/java/org/sonar/java/checks/AbstractDeprecatedChecker.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.collect.Lists;
2323
import org.sonar.java.ast.visitors.PublicApiChecker;
24+
import org.sonar.java.checks.helpers.ExpressionsHelper;
2425
import org.sonar.plugins.java.api.tree.AnnotationTree;
2526
import org.sonar.plugins.java.api.tree.ClassTree;
2627
import org.sonar.plugins.java.api.tree.IdentifierTree;
@@ -83,6 +84,18 @@ private static boolean hasDeprecatedAnnotation(Iterable<AnnotationTree> annotati
8384
return false;
8485
}
8586

87+
protected static Tree getReportTree(Tree tree) {
88+
Tree reportTree = tree;
89+
if(reportTree.is(PublicApiChecker.classKinds())) {
90+
reportTree = ExpressionsHelper.reportOnClassTree(((ClassTree) reportTree));
91+
} else if(reportTree.is(PublicApiChecker.methodKinds())) {
92+
reportTree = ((MethodTree) reportTree).simpleName();
93+
} else if(reportTree.is(Tree.Kind.VARIABLE)) {
94+
reportTree = ((VariableTree) reportTree).simpleName();
95+
}
96+
return reportTree;
97+
}
98+
8699
public static boolean isDeprecated(AnnotationTree tree) {
87100
return tree.annotationType().is(Kind.IDENTIFIER) &&
88101
"Deprecated".equals(((IdentifierTree) tree.annotationType()).name());

java-checks/src/main/java/org/sonar/java/checks/AbstractSerializableInnerClassRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ private void visitClassTree(ClassTree classTree) {
5050
if (owner.isTypeSymbol()) {
5151
Symbol.TypeSymbol ownerType = (Symbol.TypeSymbol) owner;
5252
if (isMatchingOuterClass(ownerType.type()) && !symbol.isStatic()) {
53-
addIssue(classTree, "Make this inner class static");
53+
reportIssue(classTree.simpleName(), "Make this inner class static");
5454
}
5555
} else if (owner.isMethodSymbol()) {
5656
Symbol.TypeSymbol methodOwner = (Symbol.TypeSymbol) owner.owner();
5757
if (isMatchingOuterClass(methodOwner.type()) && !owner.isStatic()) {
5858
String methodName = owner.name();
59-
addIssue(classTree, "Make \"" + methodName + "\" static");
59+
reportIssue(classTree.simpleName(), "Make \"" + methodName + "\" static");
6060
}
6161
}
6262
}

java-checks/src/main/java/org/sonar/java/checks/CustomCryptographicAlgorithmCheck.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.sonar.api.server.rule.RulesDefinition;
2424
import org.sonar.check.Priority;
2525
import org.sonar.check.Rule;
26+
import org.sonar.java.checks.helpers.ExpressionsHelper;
2627
import org.sonar.java.tag.Tag;
2728
import org.sonar.plugins.java.api.semantic.Symbol;
2829
import org.sonar.plugins.java.api.tree.ClassTree;
@@ -51,8 +52,9 @@ public List<Kind> nodesToVisit() {
5152

5253
@Override
5354
public void visitNode(Tree tree) {
54-
if (hasSemantic() && isJavaSecurityMessageDigestSubClass((ClassTree) tree)) {
55-
addIssue(tree, "Use a standard algorithm instead of creating a custom one.");
55+
ClassTree classTree = (ClassTree) tree;
56+
if (hasSemantic() && isJavaSecurityMessageDigestSubClass(classTree)) {
57+
reportIssue(ExpressionsHelper.reportOnClassTree(classTree), "Use a standard algorithm instead of creating a custom one.");
5658
}
5759
}
5860

java-checks/src/main/java/org/sonar/java/checks/DeprecatedTagPresenceCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class DeprecatedTagPresenceCheck extends AbstractDeprecatedChecker {
4141
@Override
4242
public void visitNode(Tree tree) {
4343
if (hasDeprecatedAnnotation(tree) || hasJavadocDeprecatedTag(tree)) {
44-
addIssue(tree, "Do not forget to remove this deprecated code someday.");
44+
reportIssue(getReportTree(tree), "Do not forget to remove this deprecated code someday.");
4545
}
4646
}
4747

java-checks/src/main/java/org/sonar/java/checks/EqualsNotOverriddenInSubclassCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public List<Kind> nodesToVisit() {
5858
public void visitNode(Tree tree) {
5959
ClassTree classTree = (ClassTree) tree;
6060
if (hasSemantic() && hasAtLeastOneField(classTree) && !implementsEquals(classTree) && parentClassImplementsEquals(classTree)) {
61-
addIssue(classTree, "Override this superclass' \"equals\" method.");
61+
reportIssue(classTree.simpleName(), "Override this superclass' \"equals\" method.");
6262
}
6363
}
6464

java-checks/src/main/java/org/sonar/java/checks/InnerClassTooManyLinesCheck.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.sonar.check.Priority;
2525
import org.sonar.check.Rule;
2626
import org.sonar.check.RuleProperty;
27+
import org.sonar.java.checks.helpers.ExpressionsHelper;
2728
import org.sonar.java.syntaxtoken.FirstSyntaxTokenFinder;
2829
import org.sonar.java.syntaxtoken.LastSyntaxTokenFinder;
2930
import org.sonar.java.tag.Tag;
@@ -70,7 +71,7 @@ public void visitNode(Tree tree) {
7071
int last = LastSyntaxTokenFinder.lastSyntaxToken(node).line();
7172
int length = last - first + 1;
7273
if (length > max) {
73-
addIssue(node, "Reduce this class from " + length + " to the maximum allowed " + max + " or externalize it in a public class.");
74+
reportIssue(ExpressionsHelper.reportOnClassTree(node), "Reduce this class from " + length + " to the maximum allowed " + max + " or externalize it in a public class.");
7475
}
7576
}
7677
}

java-checks/src/main/java/org/sonar/java/checks/MissingDeprecatedCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public void visitNode(Tree tree) {
6262
boolean hasJavadocDeprecatedTag = hasJavadocDeprecatedTag(tree);
6363
if (currentClassNotDeprecated() && !isLocalVar) {
6464
if (hasDeprecatedAnnotation && !hasJavadocDeprecatedTag) {
65-
addIssue(tree, "Add the missing @deprecated Javadoc tag.");
65+
reportIssue(getReportTree(tree), "Add the missing @deprecated Javadoc tag.");
6666
} else if (hasJavadocDeprecatedTag && !hasDeprecatedAnnotation) {
67-
addIssue(tree, "Add the missing @Deprecated annotation.");
67+
reportIssue(getReportTree(tree), "Add the missing @Deprecated annotation.");
6868
}
6969
}
7070
if (tree.is(CLASS_KINDS)) {

java-checks/src/main/java/org/sonar/java/checks/SynchronizedClassUsageCheck.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.sonar.api.server.rule.RulesDefinition;
2626
import org.sonar.check.Priority;
2727
import org.sonar.check.Rule;
28+
import org.sonar.java.checks.helpers.ExpressionsHelper;
2829
import org.sonar.java.model.declaration.MethodTreeImpl;
2930
import org.sonar.java.tag.Tag;
3031
import org.sonar.plugins.java.api.semantic.Type;
@@ -78,7 +79,7 @@ private class DeprecatedTypeVisitor extends BaseTreeVisitor {
7879
public void visitClass(ClassTree tree) {
7980
TypeTree superClass = tree.superClass();
8081
if (superClass != null) {
81-
reportIssueOnDeprecatedType(tree, superClass.symbolType());
82+
reportIssueOnDeprecatedType(ExpressionsHelper.reportOnClassTree(tree), superClass.symbolType());
8283
}
8384

8485
scan(tree.members());
@@ -110,7 +111,7 @@ public void visitVariable(VariableTree tree) {
110111

111112
private boolean reportIssueOnDeprecatedType(Tree tree, Type type) {
112113
if (isDeprecatedType(type)) {
113-
addIssue(tree, "Replace the synchronized class \"" + type.name() + "\" by an unsynchronized one such as " + REPLACEMENTS.get(type.fullyQualifiedName()) + ".");
114+
reportIssue(tree, "Replace the synchronized class \"" + type.name() + "\" by an unsynchronized one such as " + REPLACEMENTS.get(type.fullyQualifiedName()) + ".");
114115
return true;
115116
}
116117
return false;

java-checks/src/main/java/org/sonar/java/checks/helpers/ExpressionsHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
*/
2020
package org.sonar.java.checks.helpers;
2121

22+
import org.sonar.plugins.java.api.tree.ClassTree;
2223
import org.sonar.plugins.java.api.tree.ExpressionTree;
2324
import org.sonar.plugins.java.api.tree.IdentifierTree;
2425
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
26+
import org.sonar.plugins.java.api.tree.NewClassTree;
2527
import org.sonar.plugins.java.api.tree.ParenthesizedTree;
2628
import org.sonar.plugins.java.api.tree.Tree;
2729

@@ -67,5 +69,18 @@ public static String concatenate(@Nullable ExpressionTree tree) {
6769
return sb.toString();
6870
}
6971

72+
/**
73+
* Return the correct tree to report on for class trees.
74+
* @param classTree class tree raising an issue.
75+
* @return simple name of class tree or identifier in parent expression for anonymous class.
76+
*/
77+
public static Tree reportOnClassTree(ClassTree classTree) {
78+
Tree reportTree = classTree.simpleName();
79+
if(reportTree == null) {
80+
reportTree = ((NewClassTree) classTree.parent()).identifier();
81+
}
82+
return reportTree;
83+
}
84+
7085

7186
}

java-checks/src/test/files/checks/CustomCryptographicAlgorithmCheck.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
import java.util.Observable;
33

44
class A { // Compliant
5+
void foo() {
6+
MessageDigest md = new MessageDigest() { // Noncompliant [[sc=28;ec=41]] {{Use a standard algorithm instead of creating a custom one.}}
7+
8+
};
9+
}
510
}
611

7-
abstract class B extends MessageDigest { // Noncompliant {{Use a standard algorithm instead of creating a custom one.}}
12+
abstract class B extends MessageDigest { // Noncompliant [[sc=16;ec=17]] {{Use a standard algorithm instead of creating a custom one.}}
813
protected B(String algorithm) {
914
super(algorithm);
1015
}

0 commit comments

Comments
 (0)