Skip to content

Commit af6e07c

Browse files
committed
SONARJAVA-1430 Detect call to deprecated constructors
1 parent 2c9aa39 commit af6e07c

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public List<Tree.Kind> nodesToVisit() {
6161
public void visitNode(Tree tree) {
6262
if (tree.is(Tree.Kind.IDENTIFIER)) {
6363
Symbol symbol = ((IdentifierTree) tree).symbol();
64-
if (symbol.isDeprecated() && nestedDeprecationLevel == 0) {
64+
if (isDeprecated(symbol) && nestedDeprecationLevel == 0) {
6565
String name;
6666
if (isConstructor(symbol)) {
6767
name = symbol.owner().name();
@@ -76,6 +76,10 @@ public void visitNode(Tree tree) {
7676

7777
}
7878

79+
private static boolean isDeprecated(Symbol symbol) {
80+
return symbol.isDeprecated() || (isConstructor(symbol) && symbol.owner().isDeprecated());
81+
}
82+
7983
private static boolean isConstructor(Symbol symbol) {
8084
return symbol.isMethodSymbol() && "<init>".equals(symbol.name());
8185
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public CallToDeprecatedMethodCheck() {
66
String string = new String("my string");
77
string.getBytes(1, 1, new byte[3], 7); // Noncompliant [[sc=12;ec=20]] {{Remove this use of "getBytes"; it is deprecated.}}
88
new DeprecatedConstructor(); // Noncompliant [[sc=9;ec=30]] {{Remove this use of "DeprecatedConstructor"; it is deprecated.}}
9-
new MyDeprecatedClass();
9+
new MyDeprecatedClass(); // Noncompliant
1010
old++; // Noncompliant
1111
MyDeprecatedClass.a++; // Noncompliant
1212
}

0 commit comments

Comments
 (0)