Skip to content

Commit ad2b7d9

Browse files
Adopt Style.from instead of SourceFile.getStyle (#6093)
* 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 <team@moderne.io> * Resolve build warnings * Mark methods for replacement * Use default value instead of `orElse` or null checks * Use consistent indentation --------- Co-authored-by: Moderne <team@moderne.io>
1 parent 1419a85 commit ad2b7d9

23 files changed

Lines changed: 71 additions & 116 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/SourceFile.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite;
1717

18+
import com.google.errorprone.annotations.InlineMe;
1819
import org.jspecify.annotations.Nullable;
1920
import org.openrewrite.internal.StringUtils;
2021
import org.openrewrite.style.Style;
@@ -74,6 +75,7 @@ default boolean printEqualsInput(Parser.Input input, ExecutionContext ctx) {
7475
* @deprecated Use {@link org.openrewrite.style.Style#from(Class, SourceFile)} instead.
7576
*/
7677
@Deprecated
78+
@InlineMe(replacement = "Style.from(styleClass, this)", imports = "org.openrewrite.style.Style")
7779
default <S extends Style> @Nullable S getStyle(Class<S> styleClass) {
7880
return Style.from(styleClass, this);
7981
}
@@ -82,6 +84,7 @@ default boolean printEqualsInput(Parser.Input input, ExecutionContext ctx) {
8284
* @deprecated Use {@link org.openrewrite.style.Style#from(Class, SourceFile, Supplier)} instead.
8385
*/
8486
@Deprecated
87+
@InlineMe(replacement = "Style.from(styleClass, this, () -> defaultStyle)", imports = "org.openrewrite.style.Style")
8588
default <S extends Style> S getStyle(Class<S> styleClass, S defaultStyle) {
8689
return Style.from(styleClass, this, () -> defaultStyle);
8790
}

rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public J visit(@Nullable Tree tree, P p, Cursor cursor) {
6262

6363
t = new SpacesVisitor<>(
6464
spacesStyle,
65-
cu.getStyle(EmptyForInitializerPadStyle.class),
65+
Style.from(EmptyForInitializerPadStyle.class, cu),
6666
Style.from(EmptyForIteratorPadStyle.class, cu),
6767
stopAfter
6868
).visit(t, p, cursor.fork());

rewrite-groovy/src/main/java/org/openrewrite/groovy/format/OmitParenthesesFormat.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.java.JavaIsoVisitor;
2222
import org.openrewrite.java.tree.J;
2323
import org.openrewrite.java.tree.JavaSourceFile;
24+
import org.openrewrite.style.Style;
2425

2526
import java.util.Optional;
2627

@@ -47,7 +48,7 @@ private static class OmitParenthesesFromCompilationUnitStyle extends JavaIsoVisi
4748
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
4849
if (tree instanceof JavaSourceFile) {
4950
SourceFile cu = (SourceFile) requireNonNull(tree);
50-
OmitParenthesesStyle style = Optional.ofNullable(cu.getStyle(OmitParenthesesStyle.class)).orElse(OmitParenthesesStyle.DEFAULT);
51+
OmitParenthesesStyle style = Style.from(OmitParenthesesStyle.class, cu, () -> OmitParenthesesStyle.DEFAULT);
5152
if (style.getLastArgumentLambda()) {
5253
doAfterVisit(new OmitParenthesesForLastArgumentLambda().getVisitor());
5354
}

rewrite-groovy/src/main/java/org/openrewrite/groovy/marker/RedundantDef.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* In Groovy methods can be declared with a return type and also a redundant 'def' keyword.
2828
* This captures the extra def keyword.
29-
* @deprecated The `def` keyword is now parsed as a {@link J.Modifier.Type.LanguageExtension} type.
29+
* @deprecated The `def` keyword is now parsed as a {@link J.Modifier.Type#LanguageExtension} type.
3030
*/
3131
@Value
3232
@With

rewrite-hcl/src/main/java/org/openrewrite/hcl/format/BlankLines.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.openrewrite.*;
1919
import org.openrewrite.hcl.HclIsoVisitor;
2020
import org.openrewrite.hcl.tree.Hcl;
21+
import org.openrewrite.style.Style;
2122

2223
public class BlankLines extends Recipe {
2324

@@ -39,17 +40,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
3940
private static class BlankLinesFromCompilationUnitStyle extends HclIsoVisitor<ExecutionContext> {
4041
@Override
4142
public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile configFile, ExecutionContext ctx) {
42-
BlankLinesStyle style = configFile.getStyle(BlankLinesStyle.class);
43-
if (style == null) {
44-
style = BlankLinesStyle.DEFAULT;
45-
}
43+
BlankLinesStyle style = Style.from(BlankLinesStyle.class, configFile, () -> BlankLinesStyle.DEFAULT);
4644
return (Hcl.ConfigFile) new BlankLinesVisitor<>(style).visitNonNull(configFile, ctx);
4745
}
4846
}
4947

5048
public static <H extends Hcl> H formatBlankLines(Hcl j, Cursor cursor) {
51-
BlankLinesStyle style = cursor.firstEnclosingOrThrow(SourceFile.class)
52-
.getStyle(BlankLinesStyle.class);
49+
BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class));
5350
//noinspection unchecked
5451
return (H) new BlankLinesVisitor<>(style == null ? BlankLinesStyle.DEFAULT : style)
5552
.visitNonNull(j, 0, cursor);

rewrite-hcl/src/main/java/org/openrewrite/hcl/format/Spaces.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.hcl.HclIsoVisitor;
2222
import org.openrewrite.hcl.style.SpacesStyle;
2323
import org.openrewrite.hcl.tree.Hcl;
24+
import org.openrewrite.style.Style;
2425

2526
public class Spaces extends Recipe {
2627

@@ -42,10 +43,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4243
private static class SpacesFromCompilationUnitStyle extends HclIsoVisitor<ExecutionContext> {
4344
@Override
4445
public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) {
45-
SpacesStyle style = cf.getStyle(SpacesStyle.class);
46-
if (style == null) {
47-
style = SpacesStyle.DEFAULT;
48-
}
46+
SpacesStyle style = Style.from(SpacesStyle.class, cf, () -> SpacesStyle.DEFAULT);
4947
return (Hcl.ConfigFile) new SpacesVisitor<>(style).visitNonNull(cf, ctx);
5048
}
5149
}

rewrite-hcl/src/main/java/org/openrewrite/hcl/format/TabsAndIndents.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.hcl.HclIsoVisitor;
2222
import org.openrewrite.hcl.style.TabsAndIndentsStyle;
2323
import org.openrewrite.hcl.tree.Hcl;
24+
import org.openrewrite.style.Style;
2425

2526
public class TabsAndIndents extends Recipe {
2627

@@ -42,10 +43,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4243
private static class TabsAndIndentsFromCompilationUnitStyle extends HclIsoVisitor<ExecutionContext> {
4344
@Override
4445
public Hcl.ConfigFile visitConfigFile(Hcl.ConfigFile cf, ExecutionContext ctx) {
45-
TabsAndIndentsStyle style = cf.getStyle(TabsAndIndentsStyle.class);
46-
if (style == null) {
47-
style = TabsAndIndentsStyle.DEFAULT;
48-
}
46+
TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cf, () -> TabsAndIndentsStyle.DEFAULT);
4947
return (Hcl.ConfigFile) new TabsAndIndentsVisitor<>(style).visitNonNull(cf, ctx);
5048
}
5149
}

rewrite-java/src/main/java/org/openrewrite/java/RemoveImport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openrewrite.java.style.ImportLayoutStyle;
2525
import org.openrewrite.java.style.IntelliJ;
2626
import org.openrewrite.java.tree.*;
27+
import org.openrewrite.style.Style;
2728

2829
import java.util.*;
2930
import java.util.concurrent.atomic.AtomicReference;
@@ -58,8 +59,7 @@ public RemoveImport(String type, boolean force) {
5859
J j = tree;
5960
if (tree instanceof JavaSourceFile) {
6061
JavaSourceFile cu = (JavaSourceFile) tree;
61-
ImportLayoutStyle importLayoutStyle = Optional.ofNullable(((SourceFile) cu).getStyle(ImportLayoutStyle.class))
62-
.orElse(IntelliJ.importLayout());
62+
ImportLayoutStyle importLayoutStyle = Style.from(ImportLayoutStyle.class, cu, IntelliJ::importLayout);
6363

6464
boolean typeUsed = false;
6565
Set<String> otherTypesInPackageUsed = new TreeSet<>();

rewrite-java/src/main/java/org/openrewrite/java/SimplifySingleElementAnnotation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java;
1717

18+
import lombok.EqualsAndHashCode;
1819
import lombok.Value;
1920
import org.jspecify.annotations.Nullable;
2021
import org.openrewrite.ExecutionContext;
@@ -50,6 +51,7 @@ public static <J2 extends J> TreeVisitor<?, ExecutionContext> modifyOnly(J2 scop
5051
}
5152

5253
@Value
54+
@EqualsAndHashCode(callSuper = false)
5355
private static class SimplifySingleElementAnnotationVisitor extends JavaIsoVisitor<ExecutionContext> {
5456
@Nullable
5557
J scope;

rewrite-java/src/main/java/org/openrewrite/java/format/BlankLines.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.java.style.IntelliJ;
2323
import org.openrewrite.java.tree.J;
2424
import org.openrewrite.java.tree.JavaSourceFile;
25+
import org.openrewrite.style.Style;
2526

2627
import static java.util.Objects.requireNonNull;
2728

@@ -46,21 +47,16 @@ private static class BlankLinesFromCompilationUnitStyle extends JavaIsoVisitor<E
4647
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
4748
if (tree instanceof JavaSourceFile) {
4849
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
49-
BlankLinesStyle style = cu.getStyle(BlankLinesStyle.class);
50-
if (style == null) {
51-
style = IntelliJ.blankLines();
52-
}
50+
BlankLinesStyle style = Style.from(BlankLinesStyle.class, cu, IntelliJ::blankLines);
5351
return new BlankLinesVisitor<>(style).visit(cu, ctx);
5452
}
5553
return (J) tree;
5654
}
5755
}
5856

5957
public static <J2 extends J> J2 formatBlankLines(J j, Cursor cursor) {
60-
BlankLinesStyle style = cursor.firstEnclosingOrThrow(SourceFile.class)
61-
.getStyle(BlankLinesStyle.class);
58+
BlankLinesStyle style = Style.from(BlankLinesStyle.class, cursor.firstEnclosingOrThrow(SourceFile.class), IntelliJ::blankLines);
6259
//noinspection unchecked
63-
return (J2) new BlankLinesVisitor<>(style == null ? IntelliJ.blankLines() : style)
64-
.visitNonNull(j, 0, cursor);
60+
return (J2) new BlankLinesVisitor<>(style).visitNonNull(j, 0, cursor);
6561
}
6662
}

0 commit comments

Comments
 (0)