-
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 1 commit
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
138 changes: 138 additions & 0 deletions
138
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,138 @@ | ||
| /* | ||
| * 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.internal.ListUtils; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.Space; | ||
| import org.openrewrite.java.tree.Statement; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @EqualsAndHashCode(callSuper = false) | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @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 definition"; | ||
dsgrieve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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 = cd.getBody().getStatements(); | ||
| if (statements.isEmpty()) { | ||
| return cd; | ||
| } | ||
|
|
||
| List<Statement> fieldDeclarations = new ArrayList<>(); | ||
| List<Statement> otherStatements = new ArrayList<>(); | ||
|
|
||
| // Separate field declarations from other statements | ||
| for (Statement statement : statements) { | ||
| if (statement instanceof J.VariableDeclarations) { | ||
| fieldDeclarations.add(statement); | ||
| } else { | ||
| otherStatements.add(statement); | ||
| } | ||
| } | ||
|
|
||
| // If all fields are already at the top, no changes needed | ||
| if (fieldDeclarations.isEmpty() || isAlreadyOrdered(statements, fieldDeclarations)) { | ||
dsgrieve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return cd; | ||
| } | ||
|
|
||
| // Reorder: fields first, then other statements | ||
| List<Statement> reorderedStatements = new ArrayList<>(); | ||
dsgrieve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Add field declarations with preserved comments and proper spacing | ||
| for (int i = 0; i < fieldDeclarations.size(); i++) { | ||
dsgrieve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Statement field = fieldDeclarations.get(i); | ||
| if (i == 0) { | ||
| // First field should have the same prefix as the first statement originally had, | ||
| // but preserve its original comments | ||
| Space originalPrefix = field.getPrefix(); | ||
| Space firstStatementPrefix = statements.get(0).getPrefix(); | ||
|
|
||
dsgrieve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Combine: use first statement's whitespace but preserve field's comments | ||
| Space newPrefix = firstStatementPrefix.withComments(originalPrefix.getComments()); | ||
| field = field.withPrefix(newPrefix); | ||
| } | ||
| reorderedStatements.add(field); | ||
| } | ||
|
|
||
| // Add other statements | ||
| reorderedStatements.addAll(otherStatements); | ||
|
|
||
| return autoFormat(cd.withBody(cd.getBody().withStatements(reorderedStatements)), ctx); | ||
dsgrieve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private boolean isAlreadyOrdered(List<Statement> allStatements, List<Statement> fieldDeclarations) { | ||
dsgrieve marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (fieldDeclarations.isEmpty()) { | ||
| return true; | ||
| } | ||
|
|
||
| int firstNonFieldIndex = -1; | ||
| for (int i = 0; i < allStatements.size(); i++) { | ||
| if (!(allStatements.get(i) instanceof J.VariableDeclarations)) { | ||
| firstNonFieldIndex = i; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // If there are no non-field statements, or all fields come before first non-field | ||
| if (firstNonFieldIndex == -1) { | ||
| return true; | ||
| } | ||
|
|
||
| // Check if any field declarations come after the first non-field statement | ||
| for (int i = firstNonFieldIndex; i < allStatements.size(); i++) { | ||
| if (allStatements.get(i) instanceof J.VariableDeclarations) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
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.