Skip to content

Commit

Permalink
refactor: OpenRewrite best practices (#4432)
Browse files Browse the repository at this point in the history
* 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 <team@moderne.io>

* Work around nested import limitation to minimize diff

---------

Co-authored-by: Moderne <team@moderne.io>
  • Loading branch information
timtebeek and TeamModerne authored Aug 21, 2024
1 parent ed3525b commit 4404c55
Show file tree
Hide file tree
Showing 21 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.lang.NonNull;

import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package org.openrewrite.internal;

import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.config.ColumnDescriptor;
import org.openrewrite.config.DataTableDescriptor;
import org.openrewrite.config.RecipeIntrospectionException;
import org.openrewrite.internal.lang.NonNull;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.openrewrite.internal;

import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.lang.NonNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.With;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.internal.lang.NonNull;

import java.io.File;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.binary.Binary;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.marker.AlreadyReplaced;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.SearchResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.openrewrite.*;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.text.ChangeText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.gradle.trait;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -33,6 +34,7 @@ public void defaults(RecipeSpec spec) {
SearchResult.found(dep.getTree(), dep.getResolvedDependency().getGav().toString()))));
}

@DocumentExample
@Test
void literal() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.NonNull;

import java.io.IOException;
import java.io.InputStream;
Expand Down
13 changes: 6 additions & 7 deletions rewrite-hcl/src/main/java/org/openrewrite/hcl/HclTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.openrewrite.hcl.internal.template.Substitutions;
import org.openrewrite.hcl.style.SpacesStyle;
import org.openrewrite.hcl.tree.*;
import org.openrewrite.hcl.tree.Space.Location;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.template.SourceTemplate;
Expand Down Expand Up @@ -63,14 +62,14 @@ public <H extends Hcl> H apply(Cursor scope, HclCoordinates coordinates, Object.
onAfterVariableSubstitution.accept(substitutedTemplate);

Tree insertionPoint = coordinates.getTree();
Location loc = coordinates.getSpaceLocation();
Space.Location loc = coordinates.getSpaceLocation();

//noinspection unchecked
H h = (H) new HclVisitor<Integer>() {
@Override
public Hcl visitConfigFile(Hcl.ConfigFile configFile, Integer p) {
Hcl.ConfigFile c = (Hcl.ConfigFile) super.visitConfigFile(configFile, p);
if (loc.equals(Location.CONFIG_FILE_EOF)) {
if (loc.equals(Space.Location.CONFIG_FILE_EOF)) {
List<BodyContent> gen = substitutions.unsubstitute(templateParser.parseBodyContent(substitutedTemplate));

if (coordinates.getComparator() != null) {
Expand Down Expand Up @@ -101,7 +100,7 @@ public Hcl visitConfigFile(Hcl.ConfigFile configFile, Integer p) {
)
);
}
} else if (loc.equals(Location.CONFIG_FILE)) {
} else if (loc.equals(Space.Location.CONFIG_FILE)) {
List<BodyContent> gen = substitutions.unsubstitute(templateParser.parseBodyContent(substitutedTemplate));
c = c.withBody(
ListUtils.concatAll(
Expand All @@ -117,7 +116,7 @@ public Hcl visitConfigFile(Hcl.ConfigFile configFile, Integer p) {
@Override
public Hcl visitBlock(Hcl.Block block, Integer p) {
Hcl.Block b = (Hcl.Block) super.visitBlock(block, p);
if (loc.equals(Location.BLOCK_CLOSE)) {
if (loc.equals(Space.Location.BLOCK_CLOSE)) {
if (b.isScope(insertionPoint)) {
List<BodyContent> gen = substitutions.unsubstitute(templateParser.parseBodyContent(substitutedTemplate));

Expand Down Expand Up @@ -146,7 +145,7 @@ public Hcl visitBlock(Hcl.Block block, Integer p) {
.orElse(SpacesStyle.DEFAULT)).visit(b, p, getCursor().getParentOrThrow());
assert b != null;
}
} else if (loc.equals(Location.BLOCK)) {
} else if (loc.equals(Space.Location.BLOCK)) {
if (b.isScope(insertionPoint)) {
b = (Hcl.Block) autoFormat(templateParser.parseBodyContent(substitutedTemplate).get(0), p,
getCursor().getParentOrThrow());
Expand All @@ -159,7 +158,7 @@ public Hcl visitBlock(Hcl.Block block, Integer p) {
public Hcl visitExpression(Expression expression, Integer p) {
Hcl e = super.visitExpression(expression, p);

if (loc.equals(Location.EXPRESSION_PREFIX)) {
if (loc.equals(Space.Location.EXPRESSION_PREFIX)) {
if (e.isScope(insertionPoint)) {
e = templateParser.parseExpression(substitutedTemplate).withPrefix(expression.getPrefix());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.FileAttributes;
import org.openrewrite.hcl.internal.grammar.HCLParser;
import org.openrewrite.hcl.internal.grammar.HCLParserBaseVisitor;
import org.openrewrite.hcl.tree.*;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.marker.Markers;

import java.nio.charset.Charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import lombok.Value;
import org.apache.commons.lang3.StringUtils;
import org.intellij.lang.annotations.Language;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.java.search.UsesField;
import org.openrewrite.java.tree.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.java.tree.*;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package org.openrewrite.java;

import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.java.internal.DefaultJavaTypeSignatureBuilder;
import org.openrewrite.java.service.ImportService;
import org.openrewrite.java.tree.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.openrewrite.java.format;

import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.JavadocVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ public static JavaSourceSet build(String sourceSetName, Collection<Path> classpa
* If no GAV can be determined returns null.
*
*/
@Nullable
static String gavFromPath(Path path) {
static @Nullable String gavFromPath(Path path) {
String pathStr = PathUtils.separatorsToUnix(path.toString());
List<String> pathParts = Arrays.asList(pathStr.split("/"));
// Example maven path: ~/.m2/repository/org/openrewrite/rewrite-core/8.32.0/rewrite-core-8.32.0.jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.LoathingOfOthers;
import org.openrewrite.internal.SelfLoathing;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.java.JavaPrinter;
import org.openrewrite.java.JavaTypeVisitor;
import org.openrewrite.java.JavaVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openrewrite.xml.tree.Xml;

import java.util.Optional;

import static org.openrewrite.xml.AddOrUpdateChild.addOrUpdateChild;
import static org.openrewrite.xml.FilterTagChildrenVisitor.filterChildren;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.maven.tree.*;

import javax.xml.bind.annotation.XmlRootElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.openrewrite.maven.utilities.MavenWrapper.*;
import static org.openrewrite.maven.utilities.MavenWrapper.ASF_LICENSE_HEADER;
import static org.openrewrite.maven.utilities.MavenWrapper.WRAPPER_BATCH_LOCATION;
import static org.openrewrite.maven.utilities.MavenWrapper.WRAPPER_DOWNLOADER_LOCATION;
import static org.openrewrite.maven.utilities.MavenWrapper.WRAPPER_JAR_LOCATION;
import static org.openrewrite.maven.utilities.MavenWrapper.WRAPPER_SCRIPT_LOCATION;
import static org.openrewrite.properties.Assertions.properties;
import static org.openrewrite.test.SourceSpecs.*;
import static org.openrewrite.test.SourceSpecs.dir;
import static org.openrewrite.test.SourceSpecs.other;
import static org.openrewrite.test.SourceSpecs.text;

class UpdateMavenWrapperTest implements RewriteTest {
private final UnaryOperator<@Nullable String> notEmpty = actual -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import org.assertj.core.api.SoftAssertions;
import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.config.CompositeRecipe;
Expand All @@ -28,6 +27,7 @@
import org.openrewrite.internal.RecipeIntrospectionUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.WhitespaceValidationService;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.Markers;
import org.openrewrite.quark.Quark;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package org.openrewrite.xml.format;

import org.openrewrite.internal.lang.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.xml.XmlIsoVisitor;
import org.openrewrite.xml.style.TabsAndIndentsStyle;
import org.openrewrite.xml.tree.Xml;
Expand Down

0 comments on commit 4404c55

Please sign in to comment.