From 215f3df3900b96e6b15e62b2f7efb3df9c46446b Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 9 Sep 2024 08:38:19 +0000 Subject: [PATCH] refactor: OpenRewrite best practices Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.OpenRewriteBestPractices?organizationId=T3BlblJld3JpdGU%3D Co-authored-by: Moderne --- .../openrewrite/csharp/CSharpIsoVisitor.java | 10 +--- .../org/openrewrite/csharp/CSharpPrinter.java | 8 +-- .../org/openrewrite/csharp/CSharpVisitor.java | 6 +- .../csharp/marker/package-info.java | 4 +- .../org/openrewrite/csharp/package-info.java | 4 +- .../java/org/openrewrite/csharp/tree/Cs.java | 58 +++++++++---------- .../openrewrite/csharp/tree/package-info.java | 4 +- 7 files changed, 41 insertions(+), 53 deletions(-) diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java index 34369914..eee58d2f 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java @@ -15,16 +15,8 @@ */ package org.openrewrite.csharp; -import org.jspecify.annotations.Nullable; -import org.openrewrite.*; -import org.openrewrite.internal.ListUtils; -import org.openrewrite.marker.Markers; -import org.openrewrite.tree.*; -import org.openrewrite.java.JavaVisitor; -import org.openrewrite.java.tree.*; import org.openrewrite.csharp.tree.*; - -import java.util.List; +import org.openrewrite.java.tree.*; public class CSharpIsoVisitor

extends CSharpVisitor

{ diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java index d9115032..7041c6a2 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java @@ -15,13 +15,13 @@ */ package org.openrewrite.csharp; +import org.jspecify.annotations.Nullable; import org.openrewrite.Cursor; import org.openrewrite.PrintOutputCapture; import org.openrewrite.Tree; import org.openrewrite.csharp.marker.OmitBraces; import org.openrewrite.csharp.marker.SingleExpressionBlock; import org.openrewrite.csharp.tree.*; -import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaPrinter; import org.openrewrite.java.marker.CompactConstructor; import org.openrewrite.java.marker.Semicolon; @@ -714,7 +714,7 @@ private void beforeSyntax(Cs cs, CsSpace.Location loc, PrintOutputCapture

p) beforeSyntax(cs.getPrefix(), cs.getMarkers(), loc, p); } - private void beforeSyntax(Space prefix, Markers markers, @Nullable CsSpace.Location loc, PrintOutputCapture

p) { + private void beforeSyntax(Space prefix, Markers markers, CsSpace.@Nullable Location loc, PrintOutputCapture

p) { for (Marker marker : markers.getMarkers()) { p.append(p.getMarkerPrinter().beforePrefix(marker, new Cursor(getCursor(), marker), JAVA_MARKER_WRAPPER)); } @@ -727,7 +727,7 @@ private void beforeSyntax(Space prefix, Markers markers, @Nullable CsSpace.Locat } } - private void beforeSyntax(Space prefix, Markers markers, @Nullable Space.Location loc, PrintOutputCapture

p) { + private void beforeSyntax(Space prefix, Markers markers, Space.@Nullable Location loc, PrintOutputCapture

p) { for (Marker marker : markers.getMarkers()) { p.append(p.getMarkerPrinter().beforePrefix(marker, new Cursor(getCursor(), marker), JAVA_MARKER_WRAPPER)); } @@ -749,4 +749,4 @@ private void afterSyntax(Markers markers, PrintOutputCapture

p) { p.append(p.getMarkerPrinter().afterSyntax(marker, new Cursor(getCursor(), marker), JAVA_MARKER_WRAPPER)); } } -} \ No newline at end of file +} diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java index 64bf3569..e1dbc666 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpVisitor.java @@ -17,12 +17,12 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.*; +import org.openrewrite.csharp.tree.*; import org.openrewrite.internal.ListUtils; -import org.openrewrite.marker.Markers; -import org.openrewrite.tree.*; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.tree.*; -import org.openrewrite.csharp.tree.*; +import org.openrewrite.marker.Markers; +import org.openrewrite.tree.*; import java.util.List; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/marker/package-info.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/marker/package-info.java index 04db8340..14d0ea62 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/marker/package-info.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/marker/package-info.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -@NonNullApi +@NullMarked @NonNullFields package org.openrewrite.csharp.marker; -import org.openrewrite.internal.lang.NonNullApi; +import org.jspecify.annotations.NullMarked; import org.openrewrite.internal.lang.NonNullFields; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/package-info.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/package-info.java index 008fde1a..0f78e559 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/package-info.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/package-info.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -@NonNullApi +@NullMarked @NonNullFields package org.openrewrite.csharp; -import org.openrewrite.internal.lang.NonNullApi; +import org.jspecify.annotations.NullMarked; import org.openrewrite.internal.lang.NonNullFields; diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java index 88740fc7..c30f6109 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/Cs.java @@ -18,10 +18,10 @@ import lombok.*; import lombok.experimental.FieldDefaults; import lombok.experimental.NonFinal; +import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.csharp.CSharpPrinter; import org.openrewrite.csharp.CSharpVisitor; -import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaPrinter; import org.openrewrite.java.internal.TypesInUse; import org.openrewrite.java.tree.*; @@ -54,8 +54,7 @@ default

boolean isAcceptable(TreeVisitor v, P p) { return v.isAdaptableTo(CSharpVisitor.class); } - @Nullable - default

J acceptCSharp(CSharpVisitor

v, P p) { + default

@Nullable J acceptCSharp(CSharpVisitor

v, P p) { return v.defaultValue(this, p); } @@ -121,11 +120,12 @@ public SourceFile withCharset(Charset charset) { return withCharsetName(charset.name()); } - @Nullable - public Package getPackageDeclaration() { + @Override + public @Nullable Package getPackageDeclaration() { return null; } + @Override public Cs.CompilationUnit withPackageDeclaration(Package packageDeclaration) { return this; } @@ -164,15 +164,18 @@ public Cs.CompilationUnit withMembers(List members) { return getPadding().withMembers(JRightPadded.withElements(this.members, members)); } + @Override @Transient public List getImports() { return Collections.emptyList(); } + @Override public Cs.CompilationUnit withImports(List imports) { return this; } + @Override @Transient public List getClasses() { return Collections.emptyList(); @@ -213,6 +216,7 @@ public TypesInUse getTypesInUse() { return cache; } + @Override public Padding getPadding() { Padding p; if (this.padding == null) { @@ -378,8 +382,7 @@ public Padding getPadding() { public static class Padding { private final ArrayRankSpecifier t; - @Nullable - public JContainer getSizes() { + public @Nullable JContainer getSizes() { return t.sizes; } @@ -514,12 +517,12 @@ final class AttributeList implements Cs { @Nullable JRightPadded target; - @Nullable - public J.Identifier getTarget() { + + public J.@Nullable Identifier getTarget() { return target == null ? null : target.getElement(); } - public AttributeList withTarget(@Nullable J.Identifier target) { + public AttributeList withTarget(J.@Nullable Identifier target) { return getPadding().withTarget(JRightPadded.withElement(this.target, target)); } @@ -1259,8 +1262,7 @@ public Interpolation withExpression(Expression expression) { @Nullable JRightPadded alignment; - @Nullable - public Expression getAlignment() { + public @Nullable Expression getAlignment() { return alignment != null ? alignment.getElement() : null; } @@ -1271,8 +1273,7 @@ public Interpolation withAlignment(@Nullable Expression alignment) { @Nullable JRightPadded format; - @Nullable - public Expression getFormat() { + public @Nullable Expression getFormat() { return format != null ? format.getElement() : null; } @@ -1328,8 +1329,7 @@ public Interpolation withExpression(JRightPadded expression) { return t.expression == expression ? t : new Interpolation(t.id, t.prefix, t.markers, expression, t.alignment, t.format); } - @Nullable - public JRightPadded getAlignment() { + public @Nullable JRightPadded getAlignment() { return t.alignment; } @@ -1337,8 +1337,7 @@ public Interpolation withAlignment(@Nullable JRightPadded alignment) return t.alignment == alignment ? t : new Interpolation(t.id, t.prefix, t.markers, t.expression, alignment, t.format); } - @Nullable - public JRightPadded getFormat() { + public @Nullable JRightPadded getFormat() { return t.format; } @@ -1525,12 +1524,12 @@ public UsingDirective withUnsafe(boolean unsafe) { @Nullable JRightPadded alias; - @Nullable - public J.Identifier getAlias() { + + public J.@Nullable Identifier getAlias() { return alias != null ? alias.getElement() : null; } - public UsingDirective withAlias(@Nullable J.Identifier alias) { + public UsingDirective withAlias(J.@Nullable Identifier alias) { return getPadding().withAlias(JRightPadded.withElement(this.alias, alias)); } @@ -1591,8 +1590,7 @@ public UsingDirective withUnsafe(JLeftPadded unsafe) { return t.unsafe == unsafe ? t : new UsingDirective(t.id, t.prefix, t.markers, t.global, t.statik, unsafe, t.alias, t.namespaceOrType); } - @Nullable - public JRightPadded getAlias() { + public @Nullable JRightPadded getAlias() { return t.alias; } @@ -1657,16 +1655,17 @@ public CoordinateBuilder.Statement getCoordinates() { return new CoordinateBuilder.Statement(this); } + @Override public JavaType getType() { return typeExpression.getType(); } + @Override public PropertyDeclaration withType(@Nullable JavaType type) { return getPadding().withType(this.typeExpression.withType(type)); } - @Nullable - public NameTree getInterfaceSpecifier() { + public @Nullable NameTree getInterfaceSpecifier() { return interfaceSpecifier!= null ? interfaceSpecifier.getElement() : null; } @@ -1674,8 +1673,7 @@ public PropertyDeclaration withInterfaceSpecifier(@Nullable NameTree interfaceSp return getPadding().withInterfaceSpecifier(JRightPadded.withElement(this.interfaceSpecifier, interfaceSpecifier)); } - @Nullable - public Expression getInitializer() { + public @Nullable Expression getInitializer() { return initializer != null ? initializer.getElement() : null; } @@ -1711,8 +1709,7 @@ public TypeTree getType() { return pd.typeExpression; } - @Nullable - public JRightPadded getInterfaceSpecifier() { + public @Nullable JRightPadded getInterfaceSpecifier() { return pd.interfaceSpecifier; } @@ -1742,8 +1739,7 @@ public PropertyDeclaration withType(TypeTree type) { pd.initializer); } - @Nullable - public JLeftPadded getInitializer() { + public @Nullable JLeftPadded getInitializer() { return pd.initializer; } diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/package-info.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/package-info.java index 0d5ca5d1..97e557c1 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/package-info.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/tree/package-info.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -@NonNullApi +@NullMarked @NonNullFields package org.openrewrite.csharp.tree; -import org.openrewrite.internal.lang.NonNullApi; +import org.jspecify.annotations.NullMarked; import org.openrewrite.internal.lang.NonNullFields;