Skip to content

Commit

Permalink
Polish ChangeDefaultKeyStore to only apply to Java 11
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Feb 4, 2025
1 parent 23ddb3b commit 7d07bc4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
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)))
);
}
}

0 comments on commit 7d07bc4

Please sign in to comment.