Skip to content

Commit fcddb6d

Browse files
ranuradhAnuRam123
andauthored
Recipe to call Modifier And ConstantBootstraps methods as static (#484)
Co-authored-by: anuram <[email protected]>
1 parent 6f1c77d commit fcddb6d

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/main/resources/META-INF/rewrite/java-version-17.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ recipeList:
3535
- org.openrewrite.java.migrate.RemoveMethodInvocation:
3636
methodPattern: java.lang.System traceMethodCalls(boolean)
3737
- org.openrewrite.java.migrate.RemovedToolProviderConstructor
38+
- org.openrewrite.java.migrate.RemovedModifierAndConstantBootstrapsConstructors
3839
- org.openrewrite.java.migrate.lang.UseTextBlocks
3940
- org.openrewrite.java.migrate.DeprecatedJavaxSecurityCert
4041
- org.openrewrite.java.migrate.DeprecatedLogRecordThreadID
@@ -240,3 +241,18 @@ recipeList:
240241
- org.openrewrite.java.ChangeMethodTargetToStatic:
241242
methodPattern: javax.tools.ToolProvider *()
242243
fullyQualifiedTargetTypeName: javax.tools.ToolProvider
244+
---
245+
type: specs.openrewrite.org/v1beta/recipe
246+
name: org.openrewrite.java.migrate.RemovedModifierAndConstantBootstrapsConstructors
247+
displayName: Change `java.lang.reflect.Modifier` and ` java.lang.invoke.ConstantBootstraps` method calls to static
248+
description: >
249+
The `java.lang.reflect.Modifier()` and `java.lang.invoke.ConstantBootstraps()` constructors have been removed in Java SE 15 because both classes only contain static methods.
250+
This recipe converts the usage of all methods in the two classes to be static.
251+
For more information on these changes, see https://docs.oracle.com/en/java/javase/15/migrate/index.html#GUID-233853B8-0782-429E-BEF7-7532EE610E63
252+
recipeList:
253+
- org.openrewrite.java.ChangeMethodTargetToStatic:
254+
methodPattern: java.lang.reflect.Modifier *(..)
255+
fullyQualifiedTargetTypeName: java.lang.reflect.Modifier
256+
- org.openrewrite.java.ChangeMethodTargetToStatic:
257+
methodPattern: java.lang.invoke.ConstantBootstraps *(..)
258+
fullyQualifiedTargetTypeName: java.lang.invoke.ConstantBootstraps
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
class RemovedModifierAndConstantBootstrapsConstructorsTest implements RewriteTest {
26+
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipeFromResource("/META-INF/rewrite/java-version-17.yml", "org.openrewrite.java.migrate.RemovedModifierAndConstantBootstrapsConstructors");
30+
}
31+
32+
@DocumentExample
33+
@Test
34+
void moveToStaticTest() {
35+
rewriteRun(
36+
//language=java
37+
java(
38+
"""
39+
import java.lang.invoke.ConstantBootstraps;
40+
import java.lang.reflect.Modifier;
41+
42+
class RemovedModifierAndConstantBootstrapsConstructorsApp {
43+
public void testModifier() throws Exception {
44+
Modifier modifier = new Modifier();
45+
modifier.classModifiers();
46+
modifier.fieldModifiers();
47+
modifier.isFinal(1);
48+
modifier.isStatic(1);
49+
Modifier.isPublic(0);
50+
}
51+
public void testConstantBootstraps() throws Exception {
52+
ConstantBootstraps constantBootstraps = new ConstantBootstraps();
53+
constantBootstraps.enumConstant(null,null,null);
54+
constantBootstraps.primitiveClass(null,null,null);
55+
ConstantBootstraps.nullConstant(null, null, null);
56+
}
57+
}
58+
""",
59+
"""
60+
import java.lang.invoke.ConstantBootstraps;
61+
import java.lang.reflect.Modifier;
62+
63+
class RemovedModifierAndConstantBootstrapsConstructorsApp {
64+
public void testModifier() throws Exception {
65+
Modifier modifier = new Modifier();
66+
Modifier.classModifiers();
67+
Modifier.fieldModifiers();
68+
Modifier.isFinal(1);
69+
Modifier.isStatic(1);
70+
Modifier.isPublic(0);
71+
}
72+
public void testConstantBootstraps() throws Exception {
73+
ConstantBootstraps constantBootstraps = new ConstantBootstraps();
74+
ConstantBootstraps.enumConstant(null,null,null);
75+
ConstantBootstraps.primitiveClass(null,null,null);
76+
ConstantBootstraps.nullConstant(null, null, null);
77+
}
78+
}
79+
"""
80+
)
81+
);
82+
}
83+
}

0 commit comments

Comments
 (0)