Skip to content
Merged
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
@@ -1,6 +1,6 @@
package checks;

record MissingDeprecatedCheckWithRecords(@Deprecated int lo, int hi) { // Noncompliant
record MissingDeprecatedCheckWithRecords(@Deprecated int lo, int hi) {
public MissingDeprecatedCheckWithRecords {
@Deprecated
int x = 42;
Expand All @@ -9,3 +9,12 @@ record MissingDeprecatedCheckWithRecords(@Deprecated int lo, int hi) { // Noncom
@Deprecated
void foo() {} // Noncompliant
}

// This is a dangling JavaDoc, which is not supported, so we do not require annotations.
record Person(
String name,
/**
* @deprecated
*/
String address // Compliant
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ public int foo11() { // Compliant
return 42;
}

record Person(String name, @Deprecated(since = "date", forRemoval = true) String country) {
@Deprecated
public static final int code = 41; // Noncompliant

/**
* @deprecated reason
*/
@Deprecated
public static final int anotherCode = 42;

public Person {
@Deprecated
int x = 42;
}

@Deprecated
public int number() { // Noncompliant
return 0;
}

/**
* @deprecated reason
*/
@Deprecated
public int anotherNumber() {
return 1;
}
}
}

interface MissingDeprecatedCheckSample_Bar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*/
package org.sonar.java.checks;

import java.util.Optional;
import javax.annotation.CheckForNull;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.tree.AnnotationTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

import static org.sonar.java.checks.helpers.DeprecatedCheckerHelper.reportTreeForDeprecatedTree;
Expand All @@ -29,6 +31,11 @@
public class MissingDeprecatedCheck extends AbstractMissingDeprecatedChecker {

void handleDeprecatedElement(Tree tree, @CheckForNull AnnotationTree deprecatedAnnotation, boolean hasJavadocDeprecatedTag) {
// Record fields cannot have JavaDocs, so skip the check.
if (isRecordComponent(tree)) {
return;
}

boolean hasDeprecatedAnnotation = deprecatedAnnotation != null;
if (hasDeprecatedAnnotation) {
if (!hasJavadocDeprecatedTag) {
Expand All @@ -39,4 +46,15 @@ void handleDeprecatedElement(Tree tree, @CheckForNull AnnotationTree deprecatedA
}
}

/**
* Checks whether the argument is a component of a record (a non-static field).
*/
private static boolean isRecordComponent(Tree tree) {
if (tree instanceof VariableTree variableTree && !variableTree.symbol().isStatic()) {
return Optional.ofNullable(tree.parent())
.filter(parent -> parent.is(Tree.Kind.RECORD))
.isPresent();
}
return false;
}
}