-
Notifications
You must be signed in to change notification settings - Fork 422
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
base: main
Are you sure you want to change the base?
Add AddCommentToClass #5301
Conversation
@Option(displayName = "Multiline", | ||
description = "Comments use by default single line // but they can use multiline /* */.", | ||
required = false) | ||
@Nullable | ||
Boolean isMultiline; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about JavaDoc comments?
There was a problem hiding this comment.
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.
@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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { }
}
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
- 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?
- 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
andclassName
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).
What's changed?
Add a new recipe
AddCommentToClass
, similar toAddCommentToMethod
Checklist