Skip to content

Commit 483b01e

Browse files
authored
Merge branch 'main' into hotfix-NoGuavaJava21
2 parents 1388ca4 + c48a656 commit 483b01e

19 files changed

+414
-244
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=57dafb5c2622c6cc08b993c85b7c06956a2f53536432a30ead46166dbca0f1e9
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-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/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/lang/MigrateSecurityManagerMulticast.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
5353
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
5454

5555
if (MULTICAST_METHOD.matches(m) && m.getArguments().size() == 2) {
56-
return m.withArguments(Collections.singletonList(m.getArguments().get(0)));
56+
return m.withArguments(Collections.singletonList(m.getArguments().get(0)))
57+
.withMethodType(m.getMethodType()
58+
.withParameterNames(m.getMethodType().getParameterNames().subList(0, 1))
59+
.withParameterTypes(m.getMethodType().getParameterTypes().subList(0, 1))
60+
);
5761
}
5862
return m;
5963
}

src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java

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

18+
import org.jspecify.annotations.Nullable;
1819
import org.openrewrite.Cursor;
1920
import org.openrewrite.java.tree.*;
2021

@@ -198,4 +199,30 @@ private static boolean isInsideInitializer(Cursor cursor, int nestedBlockLevel)
198199

199200
return isInsideInitializer(requireNonNull(cursor.getParent()), nestedBlockLevel);
200201
}
202+
203+
/**
204+
* Checks whether the initializer {@linkplain Expression} is a {@linkplain J.MethodInvocation} targeting a static method.
205+
*
206+
* @param initializer {@linkplain J.VariableDeclarations.NamedVariable#getInitializer()} value
207+
* @return true iff is initialized by static method
208+
*/
209+
public static boolean initializedByStaticMethod(@Nullable Expression initializer) {
210+
if (initializer == null) {
211+
return false;
212+
}
213+
initializer = initializer.unwrap();
214+
215+
if (!(initializer instanceof J.MethodInvocation)) {
216+
// no MethodInvocation -> false
217+
return false;
218+
}
219+
220+
J.MethodInvocation invocation = (J.MethodInvocation) initializer;
221+
if (invocation.getMethodType() == null) {
222+
// not a static method -> false
223+
return false;
224+
}
225+
226+
return invocation.getMethodType().hasFlags(Flag.Static);
227+
}
201228
}

src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
7575
boolean isPrimitive = DeclarationCheck.isPrimitive(vd);
7676
boolean usesGenerics = DeclarationCheck.useGenerics(vd);
7777
boolean usesTernary = DeclarationCheck.initializedByTernary(vd);
78-
boolean usesArrayInitializer = vd.getVariables().get(0).getInitializer() instanceof J.NewArray;
79-
if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer) {
78+
Expression initializer = vd.getVariables().get(0).getInitializer();
79+
boolean usesArrayInitializer = initializer instanceof J.NewArray;
80+
boolean initializedByStaticMethod = DeclarationCheck.initializedByStaticMethod(initializer);
81+
if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer || initializedByStaticMethod) {
8082
return vd;
8183
}
8284

src/main/java/org/openrewrite/java/migrate/maven/UpdateMavenProjectPropertyJavaVersion.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
8888
Xml.Document d = super.visitDocument(document, ctx);
8989

9090
// Return early if the parent appears to be within the current repository, as properties defined there will be updated
91-
if (d.getRoot().getChild("parent")
92-
.flatMap(parent -> parent.getChild("relativePath"))
93-
.flatMap(Xml.Tag::getValue)
94-
.isPresent()) {
91+
if (getResolutionResult().parentPomIsProjectPom()) {
9592
return d;
9693
}
9794

src/main/java/org/openrewrite/java/migrate/util/OptionalStreamRecipe.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5050
}
5151

5252
private static class OptionalStreamVisitor extends JavaIsoVisitor<ExecutionContext> {
53-
private static final JavaTemplate template =
54-
JavaTemplate.builder("#{any(java.util.stream.Stream)}.flatMap(Optional::stream)")
55-
.imports("java.util.Optional")
56-
.build();
5753

5854
@Override
5955
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation invocation, ExecutionContext ctx) {
@@ -77,6 +73,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation invocation, E
7773
JRightPadded<Expression> mapSelect = mapInvocation.getPadding().getSelect();
7874
JavaType.Method mapInvocationType = mapInvocation.getMethodType();
7975
Space flatMapComments = getFlatMapComments(mapSelect, filterSelect);
76+
JavaTemplate template =
77+
JavaTemplate.builder("#{any(java.util.stream.Stream)}.flatMap(Optional::stream)")
78+
.imports("java.util.Optional")
79+
.build();
8080
J.MethodInvocation flatMapInvocation = template
8181
.apply(updateCursor(mapInvocation), mapInvocation.getCoordinates().replace(), filterInvocation.getSelect());
8282
return flatMapInvocation.getPadding()

src/main/resources/META-INF/rewrite/jakarta-ee-10.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ type: specs.openrewrite.org/v1beta/recipe
4242
name: org.openrewrite.java.migrate.jakarta.ServletCookieBehaviorChangeRFC6265
4343
displayName: Remove `getComment` and `getVersion` methods
4444
description: >-
45-
Jakarta Servlet methods have been deprecated for removal in Jakarta Servlet 6.0 to align with RFC 6265.
45+
Jakarta Servlet methods have been deprecated for removal in Jakarta Servlet 6.0 to align with RFC 6265.
4646
In addition, the behavior of these methods has been changed so the setters no longer have any effect, the getComment methods return null, and the getVersion method returns 0.
4747
The deprecated methods are removed.
4848
recipeList:
49-
- org.openrewrite.java.migrate.RemoveMethodInvocation:
49+
- org.openrewrite.java.RemoveMethodInvocations:
5050
methodPattern: jakarta.servlet.http.Cookie getComment()
51-
- org.openrewrite.java.migrate.RemoveMethodInvocation:
51+
- org.openrewrite.java.RemoveMethodInvocations:
5252
methodPattern: jakarta.servlet.http.Cookie getVersion()
53-
- org.openrewrite.java.migrate.RemoveMethodInvocation:
53+
- org.openrewrite.java.RemoveMethodInvocations:
5454
methodPattern: jakarta.servlet.http.Cookie setComment(String)
55-
- org.openrewrite.java.migrate.RemoveMethodInvocation:
55+
- org.openrewrite.java.RemoveMethodInvocations:
5656
methodPattern: jakarta.servlet.http.Cookie setVersion(int)
57-
- org.openrewrite.java.migrate.RemoveMethodInvocation:
57+
- org.openrewrite.java.RemoveMethodInvocations:
5858
methodPattern: jakarta.servlet.SessionCookieConfig getComment()
59-
- org.openrewrite.java.migrate.RemoveMethodInvocation:
59+
- org.openrewrite.java.RemoveMethodInvocations:
6060
methodPattern: jakarta.servlet.SessionCookieConfig setComment(String)
6161
---
6262
type: specs.openrewrite.org/v1beta/recipe
@@ -75,7 +75,7 @@ type: specs.openrewrite.org/v1beta/recipe
7575
name: org.openrewrite.java.migrate.jakarta.RemovedIsParmetersProvidedMethod
7676
displayName: Use `isParametersProvided()`
7777
description: >-
78-
Expression Language prior to 5.0 provides the deprecated MethodExpression.isParmetersProvided() method, with the word 'parameter' misspelled in the method name.
78+
Expression Language prior to 5.0 provides the deprecated MethodExpression.isParmetersProvided() method, with the word 'parameter' misspelled in the method name.
7979
This method is unavailable in Jakarta Expression Language 5.0. Use the correctly spelled MethodExpression.isParametersProvided() method instead.
8080
recipeList:
8181
- org.openrewrite.java.ChangeMethodName:
@@ -86,7 +86,7 @@ type: specs.openrewrite.org/v1beta/recipe
8686
name: org.openrewrite.java.migrate.jakarta.RemovedSOAPElementFactory
8787
displayName: Use `jakarta.xml.soap.SOAPFactory` to create `SOAPElements`
8888
description: >-
89-
XML Web Services prior to 4.0 provides the deprecated SOAPElementFactory class,
89+
XML Web Services prior to 4.0 provides the deprecated SOAPElementFactory class,
9090
which is removed in XML Web Services 4.0. The recommended replacement is to use jakarta.xml.soap.SOAPFactory to create SOAPElements.
9191
recipeList:
9292
- org.openrewrite.java.ChangeMethodName:

0 commit comments

Comments
 (0)