Skip to content

Commit 8652ee2

Browse files
authored
SONARJAVA-5719 S1176 Should have a separate message for undocumented type parameters. (#5575)
1 parent b2a8eb4 commit 8652ee2

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

java-checks-test-sources/default/src/main/files/non-compiling/checks/api/undocumentedAPI/UndocumentedApi.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public A() { // Noncompliant {{Document this public constructor by adding an exp
6767
/**
6868
* This is a Javadoc comment
6969
*/
70-
public class MyClass<T> implements Runnable { // Noncompliant {{Document the parameter(s): <T>}}
70+
public class MyClass<T> implements Runnable { // Noncompliant {{Document the type parameter(s): <T>}}
7171

7272
private int status; // Compliant - not public
7373

@@ -331,6 +331,13 @@ public void doSomething() { } // Compliant - Override
331331

332332
}
333333

334+
/**
335+
* Description.
336+
*/
337+
public record MyRecord<U>(U a, int b) { // Noncompliant {{Document the type parameter(s): <U>}}
338+
// ^^^^^^^^
339+
}
340+
334341
@interface MyAnnotation {}
335342

336343
class UsesVisibleForTesting {

java-checks-test-sources/default/src/main/java/checks/api/undocumentedAPI/UndocumentedApiJava23.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public String missingThrows(int value) throws NumberFormatException { // Noncomp
5151
}
5252

5353
/// Documented, but not the type.
54-
public class SomethingGenericBad<T> { // Noncompliant {{Document the parameter(s): <T>}}
54+
public class SomethingGenericBad<T> { // Noncompliant {{Document the type parameter(s): <T>}}
5555
}
5656

5757
/// Documented.

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.LinkedList;
2121
import java.util.Set;
2222
import java.util.regex.Pattern;
23-
import java.util.stream.Collectors;
2423
import org.apache.commons.lang3.StringUtils;
2524
import org.sonar.api.utils.WildcardPattern;
2625
import org.sonar.check.Rule;
@@ -129,14 +128,15 @@ private void visitNode(Tree tree, Tree reportTree, SymbolMetadata symbolMetadata
129128
} else {
130129
Set<String> undocumentedParameters = javadoc.undocumentedParameters();
131130
if (!undocumentedParameters.isEmpty()) {
132-
context.reportIssue(this, reportTree, "Document the parameter(s): " + undocumentedParameters.stream().collect(Collectors.joining(", ")));
131+
String label = getParamLabel(tree);
132+
context.reportIssue(this, reportTree, "Document the " + label + String.join(", ", undocumentedParameters));
133133
}
134134
if (hasNonVoidReturnType(tree) && javadoc.noReturnDescription()) {
135135
context.reportIssue(this, reportTree, "Document this method return value.");
136136
}
137137
Set<String> undocumentedExceptions = javadoc.undocumentedThrownExceptions();
138138
if (!undocumentedExceptions.isEmpty()) {
139-
context.reportIssue(this, reportTree, "Document this method thrown exception(s): " + undocumentedExceptions.stream().collect(Collectors.joining(", ")));
139+
context.reportIssue(this, reportTree, "Document this method thrown exception(s): " + String.join(", ", undocumentedExceptions));
140140
}
141141
}
142142
}
@@ -153,6 +153,10 @@ private boolean isNonVoidMethodWithNoParameter(Tree tree, Javadoc javadoc) {
153153
&& !javadoc.noReturnDescription();
154154
}
155155

156+
private static String getParamLabel(Tree tree) {
157+
return (tree.is(Kind.CLASS) || tree.is(Kind.INTERFACE) || tree.is(Kind.RECORD)) ? "type parameter(s): " : "parameter(s): ";
158+
}
159+
156160
private static String getType(Tree tree) {
157161
switch (tree.kind()) {
158162
case CONSTRUCTOR:
@@ -163,10 +167,7 @@ private static String getType(Tree tree) {
163167
return "field";
164168
case ANNOTATION_TYPE:
165169
return "annotation";
166-
case CLASS,
167-
INTERFACE,
168-
ENUM,
169-
RECORD:
170+
case CLASS, INTERFACE, ENUM, RECORD:
170171
return ((ClassTree) tree).declarationKeyword().text();
171172
default:
172173
return "";

0 commit comments

Comments
 (0)