From d49e38ed41300f6234cabc9794fce3959912f894 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 28 Jun 2024 12:16:46 +0200 Subject: [PATCH] Correct swapped class names and implementation For #546 --- ...tringLiteralsWithHttpHeadersConstants.java | 117 ++++++++++++------ ...eStringLiteralsWithMediaTypeConstants.java | 117 ++++++------------ 2 files changed, 117 insertions(+), 117 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithHttpHeadersConstants.java b/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithHttpHeadersConstants.java index 2c6ec2770..7900f1b7d 100644 --- a/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithHttpHeadersConstants.java +++ b/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithHttpHeadersConstants.java @@ -20,55 +20,85 @@ import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.ReplaceStringLiteralWithConstant; import org.openrewrite.java.tree.J; +import org.openrewrite.maven.search.DependencyInsight; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; -import static org.openrewrite.java.spring.http.ReplaceStringLiteralsWithMediaTypeConstants.declaresSpringWebDependency; - public class ReplaceStringLiteralsWithHttpHeadersConstants extends ScanningRecipe { - private static final List MEDIA_TYPES = Arrays.asList( - "ALL_VALUE", - "APPLICATION_ATOM_XML_VALUE", - "APPLICATION_CBOR_VALUE", - "APPLICATION_FORM_URLENCODED_VALUE", - "APPLICATION_GRAPHQL_VALUE", - "APPLICATION_GRAPHQL_RESPONSE_VALUE", - "APPLICATION_JSON_VALUE", - "APPLICATION_JSON_UTF8_VALUE", - "APPLICATION_OCTET_STREAM_VALUE", - "APPLICATION_PDF_VALUE", - "APPLICATION_PROBLEM_JSON_VALUE", - "APPLICATION_PROBLEM_JSON_UTF8_VALUE", - "APPLICATION_PROBLEM_XML_VALUE", - "APPLICATION_PROTOBUF_VALUE", - "APPLICATION_RSS_XML_VALUE", - "APPLICATION_NDJSON_VALUE", - "APPLICATION_STREAM_JSON_VALUE", - "APPLICATION_XHTML_XML_VALUE", - "APPLICATION_XML_VALUE", - "IMAGE_GIF_VALUE", - "IMAGE_JPEG_VALUE", - "IMAGE_PNG_VALUE", - "MULTIPART_FORM_DATA_VALUE", - "MULTIPART_MIXED_VALUE", - "MULTIPART_RELATED_VALUE", - "TEXT_EVENT_STREAM_VALUE", - "TEXT_HTML_VALUE", - "TEXT_MARKDOWN_VALUE", - "TEXT_PLAIN_VALUE", - "TEXT_XML_VALUE"); + private static final List HEADERS = Arrays.asList( + "ACCEPT", + "ACCEPT_CHARSET", + "ACCEPT_ENCODING", + "ACCEPT_LANGUAGE", + "ACCEPT_PATCH", + "ACCEPT_RANGES", + "ACCESS_CONTROL_ALLOW_CREDENTIALS", + "ACCESS_CONTROL_ALLOW_HEADERS", + "ACCESS_CONTROL_ALLOW_METHODS", + "ACCESS_CONTROL_ALLOW_ORIGIN", + "ACCESS_CONTROL_EXPOSE_HEADERS", + "ACCESS_CONTROL_MAX_AGE", + "ACCESS_CONTROL_REQUEST_HEADERS", + "ACCESS_CONTROL_REQUEST_METHOD", + "AGE", + "ALLOW", + "AUTHORIZATION", + "CACHE_CONTROL", + "CONNECTION", + "CONTENT_ENCODING", + "CONTENT_DISPOSITION", + "CONTENT_LANGUAGE", + "CONTENT_LENGTH", + "CONTENT_LOCATION", + "CONTENT_RANGE", + "CONTENT_TYPE", + "COOKIE", + "DATE", + "ETAG", + "EXPECT", + "EXPIRES", + "FROM", + "HOST", + "IF_MATCH", + "IF_MODIFIED_SINCE", + "IF_NONE_MATCH", + "IF_RANGE", + "IF_UNMODIFIED_SINCE", + "LAST_MODIFIED", + "LINK", + "LOCATION", + "MAX_FORWARDS", + "ORIGIN", + "PRAGMA", + "PROXY_AUTHENTICATE", + "PROXY_AUTHORIZATION", + "RANGE", + "REFERER", + "RETRY_AFTER", + "SERVER", + "SET_COOKIE", + "SET_COOKIE2", + "TE", + "TRAILER", + "TRANSFER_ENCODING", + "UPGRADE", + "USER_AGENT", + "VARY", + "VIA", + "WARNING", + "WWW_AUTHENTICATE"); @Override public String getDisplayName() { - return "Replace String literals with `MediaType` constants"; + return "Replace String literals with `HttpHeaders` constants"; } @Override public String getDescription() { - return "Replace String literals with `org.springframework.http.MediaType` constants."; + return "Replace String literals with `org.springframework.http.HttpHeaders` constants."; } @Override @@ -95,15 +125,20 @@ public TreeVisitor getVisitor(AtomicBoolean acc) { @Override public J preVisit(J tree, ExecutionContext ctx) { stopAfterPreVisit(); - for (String mediaType : MEDIA_TYPES) { - try { - doAfterVisit(new ReplaceStringLiteralWithConstant("org.springframework.http.MediaType." + mediaType).getVisitor()); - } catch (IllegalArgumentException ignore) { - // this is typically a NoSuchFieldException when an older version of Spring is used - } + for (String header : HEADERS) { + doAfterVisit(new ReplaceStringLiteralWithConstant("org.springframework.http.HttpHeaders." + header).getVisitor()); } return tree; } }); } + + static boolean declaresSpringWebDependency(SourceFile sourceFile, ExecutionContext ctx) { + TreeVisitor visitor = new DependencyInsight("org.springframework", "spring-web", "compile", null, null).getVisitor(); + if (visitor.isAcceptable(sourceFile, ctx) && visitor.visit(sourceFile, ctx) != sourceFile) { + return true; + } + visitor = new org.openrewrite.gradle.search.DependencyInsight("org.springframework", "spring-web", "compileClasspath", null).getVisitor(); + return visitor.isAcceptable(sourceFile, ctx) && visitor.visit(sourceFile, ctx) != sourceFile; + } } diff --git a/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithMediaTypeConstants.java b/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithMediaTypeConstants.java index 2a98f08ee..93c71ea8c 100644 --- a/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithMediaTypeConstants.java +++ b/src/main/java/org/openrewrite/java/spring/http/ReplaceStringLiteralsWithMediaTypeConstants.java @@ -20,85 +20,55 @@ import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.ReplaceStringLiteralWithConstant; import org.openrewrite.java.tree.J; -import org.openrewrite.maven.search.DependencyInsight; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import static org.openrewrite.java.spring.http.ReplaceStringLiteralsWithHttpHeadersConstants.declaresSpringWebDependency; + public class ReplaceStringLiteralsWithMediaTypeConstants extends ScanningRecipe { - private static final List HEADERS = Arrays.asList( - "ACCEPT", - "ACCEPT_CHARSET", - "ACCEPT_ENCODING", - "ACCEPT_LANGUAGE", - "ACCEPT_PATCH", - "ACCEPT_RANGES", - "ACCESS_CONTROL_ALLOW_CREDENTIALS", - "ACCESS_CONTROL_ALLOW_HEADERS", - "ACCESS_CONTROL_ALLOW_METHODS", - "ACCESS_CONTROL_ALLOW_ORIGIN", - "ACCESS_CONTROL_EXPOSE_HEADERS", - "ACCESS_CONTROL_MAX_AGE", - "ACCESS_CONTROL_REQUEST_HEADERS", - "ACCESS_CONTROL_REQUEST_METHOD", - "AGE", - "ALLOW", - "AUTHORIZATION", - "CACHE_CONTROL", - "CONNECTION", - "CONTENT_ENCODING", - "CONTENT_DISPOSITION", - "CONTENT_LANGUAGE", - "CONTENT_LENGTH", - "CONTENT_LOCATION", - "CONTENT_RANGE", - "CONTENT_TYPE", - "COOKIE", - "DATE", - "ETAG", - "EXPECT", - "EXPIRES", - "FROM", - "HOST", - "IF_MATCH", - "IF_MODIFIED_SINCE", - "IF_NONE_MATCH", - "IF_RANGE", - "IF_UNMODIFIED_SINCE", - "LAST_MODIFIED", - "LINK", - "LOCATION", - "MAX_FORWARDS", - "ORIGIN", - "PRAGMA", - "PROXY_AUTHENTICATE", - "PROXY_AUTHORIZATION", - "RANGE", - "REFERER", - "RETRY_AFTER", - "SERVER", - "SET_COOKIE", - "SET_COOKIE2", - "TE", - "TRAILER", - "TRANSFER_ENCODING", - "UPGRADE", - "USER_AGENT", - "VARY", - "VIA", - "WARNING", - "WWW_AUTHENTICATE"); + private static final List MEDIA_TYPES = Arrays.asList( + "ALL_VALUE", + "APPLICATION_ATOM_XML_VALUE", + "APPLICATION_CBOR_VALUE", + "APPLICATION_FORM_URLENCODED_VALUE", + "APPLICATION_GRAPHQL_VALUE", + "APPLICATION_GRAPHQL_RESPONSE_VALUE", + "APPLICATION_JSON_VALUE", + "APPLICATION_JSON_UTF8_VALUE", + "APPLICATION_OCTET_STREAM_VALUE", + "APPLICATION_PDF_VALUE", + "APPLICATION_PROBLEM_JSON_VALUE", + "APPLICATION_PROBLEM_JSON_UTF8_VALUE", + "APPLICATION_PROBLEM_XML_VALUE", + "APPLICATION_PROTOBUF_VALUE", + "APPLICATION_RSS_XML_VALUE", + "APPLICATION_NDJSON_VALUE", + "APPLICATION_STREAM_JSON_VALUE", + "APPLICATION_XHTML_XML_VALUE", + "APPLICATION_XML_VALUE", + "IMAGE_GIF_VALUE", + "IMAGE_JPEG_VALUE", + "IMAGE_PNG_VALUE", + "MULTIPART_FORM_DATA_VALUE", + "MULTIPART_MIXED_VALUE", + "MULTIPART_RELATED_VALUE", + "TEXT_EVENT_STREAM_VALUE", + "TEXT_HTML_VALUE", + "TEXT_MARKDOWN_VALUE", + "TEXT_PLAIN_VALUE", + "TEXT_XML_VALUE"); @Override public String getDisplayName() { - return "Replace String literals with `HttpHeaders` constants"; + return "Replace String literals with `MediaType` constants"; } @Override public String getDescription() { - return "Replace String literals with `org.springframework.http.HttpHeaders` constants."; + return "Replace String literals with `org.springframework.http.MediaType` constants."; } @Override @@ -125,20 +95,15 @@ public TreeVisitor getVisitor(AtomicBoolean acc) { @Override public J preVisit(J tree, ExecutionContext ctx) { stopAfterPreVisit(); - for (String header : HEADERS) { - doAfterVisit(new ReplaceStringLiteralWithConstant("org.springframework.http.HttpHeaders." + header).getVisitor()); + for (String mediaType : MEDIA_TYPES) { + try { + doAfterVisit(new ReplaceStringLiteralWithConstant("org.springframework.http.MediaType." + mediaType).getVisitor()); + } catch (IllegalArgumentException ignore) { + // this is typically a NoSuchFieldException when an older version of Spring is used + } } return tree; } }); } - - static boolean declaresSpringWebDependency(SourceFile sourceFile, ExecutionContext ctx) { - TreeVisitor visitor = new DependencyInsight("org.springframework", "spring-web", "compile", null, null).getVisitor(); - if (visitor.isAcceptable(sourceFile, ctx) && visitor.visit(sourceFile, ctx) != sourceFile) { - return true; - } - visitor = new org.openrewrite.gradle.search.DependencyInsight("org.springframework", "spring-web", "compileClasspath", null).getVisitor(); - return visitor.isAcceptable(sourceFile, ctx) && visitor.visit(sourceFile, ctx) != sourceFile; - } }