Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish ChangeDefaultKeyStore to only apply to Java 11 #670

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.java.migrate;


import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
Expand All @@ -31,7 +30,6 @@

import static org.openrewrite.Tree.randomId;


public class ChangeDefaultKeyStore extends Recipe {
private static final MethodMatcher KEYSTORE_METHOD_REF = new MethodMatcher("java.security.KeyStore getDefaultType()", true);

Expand All @@ -42,15 +40,19 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "In Java 11 the default keystore was updated from JKS to PKCS12." + "As a result, applications relying on KeyStore.getDefaultType() may encounter issues after migrating, unless their JKS keystore has been converted to PKCS12." + "This recipe returns default key store of `jks` when `KeyStore.getDefaultType()` method is called to use the pre Java 11 default keystore.";
return "In Java 11 the default keystore was updated from JKS to PKCS12. " +
"As a result, applications relying on KeyStore.getDefaultType() may encounter issues after migrating," +
" unless their JKS keystore has been converted to PKCS12. " +
"This recipe returns default key store of `jks` when `KeyStore.getDefaultType()` method is called to" +
" use the pre Java 11 default keystore.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Preconditions.and(
new UsesJavaVersion<>(11),
new UsesMethod<>("java.security.KeyStore getDefaultType()")),
new UsesJavaVersion<>(11, 11),
new UsesMethod<>(KEYSTORE_METHOD_REF)),
new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java.migrate;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -24,6 +25,29 @@
import static org.openrewrite.java.Assertions.javaVersion;

class ChangeDefaultKeyStoreTest implements RewriteTest {

@Language("java")
private static final String BEFORE = """
import java.io.FileInputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyStore;

class Foo {
void bar() {
try{
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
char[] password = "your_keystore_password".toCharArray();
FileInputStream keystoreFile = new FileInputStream("path_to_your_keystore_file.jks");
keystore.load(keystoreFile, password);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
""";

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ChangeDefaultKeyStore())
Expand All @@ -32,30 +56,11 @@ public void defaults(RecipeSpec spec) {

@DocumentExample
@Test
void keyStore() {
void keyStoreDefaultTypeChangedToExplicitType() {
rewriteRun(
//language=java
java(
"""
import java.io.FileInputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyStore;

class Foo {
void bar() {
try{
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
char[] password = "your_keystore_password".toCharArray();
FileInputStream keystoreFile = new FileInputStream("path_to_your_keystore_file.jks");
keystore.load(keystoreFile, password);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
""",
BEFORE,
"""
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -81,7 +86,7 @@ void bar() {
}

@Test
void keepString() {
void keepExplicitType() {
rewriteRun(
//language=java
java(
Expand Down Expand Up @@ -113,27 +118,18 @@ void keepStringForJava8() {
rewriteRun(
//language=java
java(
"""
import java.io.FileInputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyStore;

class Foo {
void bar() {
try{
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
char[] password = "your_keystore_password".toCharArray();
FileInputStream keystoreFile = new FileInputStream("path_to_your_keystore_file.jks");
keystore.load(keystoreFile, password);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
""",
BEFORE,
spec -> spec.markers(javaVersion(8)))
);
}

@Test
void keepStringForJava17() {
rewriteRun(
//language=java
java(
BEFORE,
spec -> spec.markers(javaVersion(17)))
);
}
}