Skip to content

Commit b1f3fc8

Browse files
committed
SONARJAVA-387 Do not raise issue when enclosing method or class is deprecated
1 parent 4f6e529 commit b1f3fc8

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

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

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import org.sonar.check.Priority;
2525
import org.sonar.check.Rule;
2626
import org.sonar.java.tag.Tag;
27+
import org.sonar.plugins.java.api.JavaFileScannerContext;
2728
import org.sonar.plugins.java.api.semantic.Symbol;
29+
import org.sonar.plugins.java.api.tree.ClassTree;
2830
import org.sonar.plugins.java.api.tree.IdentifierTree;
31+
import org.sonar.plugins.java.api.tree.MethodTree;
2932
import org.sonar.plugins.java.api.tree.Tree;
3033
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
3134
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;
@@ -41,22 +44,54 @@
4144
@SqaleConstantRemediation("15min")
4245
public class CallToDeprecatedMethodCheck extends SubscriptionBaseVisitor {
4346

47+
private int nestedDeprecationLevel = 0;
48+
49+
@Override
50+
public void scanFile(JavaFileScannerContext context) {
51+
super.scanFile(context);
52+
nestedDeprecationLevel = 0;
53+
}
54+
4455
@Override
4556
public List<Tree.Kind> nodesToVisit() {
46-
return ImmutableList.of(Tree.Kind.IDENTIFIER);
57+
return ImmutableList.of(Tree.Kind.IDENTIFIER, Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE, Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR);
4758
}
4859

4960
@Override
5061
public void visitNode(Tree tree) {
51-
Symbol symbol = ((IdentifierTree) tree).symbol();
52-
if (symbol.metadata().isAnnotatedWith("java.lang.Deprecated")) {
53-
String name;
54-
if (symbol.isMethodSymbol() && "<init>".equals(symbol.name())) {
55-
name = symbol.owner().name();
56-
} else {
57-
name = symbol.name();
62+
if (tree.is(Tree.Kind.IDENTIFIER)) {
63+
Symbol symbol = ((IdentifierTree) tree).symbol();
64+
if (symbol.isDeprecated() && nestedDeprecationLevel == 0) {
65+
String name;
66+
if (isConstructor(symbol)) {
67+
name = symbol.owner().name();
68+
} else {
69+
name = symbol.name();
70+
}
71+
reportIssue(tree, "Remove this use of \"" + name + "\"; it is deprecated.");
5872
}
59-
reportIssue(tree, "Remove this use of \"" + name + "\"; it is deprecated.");
73+
} else if (isDeprecatedMethod(tree) || isDeprecatedClassTree(tree)) {
74+
nestedDeprecationLevel++;
6075
}
76+
77+
}
78+
79+
private boolean isConstructor(Symbol symbol) {
80+
return symbol.isMethodSymbol() && "<init>".equals(symbol.name());
81+
}
82+
83+
@Override
84+
public void leaveNode(Tree tree) {
85+
if (isDeprecatedMethod(tree) || isDeprecatedClassTree(tree)) {
86+
nestedDeprecationLevel--;
87+
}
88+
}
89+
90+
private boolean isDeprecatedClassTree(Tree tree) {
91+
return tree.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && ((MethodTree) tree).symbol().isDeprecated();
92+
}
93+
94+
private boolean isDeprecatedMethod(Tree tree) {
95+
return tree.is(Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE) && ((ClassTree) tree).symbol().isDeprecated();
6196
}
6297
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.lang.Deprecated;
2+
13
public class CallToDeprecatedMethodCheck {
24

35
public CallToDeprecatedMethodCheck() {
@@ -20,7 +22,22 @@ private static class MyDeprecatedClass {
2022
private static class DeprecatedConstructor {
2123
@Deprecated
2224
public DeprecatedConstructor() {
25+
string.getBytes(1, 1, new byte[3], 7);
2326
}
27+
Object a = "".getBytes(1, 1, new byte[3], 7); // Noncompliant
28+
}
29+
30+
@Deprecated
31+
class A {
32+
Object a = new DeprecatedConstructor();
33+
}
34+
35+
class B {
36+
@Deprecated
37+
void foo() {
38+
Object a = new DeprecatedConstructor();
39+
string.getBytes(1, 1, new byte[3], 7);
40+
}
2441
}
2542

2643
}

0 commit comments

Comments
 (0)