Skip to content

Commit 4171235

Browse files
authored
Merge branch 'main' into patch-1
2 parents 3e8769a + 59d56a6 commit 4171235

File tree

52 files changed

+1447
-748
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1447
-748
lines changed

gradle/wrapper/gradle-wrapper.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionSha256Sum=31c55713e40233a8303827ceb42ca48a47267a0ad4bab9177123121e71524c26
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
3+
distributionSha256Sum=f397b287023acdba1e9f6fc5ea72d22dd63669d59ed4a289a29b1a76eee151c6
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
55
networkTimeout=10000
66
validateDistributionUrl=true
77
zipStoreBase=GRADLE_USER_HOME
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.openrewrite.ExecutionContext;
19+
import org.openrewrite.Preconditions;
20+
import org.openrewrite.Recipe;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.internal.ListUtils;
23+
import org.openrewrite.java.ChangeType;
24+
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.search.FindMethods;
26+
import org.openrewrite.java.search.UsesMethod;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.TypeUtils;
29+
30+
public class ArrayStoreExceptionToTypeNotPresentException extends Recipe {
31+
32+
private static final String ARRAY_STORE_EXCEPTION = "java.lang.ArrayStoreException";
33+
private static final String TYPE_NOT_PRESENT_EXCEPTION = "java.lang.TypeNotPresentException";
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Catch `TypeNotPresentException` thrown by `Class.getAnnotation()`";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "Replace catch blocks for `ArrayStoreException` around `Class.getAnnotation()` with `TypeNotPresentException` to ensure compatibility with Java 11+.";
43+
}
44+
45+
@Override
46+
public TreeVisitor<?, ExecutionContext> getVisitor() {
47+
String classGetAnnotationPattern = "java.lang.Class getAnnotation(java.lang.Class)";
48+
return Preconditions.check(new UsesMethod<>(classGetAnnotationPattern), new JavaIsoVisitor<ExecutionContext>() {
49+
@Override
50+
public J.Try visitTry(J.Try tryStatement, ExecutionContext ctx) {
51+
J.Try try_ = super.visitTry(tryStatement, ctx);
52+
if (FindMethods.find(try_, classGetAnnotationPattern).isEmpty()) {
53+
return try_;
54+
}
55+
return try_.withCatches(ListUtils.map(try_.getCatches(), catch_ -> {
56+
if (TypeUtils.isOfClassType(catch_.getParameter().getType(), ARRAY_STORE_EXCEPTION)) {
57+
return (J.Try.Catch) new ChangeType(ARRAY_STORE_EXCEPTION, TYPE_NOT_PRESENT_EXCEPTION, true)
58+
.getVisitor().visit(catch_, ctx);
59+
}
60+
return catch_;
61+
}));
62+
}
63+
});
64+
}
65+
}

src/main/java/org/openrewrite/java/migrate/RemoveMethodInvocation.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,25 @@ abstract class AbstractNoGuavaImmutableOf extends Recipe {
3737
private final String guavaType;
3838
private final String javaType;
3939

40+
@Option(displayName = "Whether to convert return type (the default value is false).",
41+
description = "converting the return type from Guava Type to Java Type " +
42+
"The default value is false.",
43+
example = "true",
44+
required = false)
45+
@Nullable
46+
Boolean convertReturnType;
47+
4048
AbstractNoGuavaImmutableOf(String guavaType, String javaType) {
4149
this.guavaType = guavaType;
4250
this.javaType = javaType;
4351
}
4452

53+
AbstractNoGuavaImmutableOf(String guavaType, String javaType, @Nullable Boolean convertReturnType) {
54+
this.guavaType = guavaType;
55+
this.javaType = javaType;
56+
this.convertReturnType = convertReturnType;
57+
}
58+
4559
private String getShortType(String fullyQualifiedType) {
4660
return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1);
4761
}
@@ -103,8 +117,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
103117
@Override
104118
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
105119
J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx);
106-
107-
if (multiVariable != mv && TypeUtils.isOfClassType(mv.getType(), guavaType)) {
120+
if (Boolean.TRUE.equals(convertReturnType) &&
121+
multiVariable != mv &&
122+
TypeUtils.isOfClassType(mv.getType(), guavaType)) {
108123
JavaType newType = JavaType.buildType(javaType);
109124
mv = mv.withTypeExpression(mv.getTypeExpression() == null ?
110125
null : createNewTypeExpression(mv.getTypeExpression(), newType));
@@ -117,7 +132,6 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
117132
return variable;
118133
}));
119134
}
120-
121135
return mv;
122136
}
123137

@@ -148,7 +162,6 @@ private TypeTree createNewTypeExpression(TypeTree typeTree, JavaType newType) {
148162
);
149163
}
150164

151-
152165
private boolean isParentTypeDownCast(MethodCall immutableMethod) {
153166
J parent = getCursor().dropParentUntil(J.class::isInstance).getValue();
154167
boolean isParentTypeDownCast = false;
@@ -173,10 +186,12 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {
173186
} else if (parent instanceof J.MethodInvocation) {
174187
J.MethodInvocation m = (J.MethodInvocation) parent;
175188
int index = m.getArguments().indexOf(immutableMethod);
176-
if (m.getMethodType() != null && index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
177-
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
178-
} else {
179-
isParentTypeDownCast = true;
189+
if (m.getMethodType() != null) {
190+
if (index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
191+
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
192+
} else {
193+
isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType(), guavaType);
194+
}
180195
}
181196
} else if (parent instanceof J.NewClass) {
182197
J.NewClass c = (J.NewClass) parent;
@@ -207,8 +222,8 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {
207222
private boolean isParentTypeMatched(@Nullable JavaType type) {
208223
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type);
209224
return TypeUtils.isOfClassType(fq, javaType) ||
210-
TypeUtils.isOfClassType(fq, "java.lang.Object") ||
211-
TypeUtils.isOfClassType(fq, guavaType);
225+
(Boolean.TRUE.equals(convertReturnType) && TypeUtils.isOfClassType(fq, guavaType)) ||
226+
TypeUtils.isOfClassType(fq, "java.lang.Object");
212227
}
213228
});
214229
}

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableListOf.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package org.openrewrite.java.migrate.guava;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
public class NoGuavaImmutableListOf extends AbstractNoGuavaImmutableOf {
1921
public NoGuavaImmutableListOf() {
2022
super("com.google.common.collect.ImmutableList", "java.util.List");
2123
}
24+
25+
public NoGuavaImmutableListOf(@Nullable Boolean convertReturnType) {
26+
super("com.google.common.collect.ImmutableList", "java.util.List", convertReturnType);
27+
}
2228
}

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableMapOf.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package org.openrewrite.java.migrate.guava;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
public class NoGuavaImmutableMapOf extends AbstractNoGuavaImmutableOf {
1921
public NoGuavaImmutableMapOf() {
2022
super("com.google.common.collect.ImmutableMap", "java.util.Map");
2123
}
24+
25+
public NoGuavaImmutableMapOf(@Nullable Boolean convertReturnType) {
26+
super("com.google.common.collect.ImmutableMap", "java.util.Map", convertReturnType);
27+
}
2228
}

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaImmutableSetOf.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package org.openrewrite.java.migrate.guava;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
public class NoGuavaImmutableSetOf extends AbstractNoGuavaImmutableOf {
1921
public NoGuavaImmutableSetOf() {
2022
super("com.google.common.collect.ImmutableSet", "java.util.Set");
2123
}
24+
25+
public NoGuavaImmutableSetOf(@Nullable Boolean convertReturnType) {
26+
super("com.google.common.collect.ImmutableSet", "java.util.Set", convertReturnType);
27+
}
2228
}

src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
import org.openrewrite.java.JavaParser;
2323
import org.openrewrite.java.JavaTemplate;
2424
import org.openrewrite.java.MethodMatcher;
25+
import org.openrewrite.java.tree.Expression;
2526
import org.openrewrite.java.tree.J;
2627

28+
import java.util.Collections;
29+
import java.util.List;
30+
2731
public class UpdateBeanManagerMethods extends Recipe {
2832
@Override
2933
public String getDisplayName() {
@@ -44,25 +48,39 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4448
@Override
4549
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
4650
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
47-
if (fireEventMatcher.matches(method)) {
48-
return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent().fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})")
51+
List<Expression> arguments = mi.getArguments();
52+
if (fireEventMatcher.matches(method) && mi.getSelect() != null) {
53+
if (arguments.size() <= 1) {
54+
return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent()" +
55+
".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})")
56+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4"))
57+
.build()
58+
.apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), arguments.get(0));
59+
}
60+
61+
Object[] args = new Expression[arguments.size() + 1];
62+
args[0] = mi.getSelect();
63+
for (int i = 1; i < arguments.size(); i++) {
64+
args[i] = arguments.get(i);
65+
}
66+
args[arguments.size()] = arguments.get(0);
67+
68+
String template = "#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent()" +
69+
".select(" + String.join(", ", Collections.nCopies(arguments.size() - 1, "#{any(java.lang.annotation.Annotation)}")) + ')' +
70+
".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})";
71+
return JavaTemplate.builder(template)
4972
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4"))
5073
.build()
51-
.apply(updateCursor(mi),
52-
mi.getCoordinates().replace(),
53-
mi.getSelect(),
54-
mi.getArguments().get(0));
55-
} else if (createInjectionTargetMatcher.matches(method)) {
74+
.apply(updateCursor(mi), mi.getCoordinates().replace(), args);
75+
} else if (createInjectionTargetMatcher.matches(method) && mi.getSelect() != null) {
5676
return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getInjectionTargetFactory(#{any(jakarta.enterprise.inject.spi.AnnotatedType)}).createInjectionTarget(null)")
5777
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4"))
5878
.build()
59-
.apply(updateCursor(mi),
60-
mi.getCoordinates().replace(),
61-
mi.getSelect(),
62-
mi.getArguments().get(0));
79+
.apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), arguments.get(0));
6380
}
6481
return mi;
6582
}
83+
6684
};
6785
}
6886
}

src/main/java/org/openrewrite/java/migrate/javax/AddColumnAnnotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
9090

9191
// Update existing @Column annotation
9292
J.VariableDeclarations updatedVariable = (J.VariableDeclarations) new AddOrUpdateAnnotationAttribute(
93-
"javax.persistence.Column", "name", "element", true)
93+
"javax.persistence.Column", "name", "element", true, null)
9494
.getVisitor().visit(multiVariable, ctx, getCursor().getParentTreeCursor());
9595
return super.visitVariableDeclarations(updatedVariable, ctx);
9696
}

src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean acc) {
9797
if (acc.get()) {
9898
return (J) tree;
9999
}
100-
J t = new UsesType<ExecutionContext>("javax.xml.bind..*", true).visit(tree, ctx);
100+
Tree t = new UsesType<ExecutionContext>("javax.xml.bind..*", true).visit(tree, ctx);
101101
if (t != tree) {
102102
acc.set(true);
103103
}

0 commit comments

Comments
 (0)