diff --git a/src/main/java/org/openrewrite/java/migrate/UseTabsOrSpaces.java b/src/main/java/org/openrewrite/java/migrate/UseTabsOrSpaces.java index d991c18439..78909735a0 100644 --- a/src/main/java/org/openrewrite/java/migrate/UseTabsOrSpaces.java +++ b/src/main/java/org/openrewrite/java/migrate/UseTabsOrSpaces.java @@ -25,6 +25,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; @@ -43,7 +44,7 @@ public String getDisplayName() { @Override public String getDescription() { return "This is useful for one-off migrations of a codebase that has mixed indentation styles, while " + - "preserving all other auto-detected formatting rules."; + "preserving all other auto-detected formatting rules."; } @Override @@ -53,11 +54,8 @@ public TreeVisitor getVisitor() { 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(); - } - style = style.withUseTabCharacter(useTabs); + TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents) + .withUseTabCharacter(useTabs); return new NormalizeTabsOrSpacesVisitor<>(style).visit(tree, ctx); } return (J) tree; diff --git a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java index ff2c73a301..04e372debf 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java @@ -30,6 +30,7 @@ import org.openrewrite.java.tree.TypeUtils; import org.openrewrite.marker.Markers; import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker; +import org.openrewrite.style.Style; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -37,7 +38,6 @@ import java.util.ArrayList; import java.util.Base64; import java.util.List; -import java.util.Optional; import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.toList; @@ -145,8 +145,7 @@ private J.Literal toTextBlock(J.Binary binary, String content, List s } } - TabsAndIndentsStyle tabsAndIndentsStyle = Optional.ofNullable(getCursor().firstEnclosingOrThrow(SourceFile.class) - .getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents()); + TabsAndIndentsStyle tabsAndIndentsStyle = Style.from(TabsAndIndentsStyle.class, getCursor().firstEnclosingOrThrow(SourceFile.class), IntelliJ::tabsAndIndents); boolean useTab = tabsAndIndentsStyle.getUseTabCharacter(); int tabSize = tabsAndIndentsStyle.getTabSize();