Skip to content

SONARJAVA-5719 S1176 Should have a separate message for undocumented type parameters.#5575

Open
NoemieBenard wants to merge 3 commits intomasterfrom
nb/sonarjava-5719-type-parameters
Open

SONARJAVA-5719 S1176 Should have a separate message for undocumented type parameters.#5575
NoemieBenard wants to merge 3 commits intomasterfrom
nb/sonarjava-5719-type-parameters

Conversation

@NoemieBenard
Copy link
Copy Markdown
Contributor

Add separate message for type parameters in rule S1176.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod bot commented Apr 16, 2026

SONARJAVA-5719

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha bot commented Apr 16, 2026

Summary

This PR improves the error messages in S1176 (undocumented API) to distinguish between type parameters and regular method parameters.

What changed:

  • When a class, interface, or record has undocumented generic type parameters, the message now says "Document the type parameter(s)" instead of just "Document the parameter(s)"
  • Method parameters continue to use "Document the parameter(s)" message
  • A new getParamLabel() helper method determines which label to use based on the tree type

Additional refactoring:

  • Replaced stream().collect(Collectors.joining()) with String.join() for cleaner string concatenation
  • Removed the now-unused Collectors import

This makes error messages clearer and more accurate about what needs to be documented.

What reviewers should know

Start here:

  • Review the getParamLabel() method to see the logic for distinguishing between type parameters and regular parameters
  • Check the updated test expectations in the two test files to see concrete examples of the new messages

What to verify:

  • The new messages appear only when they're appropriate (type parameters for classes/interfaces/records, regular parameters for methods)
  • Test cases confirm the correct message is used in each context
  • The String.join() refactoring produces identical output to the original stream().collect() approach

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as resolved.

if (!undocumentedParameters.isEmpty()) {
context.reportIssue(this, reportTree, "Document the parameter(s): " + undocumentedParameters.stream().collect(Collectors.joining(", ")));
String label = getParamLabel(tree);
context.reportIssue(this, reportTree, "Document the " + label + undocumentedParameters.stream().collect(Collectors.joining(", ")));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can try to improve existing code: String.join(", ", undocumentedParameters) should do the same as the long expression with a stream.

* This is a Javadoc comment
*/
public class MyClass<T> implements Runnable { // Noncompliant {{Document the parameter(s): <T>}}
public class MyClass<T> implements Runnable { // Noncompliant {{Document the type parameter(s): <T>}}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like this?

  /**
   * Description.
   */
  public record MyRecord<U>(U a, int b) {
  }

@sonarqube-next
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

🗣️ Give feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants