diff --git a/src/main/resources/META-INF/rewrite/java-version-17.yml b/src/main/resources/META-INF/rewrite/java-version-17.yml index 26da7d44c8..788b44b88a 100644 --- a/src/main/resources/META-INF/rewrite/java-version-17.yml +++ b/src/main/resources/META-INF/rewrite/java-version-17.yml @@ -1,5 +1,5 @@ # -# Copyright 2022 the original author or authors. +# Copyright 2024 the original author or authors. #
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -49,7 +49,10 @@ recipeList: groupId: com.sonatype.clm artifactId: clm-maven-plugin newVersion: 2.47.6-01 - + - org.openrewrite.java.migrate.RemovedZipFinalizeMethods + - org.openrewrite.java.migrate.RemovedSSLSessionGetPeerCertificateChainMethodImpl + - org.openrewrite.java.migrate.SunNetSslPackageUnavailable + - org.openrewrite.java.migrate.RemovedRMIConnectorServerCredentialTypesConstant --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.JavaVersion17 @@ -120,3 +123,64 @@ recipeList: - org.openrewrite.java.ChangeMethodAccessLevel: methodPattern: "*..* premain(java.lang.String, java.lang.instrument.Instrumentation)" newAccessLevel: public +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.RemovedZipFinalizeMethods +displayName: Replace `finalize` method in `java.util.zip.Zipfile`, `java.util.zip.Inflater` and `java.util.zip.Deflater` +description: > + The `finalize` method in `java.util.zip.ZipFile` is replaced with the `close` method and is replaced by the `end` method in + `java.util.zip.Inflater` and `java.util.zip.Deflater` as it is no longer available in Java SE 12 and later. +tags: + - java17 +recipeList: + - org.openrewrite.java.ChangeMethodName: + methodPattern: "java.util.zip.Inflater finalize()" + newMethodName: end + ignoreDefinition: true + - org.openrewrite.java.ChangeMethodName: + methodPattern: "java.util.zip.Deflater finalize()" + newMethodName: end + ignoreDefinition: true + - org.openrewrite.java.ChangeMethodName: + methodPattern: "java.util.zip.ZipFile finalize()" + newMethodName: close + ignoreDefinition: true +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.RemovedSSLSessionGetPeerCertificateChainMethodImpl +displayName: Replace `SSLSession.getPeerCertificateChain()` method +description: > + The `javax.net.ssl.SSLSession.getPeerCertificateChain()` method implementation was removed from the SunJSSE provider and HTTP client implementation in Java SE 15. + The default implementation will now throw an `UnsupportedOperationException`. + Applications using this method should be updated to use the `javax.net.ssl.SSLSession.getPeerCertificates()` method instead. +tags: + - java17 +recipeList: + - org.openrewrite.java.ChangeMethodName: + methodPattern: "javax.net.ssl.SSLSession getPeerCertificateChain()" + newMethodName: getPeerCertificates + ignoreDefinition: true +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.SunNetSslPackageUnavailable +displayName: Replace `com.sun.net.ssl` package +description: > + The internal API `com.sun.net.ssl` is removed. The package was intended for internal use only and replacement APIs can be found in the `javax.net.ssl` package. +tags: + - java17 +recipeList: + - org.openrewrite.java.ChangePackage: + oldPackageName: com.sun.net.ssl + newPackageName: javax.net.ssl +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.RemovedRMIConnectorServerCredentialTypesConstant +displayName: Replace `RMIConnectorServer.CREDENTIAL_TYPES` constant +description: > + This recipe replaces the `RMIConnectorServer.CREDENTIAL_TYPES` constant with the `RMIConnectorServer.CREDENTIALS_FILTER_PATTERN` constant. +tags: + - java17 +recipeList: + - org.openrewrite.java.ReplaceConstantWithAnotherConstant: + existingFullyQualifiedConstantName: javax.management.remote.rmi.RMIConnectorServer.CREDENTIAL_TYPES + fullyQualifiedConstantName: javax.management.remote.rmi.RMIConnectorServer.CREDENTIALS_FILTER_PATTERN diff --git a/src/test/java/org/openrewrite/java/migrate/SunNetSslPackageUnavailableTest.java b/src/test/java/org/openrewrite/java/migrate/SunNetSslPackageUnavailableTest.java new file mode 100644 index 0000000000..488c436871 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/SunNetSslPackageUnavailableTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2024 the original author or authors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.migrate; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.config.Environment; +import org.openrewrite.java.JavaParser; +import org.openrewrite.java.migrate.jakarta.RemoveBeanIsNullable; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.*; + +class SunNetSslPackageUnavailableTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.parser(JavaParser.fromJavaVersion() + .classpathFromResources(new InMemoryExecutionContext(), + "com.sun.net.ssl")) + .recipeFromResource("/META-INF/rewrite/java-version-17.yml", "org.openrewrite.java.migrate.SunNetSslPackageUnavailable"); + } + + @Test + void sunNetSslPackageUnavailableTest(){ + rewriteRun( + //language=java + java( + """ + import com.sun.net.ssl.HttpsURLConnection; + + class TestSunNetSsl { + void useThePackages() { + HttpsURLConnection con; + } + } + """, + """ + import javax.net.ssl.HttpsURLConnection; + + class TestSunNetSsl { + void useThePackages() { + HttpsURLConnection con; + } + } + """ + ) + ); + } + +} diff --git a/src/test/java/org/openrewrite/java/migrate/UpgradeToJava17Test.java b/src/test/java/org/openrewrite/java/migrate/UpgradeToJava17Test.java index 91e69804df..00c7dc0da1 100644 --- a/src/test/java/org/openrewrite/java/migrate/UpgradeToJava17Test.java +++ b/src/test/java/org/openrewrite/java/migrate/UpgradeToJava17Test.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.config.Environment; import org.openrewrite.java.marker.JavaVersion; import org.openrewrite.test.RecipeSpec; @@ -527,4 +528,42 @@ void lombokBumpedGoingTo17() { ) ); } + + @Test + void removedSSLSessionGetPeerCertificateChainMethodImplTest(){ + rewriteRun( + //language=java + java( + """ + import java.security.cert.Certificate; + import javax.net.ssl.SSLContext; + import javax.net.ssl.SSLEngine; + import javax.net.ssl.SSLSession; + class RemovedSSLSessionGetPeerCertificateChainMethodImplApp { + void test() throws Exception { + SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine(); + SSLSession session = sslEngine.getHandshakeSession(); + session.getPeerCertificateChain(); //This should trigger + Certificate[] certs = session.getPeerCertificates(); //This should not trigger + } + } + """, + """ + import java.security.cert.Certificate; + import javax.net.ssl.SSLContext; + import javax.net.ssl.SSLEngine; + import javax.net.ssl.SSLSession; + class RemovedSSLSessionGetPeerCertificateChainMethodImplApp { + void test() throws Exception { + SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine(); + SSLSession session = sslEngine.getHandshakeSession(); + session.getPeerCertificates(); //This should trigger + Certificate[] certs = session.getPeerCertificates(); //This should not trigger + } + } + """ + ) + ); + } + } diff --git a/src/test/resources/META-INF/rewrite/classpath/com.sun.net.ssl.jar b/src/test/resources/META-INF/rewrite/classpath/com.sun.net.ssl.jar new file mode 100644 index 0000000000..9b23c32c23 Binary files /dev/null and b/src/test/resources/META-INF/rewrite/classpath/com.sun.net.ssl.jar differ