Skip to content

Add AddCommentToClass #5301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add AddCommentToClass #5301

wants to merge 2 commits into from

Conversation

SiBorea
Copy link
Contributor

@SiBorea SiBorea commented Apr 15, 2025

What's changed?

Add a new recipe AddCommentToClass, similar to AddCommentToMethod

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Apr 15, 2025
Comment on lines +66 to +70
@Option(displayName = "Multiline",
description = "Comments use by default single line // but they can use multiline /* */.",
required = false)
@Nullable
Boolean isMultiline;
Copy link
Member

Choose a reason for hiding this comment

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

What about JavaDoc comments?

Copy link
Contributor

Choose a reason for hiding this comment

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

To be fair, AddCommentToMethod does not support JavaDoc ;) But I 100% get your motivation.

Comment on lines +61 to +64
@Option(displayName = "Superclasses or interfaces",
description = "The FQNs of the superclasses or interfaces to match class to add the comment to. ",
example = "com.netflix.hystrix.HystrixCommand")
List<String> implementations;
Copy link
Member

Choose a reason for hiding this comment

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

There are many different criteria one might wish to use to target comments. Path to the file, type being in use, something on the classpath, only for test sources, etc.

The recipe could try to support parameters for all of these or it could leave it up to Preconditions. For this specific case org.openrewrite.java.search.FindImplementations can be used.

Copy link
Contributor

@MBoegers MBoegers May 5, 2025

Choose a reason for hiding this comment

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

Cool idea! But it's not that simple when inner types are involved. Concidder:

import java.util.HashMap;
                  
public class Example extends HashMap implements Runnable {
    @Override
    public void run() { }
    public class Inner { }
}

With a recipe like:

type: specs.openrewrite.org/v1beta/recipe
# ...
preconditions:
    - org.openrewrite.java.search.FindImplementations:
       typeName: java.util.HashMap
recipeList:
    - org.openrewrite.java.AddCommentToClass:
      comment: " Short comment to add"
      isMultiline: false

The result is unexpeted as the precondition is evaluated on CompilationUnit level.

    public void run() { }
-    public class Inner { }
+    // Short comment to add
+   public class Inner { }
 }

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a superclass and className to the existing solution and checking the implements, extends and type of the class declaration would be a good fit here. Plus, migrating from a String to a TypeMatcher is beneficial too.

Copy link
Contributor

@MBoegers MBoegers left a comment

Choose a reason for hiding this comment

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

Hi @SiBorea I have to request a few changes.

  1. Have what if I want to match the Class by its FQN? For instance, add a Comment to all classes within a package or with a Suffix?
  2. Have you thought about the handling of inner classes? They should not get the same comment if their containing class is matched.

To overcome these I suggest

  • Use TypeMatcher instead of a String comparesion to check if it is the desired type
  • Add superClass and className as options to enable advanced matching capabilities
  • seems a little duplicated but opt out of a visitClassDeclaration if not all type options are meet (like preconditions).

@github-project-automation github-project-automation bot moved this from Ready to Review to In Progress in OpenRewrite May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants