|
24 | 24 | import org.sonar.check.Priority; |
25 | 25 | import org.sonar.check.Rule; |
26 | 26 | import org.sonar.java.tag.Tag; |
| 27 | +import org.sonar.plugins.java.api.JavaFileScannerContext; |
27 | 28 | import org.sonar.plugins.java.api.semantic.Symbol; |
| 29 | +import org.sonar.plugins.java.api.tree.ClassTree; |
28 | 30 | import org.sonar.plugins.java.api.tree.IdentifierTree; |
| 31 | +import org.sonar.plugins.java.api.tree.MethodTree; |
29 | 32 | import org.sonar.plugins.java.api.tree.Tree; |
30 | 33 | import org.sonar.squidbridge.annotations.SqaleConstantRemediation; |
31 | 34 | import org.sonar.squidbridge.annotations.SqaleSubCharacteristic; |
|
41 | 44 | @SqaleConstantRemediation("15min") |
42 | 45 | public class CallToDeprecatedMethodCheck extends SubscriptionBaseVisitor { |
43 | 46 |
|
| 47 | + private int nestedDeprecationLevel = 0; |
| 48 | + |
| 49 | + @Override |
| 50 | + public void scanFile(JavaFileScannerContext context) { |
| 51 | + super.scanFile(context); |
| 52 | + nestedDeprecationLevel = 0; |
| 53 | + } |
| 54 | + |
44 | 55 | @Override |
45 | 56 | 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); |
47 | 58 | } |
48 | 59 |
|
49 | 60 | @Override |
50 | 61 | 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."); |
58 | 72 | } |
59 | | - reportIssue(tree, "Remove this use of \"" + name + "\"; it is deprecated."); |
| 73 | + } else if (isDeprecatedMethod(tree) || isDeprecatedClassTree(tree)) { |
| 74 | + nestedDeprecationLevel++; |
60 | 75 | } |
| 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(); |
61 | 96 | } |
62 | 97 | } |
0 commit comments