-
Notifications
You must be signed in to change notification settings - Fork 525
Add optional ecosystem parameter to ChangeType
#7833
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
Draft
Jenson3210
wants to merge
1
commit into
main
Choose a base branch
from
changetype-ecosystem-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+97
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
| package org.openrewrite.java; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Value; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
@@ -56,6 +57,18 @@ public class ChangeType extends Recipe { | |
| @Nullable | ||
| Boolean ignoreDefinition; | ||
|
|
||
| @Option(displayName = "Ecosystem", | ||
| description = "Restrict the type change to source files of a given ecosystem: `jvm` (Java, Kotlin, " + | ||
| "Groovy), `python`, or `javascript`. Python and JavaScript reuse the Java LST model (their " + | ||
| "compilation units implement `JavaSourceFile`), so without this filter a change targeting one " + | ||
| "ecosystem still scans the others' source files unnecessarily. Reference-based files (such as " + | ||
| "XML and properties) are always processed because they may carry type references. When `null` " + | ||
| "(the default), the change is applied to all source files.", | ||
| valid = {"jvm", "python", "javascript"}, | ||
| required = false) | ||
| @Nullable | ||
| String ecosystem; | ||
|
|
||
| String displayName = "Change type"; | ||
|
|
||
| @Override | ||
|
|
@@ -82,6 +95,46 @@ public String getInstanceNameSuffix() { | |
|
|
||
| String description = "Change a given type to another."; | ||
|
|
||
| public ChangeType(String oldFullyQualifiedTypeName, String newFullyQualifiedTypeName, @Nullable Boolean ignoreDefinition) { | ||
| this(oldFullyQualifiedTypeName, newFullyQualifiedTypeName, ignoreDefinition, null); | ||
| } | ||
|
|
||
| @JsonCreator | ||
| public ChangeType(String oldFullyQualifiedTypeName, String newFullyQualifiedTypeName, @Nullable Boolean ignoreDefinition, @Nullable String ecosystem) { | ||
| this.oldFullyQualifiedTypeName = oldFullyQualifiedTypeName; | ||
| this.newFullyQualifiedTypeName = newFullyQualifiedTypeName; | ||
| this.ignoreDefinition = ignoreDefinition; | ||
| this.ecosystem = ecosystem; | ||
| } | ||
|
|
||
| private boolean skipForEcosystem(JavaSourceFile cu) { | ||
| if (ecosystem == null || ecosystem.isEmpty()) { | ||
| return false; | ||
| } | ||
| String impl = cu.getClass().getName(); | ||
| String fileEcosystem; | ||
| if (impl.startsWith("org.openrewrite.python.")) { | ||
| fileEcosystem = "python"; | ||
| } else if (impl.startsWith("org.openrewrite.javascript.")) { | ||
| fileEcosystem = "javascript"; | ||
| } else { | ||
| // java, kotlin, groovy, scala, ... | ||
| fileEcosystem = "jvm"; | ||
| } | ||
| return !normalizeEcosystem(ecosystem).equals(fileEcosystem); | ||
| } | ||
|
|
||
| private static String normalizeEcosystem(String ecosystem) { | ||
| switch (ecosystem.toLowerCase()) { | ||
| case "js": | ||
| return "javascript"; | ||
| case "py": | ||
| return "python"; | ||
|
Comment on lines
+129
to
+132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the alternatives also be in the valid set at that point? |
||
| default: | ||
| return ecosystem.toLowerCase(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| TreeVisitor<?, ExecutionContext> condition = new TreeVisitor<Tree, ExecutionContext>() { | ||
|
|
@@ -90,6 +143,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() { | |
| stopAfterPreVisit(); | ||
| if (tree instanceof JavaSourceFile) { | ||
| JavaSourceFile cu = (JavaSourceFile) tree; | ||
| if (skipForEcosystem(cu)) { | ||
| return tree; | ||
| } | ||
| if (!Boolean.TRUE.equals(ignoreDefinition) && containsClassDefinition(cu, oldFullyQualifiedTypeName)) { | ||
| return SearchResult.found(cu); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.