diff --git a/rewrite-core/src/main/java/org/openrewrite/SourceFile.java b/rewrite-core/src/main/java/org/openrewrite/SourceFile.java index 88f6c7e8e5..d74ae99d38 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); } 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 bcf98d05c5..146ec94a25 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 1f2ed42f5f..2977758cc5 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(cu.getStyle(OmitParenthesesStyle.class)).orElse(OmitParenthesesStyle.DEFAULT); + OmitParenthesesStyle style = Style.from(OmitParenthesesStyle.class, cu, () -> OmitParenthesesStyle.DEFAULT); if (style.getLastArgumentLambda()) { doAfterVisit(new OmitParenthesesForLastArgumentLambda().getVisitor()); } 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 98dab8d822..d6c4dde5af 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-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java index c6c5a37e29..e9768ee79d 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,17 +40,13 @@ 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); - if (style == null) { - style = BlankLinesStyle.DEFAULT; - } + BlankLinesStyle style = Style.from(BlankLinesStyle.class, configFile, () -> BlankLinesStyle.DEFAULT); return (Hcl.ConfigFile) new BlankLinesVisitor<>(style).visitNonNull(configFile, ctx); } } 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 f379f1d04a..f452607431 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 = cf.getStyle(SpacesStyle.class); - 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 f177a6e1f9..63e2a0d520 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 = cf.getStyle(TabsAndIndentsStyle.class); - 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 22787f8689..dd98630573 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(((SourceFile) cu).getStyle(ImportLayoutStyle.class)) - .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/SimplifySingleElementAnnotation.java b/rewrite-java/src/main/java/org/openrewrite/java/SimplifySingleElementAnnotation.java index 4e3c71300f..66e73b4c39 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; 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 366314ec39..7fadd4fb5a 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,10 +55,8 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) { } public static 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), 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 c7d6fc7d47..6d14eafb7b 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(cu.getStyle(SpacesStyle.class)).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 12c35a748a..87a3d3acae 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 e9c215464a..c4fe93e000 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 = ((SourceFile) cu).getStyle(TabsAndIndentsStyle.class); - 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/PadEmptyForLoopComponents.java b/rewrite-java/src/main/java/org/openrewrite/java/format/PadEmptyForLoopComponents.java index 9b8621d3fc..4980a76627 100755 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/PadEmptyForLoopComponents.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/PadEmptyForLoopComponents.java @@ -52,7 +52,7 @@ public TreeVisitor 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 6ae901244e..bb26d7da53 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(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)) - .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 b713095682..f7d3b124f1 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 = cu.getStyle(SpacesStyle.class); - 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.from(SpacesStyle.class, cu, IntelliJ::spaces), + Style.from(EmptyForInitializerPadStyle.class, cu), + Style.from(EmptyForIteratorPadStyle.class, cu)) + .visit(cu, ctx); } return super.visit(tree, ctx); } @@ -64,7 +63,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 5c86598223..1798725782 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 488814b2c9..d36613bbca 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(cu.getStyle(ImportLayoutStyle.class)) - .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(cu.getStyle(GeneralFormatStyle.class)) - .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 6c4b5e6763..9e789bac5d 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(((SourceFile) cu).getStyle(BlankLinesStyle.class)) - .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(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)) - .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(((SourceFile) cu).getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces()), - stopAfter - ).visit(t, p, cursor.fork()); - - t = new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)) - .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(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents()), - Optional.ofNullable(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)).orElse(IntelliJ.wrappingAndBraces()), - stopAfter - ).visit(t, p, cursor.fork()); - - t = new NormalizeLineBreaksVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(GeneralFormatStyle.class)) - .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(((SourceFile) cu).getStyle(BlankLinesStyle.class)) - .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( - ((SourceFile) cu).getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces()), + t = (JavaSourceFile) new SpacesVisitor

(Style.from(SpacesStyle.class, cu, IntelliJ::spaces), stopAfter) .visit(t, p); - - t = (JavaSourceFile) new WrappingAndBracesVisitor<>(Optional.ofNullable(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)) - .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(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)) - .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(((SourceFile) cu).getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents()), - Optional.ofNullable(((SourceFile) cu).getStyle(WrappingAndBracesStyle.class)).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 fe21fc8a51..5003f19ee8 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(cu.getStyle(ImportLayoutStyle.class)) - .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 48be5b6361..890c75c96b 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(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 3bd5d105e6..bf2f74d0e2 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 = document.getStyle(GeneralFormatStyle.class); - if (generalFormatStyle == null) { - generalFormatStyle = autodetectGeneralFormatStyle(document); - } - + GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, document, () -> autodetectGeneralFormatStyle(document)); doAfterVisit(new NormalizeLineBreaksVisitor<>(generalFormatStyle)); return document; }