From 5ebd55106de69e1dc7af25cefbdb5b30aaeee0eb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 30 Sep 2025 14:17:48 +0000 Subject: [PATCH 1/5] Adopt `Style.from` instead of `SourceFile.getStyle` Use this link to re-run the recipe: https://app.moderne.io/builder/6OMjIdVBh?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne --- .../groovy/format/AutoFormatVisitor.java | 2 +- .../groovy/format/OmitParenthesesFormat.java | 2 +- .../openrewrite/hcl/format/BlankLines.java | 5 ++-- .../org/openrewrite/hcl/format/Spaces.java | 2 +- .../hcl/format/TabsAndIndents.java | 2 +- .../org/openrewrite/java/RemoveImport.java | 2 +- .../openrewrite/java/format/BlankLines.java | 5 ++-- .../java/format/MethodParamPad.java | 2 +- .../java/format/NormalizeLineBreaks.java | 2 +- .../java/format/NormalizeTabsOrSpaces.java | 2 +- .../format/PadEmptyForLoopComponents.java | 2 +- .../openrewrite/java/format/ShiftFormat.java | 2 +- .../org/openrewrite/java/format/Spaces.java | 8 +++--- .../java/format/TypecastParenPad.java | 4 +-- .../java/format/WrappingAndBraces.java | 2 +- .../org/openrewrite/kotlin/AddImport.java | 4 +-- .../kotlin/format/AutoFormatVisitor.java | 26 +++++++++---------- .../format/ImportReorderingVisitor.java | 2 +- .../SpacesFromCompilationUnitStyle.java | 2 +- .../xml/format/NormalizeLineBreaks.java | 2 +- 20 files changed, 39 insertions(+), 41 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java index bcf98d05c5c..e71e2a55adb 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java @@ -62,7 +62,7 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) { t = new SpacesVisitor<>( spacesStyle, - cu.getStyle(EmptyForInitializerPadStyle.class), + Style.from(EmptyForInitializerPadStyle.class, cu), Style.from(EmptyForIteratorPadStyle.class, cu), stopAfter ).visit(t, p, cursor.fork()); diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java index 1f2ed42f5f8..047dce8bf49 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java @@ -47,7 +47,7 @@ private static class OmitParenthesesFromCompilationUnitStyle extends JavaIsoVisi public J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { SourceFile cu = (SourceFile) requireNonNull(tree); - OmitParenthesesStyle style = Optional.ofNullable(cu.getStyle(OmitParenthesesStyle.class)).orElse(OmitParenthesesStyle.DEFAULT); + OmitParenthesesStyle style = Optional.ofNullable(Style.from(OmitParenthesesStyle.class, cu)).orElse(OmitParenthesesStyle.DEFAULT); if (style.getLastArgumentLambda()) { doAfterVisit(new OmitParenthesesForLastArgumentLambda().getVisitor()); } diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java index c6c5a37e29f..2eab17aadff 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java @@ -39,7 +39,7 @@ public TreeVisitor getVisitor() { private static class BlankLinesFromCompilationUnitStyle extends HclIsoVisitor { @Override public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile configFile, ExecutionContext ctx) { - BlankLinesStyle style = configFile.getStyle(BlankLinesStyle.class); + BlankLinesStyle style = Style.from(BlankLinesStyle.class, configFile); if (style == null) { style = BlankLinesStyle.DEFAULT; } @@ -48,8 +48,7 @@ public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile configFile, ExecutionContex } public static H formatBlankLines(Hcl j, Cursor cursor) { - BlankLinesStyle style = cursor.firstEnclosingOrThrow(SourceFile.class) - .getStyle(BlankLinesStyle.class); + BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class)); //noinspection unchecked return (H) new BlankLinesVisitor<>(style == null ? BlankLinesStyle.DEFAULT : style) .visitNonNull(j, 0, cursor); diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java index f379f1d04ad..60afe679a63 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java @@ -42,7 +42,7 @@ public TreeVisitor getVisitor() { private static class SpacesFromCompilationUnitStyle extends HclIsoVisitor { @Override public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) { - SpacesStyle style = cf.getStyle(SpacesStyle.class); + SpacesStyle style = Style.from(SpacesStyle.class, cf); if (style == null) { style = SpacesStyle.DEFAULT; } diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java index f177a6e1f9f..dd0073680aa 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java @@ -42,7 +42,7 @@ public TreeVisitor getVisitor() { private static class TabsAndIndentsFromCompilationUnitStyle extends HclIsoVisitor { @Override public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) { - TabsAndIndentsStyle style = cf.getStyle(TabsAndIndentsStyle.class); + TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cf); if (style == null) { style = TabsAndIndentsStyle.DEFAULT; } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java index 22787f86897..331656b4579 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java @@ -58,7 +58,7 @@ public RemoveImport(String type, boolean force) { J j = tree; if (tree instanceof JavaSourceFile) { JavaSourceFile cu = (JavaSourceFile) tree; - ImportLayoutStyle importLayoutStyle = Optional.ofNullable(((SourceFile) cu).getStyle(ImportLayoutStyle.class)) + ImportLayoutStyle importLayoutStyle = Optional.ofNullable(Style.from(ImportLayoutStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.importLayout()); boolean typeUsed = false; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java index 366314ec397..7a67b81b652 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java @@ -46,7 +46,7 @@ private static class BlankLinesFromCompilationUnitStyle extends JavaIsoVisitor J2 formatBlankLines(J j, Cursor cursor) { - BlankLinesStyle style = cursor.firstEnclosingOrThrow(SourceFile.class) - .getStyle(BlankLinesStyle.class); + BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class)); //noinspection unchecked return (J2) new BlankLinesVisitor<>(style == null ? IntelliJ.blankLines() : style) .visitNonNull(j, 0, cursor); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java b/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java index c7d6fc7d479..f417198b8de 100755 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java @@ -62,7 +62,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { public J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { SourceFile cu = (SourceFile) requireNonNull(tree); - spacesStyle = Optional.ofNullable(cu.getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces()); + spacesStyle = Optional.ofNullable(Style.from(SpacesStyle.class, cu)).orElse(IntelliJ.spaces()); methodParamPadStyle = Optional.ofNullable(Style.from(MethodParamPadStyle.class, cu)).orElse(Checkstyle.methodParamPadStyle()); spacesStyle = spacesStyle.withBeforeParentheses( diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java index 12c35a748aa..41e13c8a554 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java @@ -48,7 +48,7 @@ private static class LineBreaksFromCompilationUnitStyle extends JavaIsoVisitor getVisitor() { public J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { SourceFile cu = (SourceFile) requireNonNull(tree); - if (cu.getStyle(EmptyForIteratorPadStyle.class) != null || cu.getStyle(EmptyForInitializerPadStyle.class) != null) { + if (Style.from(EmptyForIteratorPadStyle.class, cu) != null || Style.from(EmptyForInitializerPadStyle.class, cu) != null) { return cu.withMarkers(cu.getMarkers().add(new SearchResult(randomId(), null))); } return (JavaSourceFile) cu; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java b/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java index 6ae901244e7..c238512a74c 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java @@ -39,7 +39,7 @@ private ShiftFormat() { public static J2 indent(J j, Cursor cursor, int shift) { JavaSourceFile cu = cursor.firstEnclosingOrThrow(JavaSourceFile.class); - TabsAndIndentsStyle tabsAndIndents = Optional.ofNullable(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)) + TabsAndIndentsStyle tabsAndIndents = Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.tabsAndIndents()); //noinspection unchecked diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java b/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java index b7130956821..fb5288dc666 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java @@ -50,12 +50,12 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { JavaSourceFile cu = (JavaSourceFile) tree; - SpacesStyle style = cu.getStyle(SpacesStyle.class); + SpacesStyle style = Style.from(SpacesStyle.class, cu); if (style == null) { style = IntelliJ.spaces(); } - return new SpacesVisitor<>(style, cu.getStyle(EmptyForInitializerPadStyle.class), - cu.getStyle(EmptyForIteratorPadStyle.class)).visit(cu, ctx); + return new SpacesVisitor<>(style, Style.from(EmptyForInitializerPadStyle.class, cu), + Style.from(EmptyForIteratorPadStyle.class, cu)).visit(cu, ctx); } return super.visit(tree, ctx); } @@ -64,7 +64,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { public static J2 formatSpaces(J j, Cursor cursor) { SourceFile cu = cursor.firstEnclosingOrThrow(SourceFile.class); - SpacesStyle style = cu.getStyle(SpacesStyle.class); + SpacesStyle style = Style.from(SpacesStyle.class, cu); //noinspection unchecked return (J2) new SpacesVisitor<>(style == null ? IntelliJ.spaces() : style, Style.from(EmptyForInitializerPadStyle.class, cu), diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java b/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java index 5c865982234..fd31e77c68f 100755 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java @@ -55,8 +55,8 @@ private static class TypecastParenPadVisitor extends JavaIsoVisitor classpath = cu.getMarkers().findFirst(JavaSourceSet.class) @@ -192,7 +192,7 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String List> newImports = layoutStyle.addImport(cu.getPadding().getImports(), importToAdd, cu.getPackageDeclaration(), classpath); // ImportLayoutStyle::addImport adds always `\n` as newlines. Checking if we need to fix them - GeneralFormatStyle generalFormatStyle = Optional.ofNullable(cu.getStyle(GeneralFormatStyle.class)) + GeneralFormatStyle generalFormatStyle = Optional.ofNullable(Style.from(GeneralFormatStyle.class, cu)) .orElse(autodetectGeneralFormatStyle(cu)); newImports = checkCRLF(newImports, generalFormatStyle); diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java index 6c4b5e67630..6f68edfba74 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java @@ -53,30 +53,30 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) { t = new MinimumViableSpacingVisitor<>(stopAfter).visit(t, p, cursor.fork()); - t = new BlankLinesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(BlankLinesStyle.class)) + t = new BlankLinesVisitor<>(Optional.ofNullable(Style.from(BlankLinesStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.blankLines()), stopAfter) .visit(t, p, cursor.fork()); - t = new WrappingAndBracesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)) + t = new WrappingAndBracesVisitor<>(Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.wrappingAndBraces()), stopAfter) .visit(t, p, cursor.fork()); t = new SpacesVisitor<>( - Optional.ofNullable(((SourceFile) cu).getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces()), + Optional.ofNullable(Style.from(SpacesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.spaces()), stopAfter ).visit(t, p, cursor.fork()); - t = new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)) + t = new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.tabsAndIndents()), stopAfter) .visit(t, p, cursor.fork()); t = new TabsAndIndentsVisitor<>( - Optional.ofNullable(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents()), - Optional.ofNullable(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)).orElse(IntelliJ.wrappingAndBraces()), + Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))).orElse(IntelliJ.tabsAndIndents()), + Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.wrappingAndBraces()), stopAfter ).visit(t, p, cursor.fork()); - t = new NormalizeLineBreaksVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(GeneralFormatStyle.class)) + t = new NormalizeLineBreaksVisitor<>(Optional.ofNullable(Style.from(GeneralFormatStyle.class, ((SourceFile) cu))) .orElse(new GeneralFormatStyle(false)), stopAfter) .visit(t, p, cursor.fork()); @@ -98,26 +98,26 @@ public J visit(@Nullable Tree tree, P p) { JavaSourceFile t = (JavaSourceFile) new RemoveTrailingWhitespaceVisitor<>(stopAfter).visit(cu, p); - t = (JavaSourceFile) new BlankLinesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(BlankLinesStyle.class)) + t = (JavaSourceFile) new BlankLinesVisitor<>(Optional.ofNullable(Style.from(BlankLinesStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.blankLines()), stopAfter) .visit(t, p); t = (JavaSourceFile) new SpacesVisitor

(Optional.ofNullable( - ((SourceFile) cu).getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces()), + Style.from(SpacesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.spaces()), stopAfter) .visit(t, p); - t = (JavaSourceFile) new WrappingAndBracesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)) + t = (JavaSourceFile) new WrappingAndBracesVisitor<>(Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.wrappingAndBraces()), stopAfter) .visit(t, p); - t = (JavaSourceFile) new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)) + t = (JavaSourceFile) new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))) .orElse(IntelliJ.tabsAndIndents()), stopAfter) .visit(t, p); t = (JavaSourceFile) new TabsAndIndentsVisitor<>( - Optional.ofNullable(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents()), - Optional.ofNullable(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)).orElse(IntelliJ.wrappingAndBraces()), + Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))).orElse(IntelliJ.tabsAndIndents()), + Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.wrappingAndBraces()), stopAfter ).visit(t, p); diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java index fe21fc8a51f..64222bf3e2e 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java @@ -32,7 +32,7 @@ public class ImportReorderingVisitor

extends KotlinIsoVisitor

{ public K.CompilationUnit visitCompilationUnit(K.CompilationUnit cu, P p) { List> importList = cu.getPadding().getImports(); - ImportLayoutStyle layoutStyle = Optional.ofNullable(cu.getStyle(ImportLayoutStyle.class)) + ImportLayoutStyle layoutStyle = Optional.ofNullable(Style.from(ImportLayoutStyle.class, cu)) .orElse(IntelliJ.importLayout()); List> ordered = layoutStyle.orderImports(importList, new HashSet<>()); diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java index 48be5b6361a..e415aa97c2a 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java @@ -31,7 +31,7 @@ public class SpacesFromCompilationUnitStyle extends KotlinIsoVisitor IntelliJ.spaces()); return new SpacesVisitor<>(style).visitNonNull(cu, getCursor().fork()); } } diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java b/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java index 3bd5d105e6e..17951d38e99 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java @@ -44,7 +44,7 @@ public LineBreaksFromCompilationUnitStyle getVisitor() { private static class LineBreaksFromCompilationUnitStyle extends XmlIsoVisitor { @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { - GeneralFormatStyle generalFormatStyle = document.getStyle(GeneralFormatStyle.class); + GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, document); if (generalFormatStyle == null) { generalFormatStyle = autodetectGeneralFormatStyle(document); } From f1d257251c590b99c094535429ba5e17e66f1ec4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 30 Sep 2025 16:19:08 +0200 Subject: [PATCH 2/5] Resolve build warnings --- .../main/java/org/openrewrite/groovy/marker/RedundantDef.java | 2 +- .../org/openrewrite/java/SimplifySingleElementAnnotation.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/marker/RedundantDef.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/marker/RedundantDef.java index 98dab8d8225..d6c4dde5af0 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/marker/RedundantDef.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/marker/RedundantDef.java @@ -26,7 +26,7 @@ /** * In Groovy methods can be declared with a return type and also a redundant 'def' keyword. * This captures the extra def keyword. - * @deprecated The `def` keyword is now parsed as a {@link J.Modifier.Type.LanguageExtension} type. + * @deprecated The `def` keyword is now parsed as a {@link J.Modifier.Type#LanguageExtension} type. */ @Value @With diff --git a/rewrite-java/src/main/java/org/openrewrite/java/SimplifySingleElementAnnotation.java b/rewrite-java/src/main/java/org/openrewrite/java/SimplifySingleElementAnnotation.java index 4e3c71300f9..66e73b4c398 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/SimplifySingleElementAnnotation.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/SimplifySingleElementAnnotation.java @@ -15,6 +15,7 @@ */ package org.openrewrite.java; +import lombok.EqualsAndHashCode; import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; @@ -50,6 +51,7 @@ public static TreeVisitor modifyOnly(J2 scop } @Value + @EqualsAndHashCode(callSuper = false) private static class SimplifySingleElementAnnotationVisitor extends JavaIsoVisitor { @Nullable J scope; From 41122d7a114bc26eb5dec04bb438157e8c4918b2 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 30 Sep 2025 16:19:21 +0200 Subject: [PATCH 3/5] Mark methods for replacement --- rewrite-core/src/main/java/org/openrewrite/SourceFile.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rewrite-core/src/main/java/org/openrewrite/SourceFile.java b/rewrite-core/src/main/java/org/openrewrite/SourceFile.java index 88f6c7e8e53..d74ae99d386 100644 --- a/rewrite-core/src/main/java/org/openrewrite/SourceFile.java +++ b/rewrite-core/src/main/java/org/openrewrite/SourceFile.java @@ -15,6 +15,7 @@ */ package org.openrewrite; +import com.google.errorprone.annotations.InlineMe; import org.jspecify.annotations.Nullable; import org.openrewrite.internal.StringUtils; import org.openrewrite.style.Style; @@ -74,6 +75,7 @@ default boolean printEqualsInput(Parser.Input input, ExecutionContext ctx) { * @deprecated Use {@link org.openrewrite.style.Style#from(Class, SourceFile)} instead. */ @Deprecated + @InlineMe(replacement = "Style.from(styleClass, this)", imports = "org.openrewrite.style.Style") default @Nullable S getStyle(Class styleClass) { return Style.from(styleClass, this); } @@ -82,6 +84,7 @@ default boolean printEqualsInput(Parser.Input input, ExecutionContext ctx) { * @deprecated Use {@link org.openrewrite.style.Style#from(Class, SourceFile, Supplier)} instead. */ @Deprecated + @InlineMe(replacement = "Style.from(styleClass, this, () -> defaultStyle)", imports = "org.openrewrite.style.Style") default S getStyle(Class styleClass, S defaultStyle) { return Style.from(styleClass, this, () -> defaultStyle); } From 3788c59580f2149fd2eeb31a197567c6dd383264 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 30 Sep 2025 16:34:01 +0200 Subject: [PATCH 4/5] Use default value instead of `orElse` or null checks --- .../groovy/format/OmitParenthesesFormat.java | 3 +- .../openrewrite/hcl/format/BlankLines.java | 6 +- .../org/openrewrite/hcl/format/Spaces.java | 6 +- .../hcl/format/TabsAndIndents.java | 6 +- .../org/openrewrite/java/RemoveImport.java | 4 +- .../openrewrite/java/format/BlankLines.java | 11 ++- .../java/format/MethodParamPad.java | 4 +- .../java/format/NormalizeLineBreaks.java | 6 +- .../java/format/NormalizeTabsOrSpaces.java | 6 +- .../openrewrite/java/format/ShiftFormat.java | 4 +- .../org/openrewrite/java/format/Spaces.java | 11 ++- .../java/format/TypecastParenPad.java | 6 +- .../java/format/WrappingAndBraces.java | 5 +- .../org/openrewrite/kotlin/AddImport.java | 8 +-- .../kotlin/format/AutoFormatVisitor.java | 67 ++++++------------- .../format/ImportReorderingVisitor.java | 5 +- .../SpacesFromCompilationUnitStyle.java | 3 +- .../xml/format/NormalizeLineBreaks.java | 9 +-- 18 files changed, 61 insertions(+), 109 deletions(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java index 047dce8bf49..2977758cc50 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java @@ -21,6 +21,7 @@ import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; +import org.openrewrite.style.Style; import java.util.Optional; @@ -47,7 +48,7 @@ private static class OmitParenthesesFromCompilationUnitStyle extends JavaIsoVisi public J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { SourceFile cu = (SourceFile) requireNonNull(tree); - OmitParenthesesStyle style = Optional.ofNullable(Style.from(OmitParenthesesStyle.class, cu)).orElse(OmitParenthesesStyle.DEFAULT); + OmitParenthesesStyle style = Style.from(OmitParenthesesStyle.class, cu, () -> OmitParenthesesStyle.DEFAULT); if (style.getLastArgumentLambda()) { doAfterVisit(new OmitParenthesesForLastArgumentLambda().getVisitor()); } diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java index 2eab17aadff..e9768ee79db 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java @@ -18,6 +18,7 @@ import org.openrewrite.*; import org.openrewrite.hcl.HclIsoVisitor; import org.openrewrite.hcl.tree.Hcl; +import org.openrewrite.style.Style; public class BlankLines extends Recipe { @@ -39,10 +40,7 @@ public TreeVisitor getVisitor() { private static class BlankLinesFromCompilationUnitStyle extends HclIsoVisitor { @Override public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile configFile, ExecutionContext ctx) { - BlankLinesStyle style = Style.from(BlankLinesStyle.class, configFile); - if (style == null) { - style = BlankLinesStyle.DEFAULT; - } + BlankLinesStyle style = Style.from(BlankLinesStyle.class, configFile, () -> BlankLinesStyle.DEFAULT); return (Hcl.ConfigFile) new BlankLinesVisitor<>(style).visitNonNull(configFile, ctx); } } diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java index 60afe679a63..f4526074315 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java @@ -21,6 +21,7 @@ import org.openrewrite.hcl.HclIsoVisitor; import org.openrewrite.hcl.style.SpacesStyle; import org.openrewrite.hcl.tree.Hcl; +import org.openrewrite.style.Style; public class Spaces extends Recipe { @@ -42,10 +43,7 @@ public TreeVisitor getVisitor() { private static class SpacesFromCompilationUnitStyle extends HclIsoVisitor { @Override public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) { - SpacesStyle style = Style.from(SpacesStyle.class, cf); - if (style == null) { - style = SpacesStyle.DEFAULT; - } + SpacesStyle style = Style.from(SpacesStyle.class, cf, () -> SpacesStyle.DEFAULT); return (Hcl.ConfigFile) new SpacesVisitor<>(style).visitNonNull(cf, ctx); } } diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java index dd0073680aa..63e2a0d520d 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java @@ -21,6 +21,7 @@ import org.openrewrite.hcl.HclIsoVisitor; import org.openrewrite.hcl.style.TabsAndIndentsStyle; import org.openrewrite.hcl.tree.Hcl; +import org.openrewrite.style.Style; public class TabsAndIndents extends Recipe { @@ -42,10 +43,7 @@ public TreeVisitor getVisitor() { private static class TabsAndIndentsFromCompilationUnitStyle extends HclIsoVisitor { @Override public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) { - TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cf); - if (style == null) { - style = TabsAndIndentsStyle.DEFAULT; - } + TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cf, () -> TabsAndIndentsStyle.DEFAULT); return (Hcl.ConfigFile) new TabsAndIndentsVisitor<>(style).visitNonNull(cf, ctx); } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java index 331656b4579..dd986305730 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java @@ -24,6 +24,7 @@ import org.openrewrite.java.style.ImportLayoutStyle; import org.openrewrite.java.style.IntelliJ; import org.openrewrite.java.tree.*; +import org.openrewrite.style.Style; import java.util.*; import java.util.concurrent.atomic.AtomicReference; @@ -58,8 +59,7 @@ public RemoveImport(String type, boolean force) { J j = tree; if (tree instanceof JavaSourceFile) { JavaSourceFile cu = (JavaSourceFile) tree; - ImportLayoutStyle importLayoutStyle = Optional.ofNullable(Style.from(ImportLayoutStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.importLayout()); + ImportLayoutStyle importLayoutStyle = Style.from(ImportLayoutStyle.class, cu, IntelliJ::importLayout); boolean typeUsed = false; Set otherTypesInPackageUsed = new TreeSet<>(); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java index 7a67b81b652..7fadd4fb5a9 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java @@ -22,6 +22,7 @@ import org.openrewrite.java.style.IntelliJ; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; +import org.openrewrite.style.Style; import static java.util.Objects.requireNonNull; @@ -46,10 +47,7 @@ private static class BlankLinesFromCompilationUnitStyle extends JavaIsoVisitor(style).visit(cu, ctx); } return (J) tree; @@ -57,9 +55,8 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) { } public static J2 formatBlankLines(J j, Cursor cursor) { - BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class)); + BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class), IntelliJ::blankLines); //noinspection unchecked - return (J2) new BlankLinesVisitor<>(style == null ? IntelliJ.blankLines() : style) - .visitNonNull(j, 0, cursor); + return (J2) new BlankLinesVisitor<>(style).visitNonNull(j, 0, cursor); } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java b/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java index f417198b8de..6d14eafb7bc 100755 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/MethodParamPad.java @@ -62,8 +62,8 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { public J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { SourceFile cu = (SourceFile) requireNonNull(tree); - spacesStyle = Optional.ofNullable(Style.from(SpacesStyle.class, cu)).orElse(IntelliJ.spaces()); - methodParamPadStyle = Optional.ofNullable(Style.from(MethodParamPadStyle.class, cu)).orElse(Checkstyle.methodParamPadStyle()); + spacesStyle = Style.from(SpacesStyle.class, cu, IntelliJ::spaces); + methodParamPadStyle = Style.from(MethodParamPadStyle.class, cu, Checkstyle::methodParamPadStyle); spacesStyle = spacesStyle.withBeforeParentheses( spacesStyle.getBeforeParentheses() diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java index 41e13c8a554..87a3d3acae1 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaks.java @@ -21,6 +21,7 @@ import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.style.GeneralFormatStyle; +import org.openrewrite.style.Style; import static java.util.Objects.requireNonNull; import static org.openrewrite.java.format.AutodetectGeneralFormatStyle.autodetectGeneralFormatStyle; @@ -48,10 +49,7 @@ private static class LineBreaksFromCompilationUnitStyle extends JavaIsoVisitor autodetectGeneralFormatStyle(cu)); doAfterVisit(new NormalizeLineBreaksVisitor<>(generalFormatStyle)); } return (J) tree; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpaces.java b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpaces.java index c46d9f856df..c4fe93e000f 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpaces.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpaces.java @@ -22,6 +22,7 @@ import org.openrewrite.java.style.TabsAndIndentsStyle; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; +import org.openrewrite.style.Style; import static java.util.Objects.requireNonNull; @@ -47,10 +48,7 @@ private static class TabsAndIndentsFromCompilationUnitStyle extends JavaIsoVisit public J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree); - TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu)); - if (style == null) { - style = IntelliJ.tabsAndIndents(); - } + TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents); return new NormalizeTabsOrSpacesVisitor<>(style).visit(cu, ctx); } return (J) tree; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java b/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java index c238512a74c..bb26d7da533 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/ShiftFormat.java @@ -24,6 +24,7 @@ import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.java.tree.Space; +import org.openrewrite.style.Style; import java.util.Objects; import java.util.Optional; @@ -39,8 +40,7 @@ private ShiftFormat() { public static J2 indent(J j, Cursor cursor, int shift) { JavaSourceFile cu = cursor.firstEnclosingOrThrow(JavaSourceFile.class); - TabsAndIndentsStyle tabsAndIndents = Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.tabsAndIndents()); + TabsAndIndentsStyle tabsAndIndents = Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents); //noinspection unchecked return (J2) Objects.requireNonNull(new JavaIsoVisitor() { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java b/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java index fb5288dc666..f7d3b124f10 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/Spaces.java @@ -50,12 +50,11 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { JavaSourceFile cu = (JavaSourceFile) tree; - SpacesStyle style = Style.from(SpacesStyle.class, cu); - if (style == null) { - style = IntelliJ.spaces(); - } - return new SpacesVisitor<>(style, Style.from(EmptyForInitializerPadStyle.class, cu), - Style.from(EmptyForIteratorPadStyle.class, cu)).visit(cu, ctx); + return new SpacesVisitor<>( + Style.from(SpacesStyle.class, cu, IntelliJ::spaces), + Style.from(EmptyForInitializerPadStyle.class, cu), + Style.from(EmptyForIteratorPadStyle.class, cu)) + .visit(cu, ctx); } return super.visit(tree, ctx); } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java b/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java index fd31e77c68f..1798725782d 100755 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java @@ -24,6 +24,7 @@ import org.openrewrite.java.style.TypecastParenPadStyle; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; +import org.openrewrite.style.Style; import java.util.Optional; @@ -55,9 +56,8 @@ private static class TypecastParenPadVisitor extends JavaIsoVisitor(style).visit(cu, ctx); } return (J) tree; diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/AddImport.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/AddImport.java index 77c76c0ab86..d36613bbcad 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/AddImport.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/AddImport.java @@ -29,6 +29,7 @@ import org.openrewrite.kotlin.tree.K; import org.openrewrite.marker.Markers; import org.openrewrite.style.GeneralFormatStyle; +import org.openrewrite.style.Style; import java.util.*; import java.util.concurrent.atomic.AtomicReference; @@ -182,8 +183,7 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String } } - ImportLayoutStyle layoutStyle = Optional.ofNullable(Style.from(ImportLayoutStyle.class, cu)) - .orElse(IntelliJ.importLayout()); + ImportLayoutStyle layoutStyle = Style.from(ImportLayoutStyle.class, cu, IntelliJ::importLayout); List classpath = cu.getMarkers().findFirst(JavaSourceSet.class) .map(JavaSourceSet::getClasspath) @@ -192,8 +192,8 @@ public AddImport(@Nullable String packageName, String typeName, @Nullable String List> newImports = layoutStyle.addImport(cu.getPadding().getImports(), importToAdd, cu.getPackageDeclaration(), classpath); // ImportLayoutStyle::addImport adds always `\n` as newlines. Checking if we need to fix them - GeneralFormatStyle generalFormatStyle = Optional.ofNullable(Style.from(GeneralFormatStyle.class, cu)) - .orElse(autodetectGeneralFormatStyle(cu)); + K.CompilationUnit finalCu = cu; + GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, cu, ()-> autodetectGeneralFormatStyle(finalCu)); newImports = checkCRLF(newImports, generalFormatStyle); cu = cu.getPadding().withImports(newImports); diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java index 6f68edfba74..9e789bac5d1 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/AutoFormatVisitor.java @@ -18,15 +18,13 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.Cursor; -import org.openrewrite.SourceFile; import org.openrewrite.Tree; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.kotlin.KotlinIsoVisitor; import org.openrewrite.kotlin.style.*; import org.openrewrite.style.GeneralFormatStyle; - -import java.util.Optional; +import org.openrewrite.style.Style; import static java.util.Objects.requireNonNull; @@ -50,39 +48,24 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) { cursor.firstEnclosingOrThrow(JavaSourceFile.class); J t = new NormalizeFormatVisitor<>(stopAfter).visit(tree, p, cursor.fork()); - t = new MinimumViableSpacingVisitor<>(stopAfter).visit(t, p, cursor.fork()); - - t = new BlankLinesVisitor<>(Optional.ofNullable(Style.from(BlankLinesStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.blankLines()), stopAfter) + t = new BlankLinesVisitor<>(Style.from(BlankLinesStyle.class, cu, IntelliJ::blankLines), stopAfter) .visit(t, p, cursor.fork()); - - t = new WrappingAndBracesVisitor<>(Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.wrappingAndBraces()), stopAfter) + t = new WrappingAndBracesVisitor<>(Style.from(WrappingAndBracesStyle.class, cu, IntelliJ::wrappingAndBraces), stopAfter) .visit(t, p, cursor.fork()); - - t = new SpacesVisitor<>( - Optional.ofNullable(Style.from(SpacesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.spaces()), - stopAfter - ).visit(t, p, cursor.fork()); - - t = new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.tabsAndIndents()), stopAfter) + t = new SpacesVisitor<>(Style.from(SpacesStyle.class, cu, IntelliJ::spaces), stopAfter) + .visit(t, p, cursor.fork()); + t = new NormalizeTabsOrSpacesVisitor<>(Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents), stopAfter) .visit(t, p, cursor.fork()); - t = new TabsAndIndentsVisitor<>( - Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))).orElse(IntelliJ.tabsAndIndents()), - Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.wrappingAndBraces()), - stopAfter - ).visit(t, p, cursor.fork()); - - t = new NormalizeLineBreaksVisitor<>(Optional.ofNullable(Style.from(GeneralFormatStyle.class, ((SourceFile) cu))) - .orElse(new GeneralFormatStyle(false)), stopAfter) + Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents), + Style.from(WrappingAndBracesStyle.class, cu, IntelliJ::wrappingAndBraces), + stopAfter) + .visit(t, p, cursor.fork()); + t = new NormalizeLineBreaksVisitor<>(Style.from(GeneralFormatStyle.class, cu, () -> new GeneralFormatStyle(false)), stopAfter) .visit(t, p, cursor.fork()); - t = new RemoveTrailingWhitespaceVisitor<>(stopAfter).visit(t, p, cursor.fork()); - - return new ImportReorderingVisitor<>().visit(t, p, cursor.fork()); + return new ImportReorderingVisitor<>().visitNonNull(t, p, cursor.fork()); } @Override @@ -97,34 +80,22 @@ public J visit(@Nullable Tree tree, P p) { } JavaSourceFile t = (JavaSourceFile) new RemoveTrailingWhitespaceVisitor<>(stopAfter).visit(cu, p); - - t = (JavaSourceFile) new BlankLinesVisitor<>(Optional.ofNullable(Style.from(BlankLinesStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.blankLines()), stopAfter) + t = (JavaSourceFile) new BlankLinesVisitor<>(Style.from(BlankLinesStyle.class, cu, IntelliJ::blankLines), stopAfter) .visit(t, p); - - t = (JavaSourceFile) new SpacesVisitor

(Optional.ofNullable( - Style.from(SpacesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.spaces()), + t = (JavaSourceFile) new SpacesVisitor

(Style.from(SpacesStyle.class, cu, IntelliJ::spaces), stopAfter) .visit(t, p); - - t = (JavaSourceFile) new WrappingAndBracesVisitor<>(Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.wrappingAndBraces()), stopAfter) + t = (JavaSourceFile) new WrappingAndBracesVisitor<>(Style.from(WrappingAndBracesStyle.class, cu, IntelliJ::wrappingAndBraces), stopAfter) .visit(t, p); - - t = (JavaSourceFile) new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))) - .orElse(IntelliJ.tabsAndIndents()), stopAfter) + t = (JavaSourceFile) new NormalizeTabsOrSpacesVisitor<>(Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents), stopAfter) .visit(t, p); - t = (JavaSourceFile) new TabsAndIndentsVisitor<>( - Optional.ofNullable(Style.from(TabsAndIndentsStyle.class, ((SourceFile) cu))).orElse(IntelliJ.tabsAndIndents()), - Optional.ofNullable(Style.from(WrappingAndBracesStyle.class, ((SourceFile) cu))).orElse(IntelliJ.wrappingAndBraces()), + Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents), + Style.from(WrappingAndBracesStyle.class, cu, IntelliJ::wrappingAndBraces), stopAfter ).visit(t, p); - t = (JavaSourceFile) new TrailingCommaVisitor<>(IntelliJ.other().getUseTrailingComma()).visit(t, p); - - assert t != null; - return t; + return new TrailingCommaVisitor<>(IntelliJ.other().getUseTrailingComma()).visitNonNull(t, p); } return (J) tree; } diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java index 64222bf3e2e..5003f19ee84 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/ImportReorderingVisitor.java @@ -21,6 +21,7 @@ import org.openrewrite.kotlin.style.ImportLayoutStyle; import org.openrewrite.kotlin.style.IntelliJ; import org.openrewrite.kotlin.tree.K; +import org.openrewrite.style.Style; import java.util.HashSet; import java.util.List; @@ -32,9 +33,7 @@ public class ImportReorderingVisitor

extends KotlinIsoVisitor

{ public K.CompilationUnit visitCompilationUnit(K.CompilationUnit cu, P p) { List> importList = cu.getPadding().getImports(); - ImportLayoutStyle layoutStyle = Optional.ofNullable(Style.from(ImportLayoutStyle.class, cu)) - .orElse(IntelliJ.importLayout()); - + ImportLayoutStyle layoutStyle = Style.from(ImportLayoutStyle.class, cu, IntelliJ::importLayout); List> ordered = layoutStyle.orderImports(importList, new HashSet<>()); if (referentialIdentical(importList, ordered)) { diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java index e415aa97c2a..890c75c96bf 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/format/SpacesFromCompilationUnitStyle.java @@ -23,6 +23,7 @@ import org.openrewrite.kotlin.style.IntelliJ; import org.openrewrite.kotlin.style.SpacesStyle; import org.openrewrite.kotlin.tree.K; +import org.openrewrite.style.Style; public class SpacesFromCompilationUnitStyle extends KotlinIsoVisitor { @Override @@ -31,7 +32,7 @@ public class SpacesFromCompilationUnitStyle extends KotlinIsoVisitor IntelliJ.spaces()); + SpacesStyle style = Style.from(SpacesStyle.class, cu, IntelliJ::spaces); return new SpacesVisitor<>(style).visitNonNull(cu, getCursor().fork()); } } diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java b/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java index 17951d38e99..bf2f74d0e22 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/format/NormalizeLineBreaks.java @@ -18,6 +18,7 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; import org.openrewrite.style.GeneralFormatStyle; +import org.openrewrite.style.Style; import org.openrewrite.xml.XmlIsoVisitor; import org.openrewrite.xml.tree.Xml; @@ -37,18 +38,14 @@ public String getDescription() { } @Override - public LineBreaksFromCompilationUnitStyle getVisitor() { + public XmlIsoVisitor getVisitor() { return new LineBreaksFromCompilationUnitStyle(); } private static class LineBreaksFromCompilationUnitStyle extends XmlIsoVisitor { @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { - GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, document); - if (generalFormatStyle == null) { - generalFormatStyle = autodetectGeneralFormatStyle(document); - } - + GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, document, () -> autodetectGeneralFormatStyle(document)); doAfterVisit(new NormalizeLineBreaksVisitor<>(generalFormatStyle)); return document; } From 3807c5213f40cdb3af51d6055eed9fe51797865f Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 30 Sep 2025 16:35:51 +0200 Subject: [PATCH 5/5] Use consistent indentation --- .../java/org/openrewrite/groovy/format/AutoFormatVisitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java index e71e2a55adb..146ec94a259 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java @@ -62,7 +62,7 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) { t = new SpacesVisitor<>( spacesStyle, - Style.from(EmptyForInitializerPadStyle.class, cu), + Style.from(EmptyForInitializerPadStyle.class, cu), Style.from(EmptyForIteratorPadStyle.class, cu), stopAfter ).visit(t, p, cursor.fork());