Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public int foo7() { // Noncompliant
public void foo8() { // Noncompliant
}

@java.lang.Deprecated // Compliant - no one does this
public void foo9() {
@java.lang.Deprecated
public void foo9() { // Noncompliant {{Do not forget to remove this deprecated code someday.}}
// ^^^^

@Deprecated
int local1 = 0; // Noncompliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public void foo8() { // Noncompliant
}

@java.lang.Deprecated
public void foo9() {
public void foo9() { // Noncompliant {{Add the missing @deprecated Javadoc tag.}}
// ^^^^

@Deprecated
int local1 = 0;
Expand Down Expand Up @@ -153,3 +154,49 @@ interface mockito {
@Deprecated
<T> T createMock(Class<T> toMock, Method... mockedMethods);
}

class QualifiedDeprecation {

static class Annotations {
@interface Deprecated {}
}

/** @deprecated Use replacement instead. */
@Annotations.Deprecated
public void customAnnotation() {} // Noncompliant {{Add the missing @Deprecated annotation.}}
// ^^^^^^^^^^^^^^^^

/** @deprecated Use replacement instead. */
@java.lang.Deprecated
public void method() {}

/** @deprecated Use replacement instead. */
@java.lang.Deprecated
public int field;

@java.lang.Deprecated
public int undocumentedField; // Noncompliant {{Add the missing @deprecated Javadoc tag.}}
// ^^^^^^^^^^^^^^^^^

public enum State {
UNKNOWN;

/** @deprecated Use {@link #forNumber(int)} instead. */
@java.lang.Deprecated
public static State valueOf(int value) {
return forNumber(value);
}

public static State forNumber(int value) {
return UNKNOWN;
}
}
}

/** @deprecated Use replacement instead. */
@java.lang.Deprecated
class QualifiedDeprecatedClass {}

@java.lang.Deprecated
class UndocumentedQualifiedDeprecatedClass {} // Noncompliant {{Add the missing @deprecated Javadoc tag.}}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

public class DeprecatedCheckerHelper {

private static final String DEPRECATED_ANNOTATION = "java.lang.Deprecated";
private static final String DEPRECATED_TAG = "@deprecated";
private static final Kind[] CLASS_KINDS = PublicApiChecker.classKinds();
private static final Kind[] METHOD_KINDS = PublicApiChecker.methodKinds();
Expand Down Expand Up @@ -148,8 +149,10 @@ private static AnnotationTree deprecatedAnnotation(Iterable<AnnotationTree> anno
}

private static boolean isDeprecated(AnnotationTree tree) {
return tree.annotationType().is(Kind.IDENTIFIER) &&
"Deprecated".equals(((IdentifierTree) tree.annotationType()).name());
if (tree.annotationType().is(Kind.IDENTIFIER)) {
return "Deprecated".equals(((IdentifierTree) tree.annotationType()).name());
}
return tree.annotationType().symbolType().is(DEPRECATED_ANNOTATION);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;

Expand All @@ -29,6 +30,22 @@

class DeprecatedCheckerHelperTest {

@ParameterizedTest
@ValueSource(strings = {"Deprecated", "java.lang.Deprecated"})
void deprecatedAnnotation(String name) {
var classTree = parseClass("@" + name + "(since = \"9\", forRemoval = true) class A {}");
var annotation = DeprecatedCheckerHelper.deprecatedAnnotation(classTree);
assertThat(annotation).isNotNull();
assertThat(DeprecatedCheckerHelper.getAnnotationAttributeValue(annotation, "since", String.class)).contains("9");
assertThat(DeprecatedCheckerHelper.getAnnotationAttributeValue(annotation, "forRemoval", Boolean.class)).contains(true);
}

@ParameterizedTest
@ValueSource(strings = {"other.Deprecated", "java.lang.Override"})
void unrelatedAnnotation(String name) {
assertThat(DeprecatedCheckerHelper.deprecatedAnnotation(parseClass("@" + name + " class A {}"))).isNull();
}

@Test
void getAnnotationAttributeValue() {
assertValue("@Deprecated(forRemoval = true)", "forRemoval", true, Boolean.class);
Expand Down
Loading