-
Notifications
You must be signed in to change notification settings - Fork 90
721: Recipe to move fields to the top of class definition #736
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
dsgrieve
wants to merge
10
commits into
main
Choose a base branch
from
721-move-fields-to-the-top-of-class-definition
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.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a6a9161
Add MoveFieldsToTopOfClass
dsgrieve 160aff6
Apply suggestions from code review
dsgrieve 8e3c325
* Eliminated ArrayList allocations
dsgrieve 7c74a4c
Update src/main/resources/META-INF/rewrite/static-analysis.yml
dsgrieve f39cdc5
Fix for "Expected recipe to complete in 1 cycle, but took 2 cycles."
dsgrieve a5ae444
Update src/main/java/org/openrewrite/staticanalysis/MoveFieldsToTopOf…
dsgrieve edb5106
do not fix line spacing
dsgrieve ef87dd4
Apply suggestions from code review
dsgrieve d0a22b5
fix compile error introduced by review bot
dsgrieve 3e9c2bf
Update src/main/java/org/openrewrite/staticanalysis/MoveFieldsToTopOf…
dsgrieve 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
131 changes: 131 additions & 0 deletions
131
src/main/java/org/openrewrite/staticanalysis/MoveFieldsToTopOfClass.java
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 |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Value; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.Statement; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| @EqualsAndHashCode(callSuper = false) | ||
| @Value | ||
| public class MoveFieldsToTopOfClass extends Recipe { | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Move fields to the top of class declaration"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Reorders class members so that all field declarations appear before any method declarations, " + | ||
| "constructors, or other class members. This improves code organization and readability by " + | ||
| "grouping field declarations together at the top of the class. Comments associated with fields " + | ||
| "are preserved during the reordering."; | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getEstimatedEffortPerOccurrence() { | ||
| return Duration.ofMinutes(2); | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
| J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); | ||
|
|
||
| List<Statement> statements = | ||
| // getStatements() returns a new List each time, so no need to defensively copy | ||
| cd.getBody().getStatements() | ||
| .stream() | ||
| .sorted(statementComparator) | ||
| .collect(toList()); | ||
|
|
||
| return cd.withBody(cd.getBody().withStatements(statements)); | ||
| } | ||
|
|
||
| private final Comparator<Statement> statementComparator = | ||
| (s1, s2) -> { | ||
| boolean s1IsField = s1 instanceof J.VariableDeclarations; | ||
| boolean s2IsField = s2 instanceof J.VariableDeclarations; | ||
|
|
||
| // Fields come before non-fields | ||
| if (s1IsField && !s2IsField) { | ||
| return -1; | ||
| } | ||
| if (!s1IsField && s2IsField) { | ||
| return 1; | ||
| } | ||
| if (!s1IsField) { | ||
| return 0; // Both are non-fields, preserve order | ||
|
|
||
| // Both are fields - sort by visibility and modifiers | ||
| } | ||
| J.VariableDeclarations field2 = (J.VariableDeclarations) s2; | ||
|
|
||
| int priority1 = getFieldSortOrder(field1); | ||
| int priority2 = getFieldSortOrder(field2); | ||
|
|
||
| return Integer.compare(priority1, priority2); | ||
| }; | ||
|
|
||
|
|
||
| // bitmasks for field sorting | ||
| // order: public static final < static final < protected static final < private static final < | ||
| // public static < static < protected static < private static < | ||
| // public final < final < protected final < private final < | ||
| // public < package-private < protected < private | ||
| private static final int PACKAGE_PRIVATE = 1; | ||
| private static final int PROTECTED = PACKAGE_PRIVATE << 1; | ||
| private static final int PRIVATE = PROTECTED << 1; | ||
| private static final int NON_FINAL = PRIVATE << 1; | ||
| private static final int NON_STATIC = NON_FINAL << 1; | ||
|
|
||
| private int getFieldSortOrder(J.VariableDeclarations field) { | ||
| int order = 0; | ||
| if (field.hasModifier(J.Modifier.Type.Protected)) { | ||
| order |= PROTECTED; | ||
| } else if (field.hasModifier(J.Modifier.Type.Private)) { | ||
| order |= PRIVATE; | ||
| } else { | ||
| order |= PACKAGE_PRIVATE; | ||
| } | ||
|
|
||
| if (!field.hasModifier(J.Modifier.Type.Static)) { | ||
| order |= NON_STATIC; | ||
| } | ||
|
|
||
| if (!field.hasModifier(J.Modifier.Type.Final)) { | ||
| order |= NON_FINAL; | ||
| } | ||
| return order; | ||
| } | ||
|
|
||
| }; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.