-
Notifications
You must be signed in to change notification settings - Fork 101
Java 11 recipe for default keystore change #667
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
Merged
MBoegers
merged 7 commits into
openrewrite:main
from
ranuradh:recipe_default_keystore_change_1
Feb 4, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d42b518
first commit
AnuRam123 ebfc400
add .yml and updated description
AnuRam123 6bb7568
small update
AnuRam123 609e3c5
post review comments
AnuRam123 91889d3
small updates
AnuRam123 ec5bffb
add no change tests and additional precondition for `java.security.Ke…
MBoegers 9739dd5
Merge branch 'main' into recipe_default_keystore_change_1
MBoegers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/org/openrewrite/java/migrate/ChangeDefaultKeyStore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* 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.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesJavaVersion; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Space; | ||
import org.openrewrite.marker.Markers; | ||
|
||
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); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Return String `jks` when `KeyStore.getDefaultType()` is called"; | ||
} | ||
|
||
@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."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.and( | ||
new UsesJavaVersion<>(11), | ||
new UsesMethod<>("java.security.KeyStore getDefaultType()")), | ||
new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
if (KEYSTORE_METHOD_REF.matches(method)) { | ||
return new J.Literal(randomId(), Space.EMPTY, Markers.EMPTY, "\"jks\"", "\"jks\"", null, JavaType.Primitive.String); | ||
} | ||
return super.visitMethodInvocation(method, ctx); | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/test/java/org/openrewrite/java/migrate/ChangeDefaultKeyStoreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* 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.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.java.Assertions.javaVersion; | ||
|
||
class ChangeDefaultKeyStoreTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ChangeDefaultKeyStore()) | ||
.allSources(src -> src.markers(javaVersion(11))); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void keyStore() { | ||
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(); | ||
} | ||
} | ||
} | ||
""", | ||
""" | ||
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("jks"); | ||
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(); | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void keepString() { | ||
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("jks"); | ||
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(); | ||
} | ||
} | ||
} | ||
""") | ||
); | ||
} | ||
|
||
@Test | ||
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(); | ||
} | ||
} | ||
} | ||
""", | ||
spec -> spec.markers(javaVersion(8))) | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.