Skip to content

Commit 6ee0d51

Browse files
authored
Merge branch 'main' into recipe-for-InlineMe-annotation
2 parents 2524fe3 + e584126 commit 6ee0d51

23 files changed

+515
-154
lines changed

.github/workflows/sdkman-candidates.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Checkout
15-
uses: actions/checkout@v4
15+
uses: actions/checkout@v5
1616
with:
1717
fetch-depth: 0
1818

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ recipeDependencies {
1212
parserClasspath("jakarta.servlet:jakarta.servlet-api:6.0.0")
1313
parserClasspath("javax.persistence:javax.persistence-api:2.2")
1414
parserClasspath("org.glassfish:javax.servlet:3.0")
15+
parserClasspath("javax.annotation:javax.annotation-api:1.3.2")
1516
}
1617

1718
val rewriteVersion = rewriteRecipe.rewriteVersion.get()

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeRecipe.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package org.openrewrite.java.migrate.joda;
1717

18-
import lombok.EqualsAndHashCode;
1918
import lombok.Getter;
20-
import lombok.Value;
2119
import org.jspecify.annotations.Nullable;
22-
import org.openrewrite.*;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Preconditions;
22+
import org.openrewrite.ScanningRecipe;
23+
import org.openrewrite.TreeVisitor;
2324
import org.openrewrite.java.search.UsesType;
2425
import org.openrewrite.java.tree.J;
2526
import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable;
@@ -29,23 +30,7 @@
2930

3031
import static java.util.Collections.emptyList;
3132

32-
@EqualsAndHashCode(callSuper = false)
33-
@Value
3433
public class JodaTimeRecipe extends ScanningRecipe<JodaTimeRecipe.Accumulator> {
35-
36-
/**
37-
* Controls whether additional safety checks are performed during the migration process.
38-
* When enabled, the recipe will verify that expressions are safe to migrate before performing the migration.
39-
* This helps prevent potential issues or bugs that might arise from automatic migration.
40-
*/
41-
@Option(displayName = "Safe migration only",
42-
description = "When enabled, performs additional safety checks to verify that expressions are safe to migrate before converting them. " +
43-
"Safety checks include analyzing method parameters, return values, and variable usages across class boundaries.",
44-
required = false
45-
)
46-
@Nullable
47-
Boolean safeMigrationOnly;
48-
4934
@Override
5035
public String getDisplayName() {
5136
return "Migrate Joda-Time to Java time";
@@ -63,12 +48,12 @@ public Accumulator getInitialValue(ExecutionContext ctx) {
6348

6449
@Override
6550
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
66-
return new JodaTimeScanner(acc, Boolean.TRUE.equals(safeMigrationOnly));
51+
return new JodaTimeScanner(acc);
6752
}
6853

6954
@Override
7055
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
71-
JodaTimeVisitor jodaTimeVisitor = new JodaTimeVisitor(acc, Boolean.TRUE.equals(safeMigrationOnly), new LinkedList<>());
56+
JodaTimeVisitor jodaTimeVisitor = new JodaTimeVisitor(acc, true, new LinkedList<>());
7257
return Preconditions.check(new UsesType<>("org.joda.time.*", true), jodaTimeVisitor);
7358
}
7459

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeScanner.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,15 @@ class JodaTimeScanner extends ScopeAwareVisitor {
4141

4242
@Getter
4343
private final JodaTimeRecipe.Accumulator acc;
44-
private final boolean safeMigrationOnly;
4544

4645
private final Map<NamedVariable, Set<NamedVariable>> varDependencies = new HashMap<>();
4746
private final Map<JavaType, Set<String>> unsafeVarsByType = new HashMap<>();
4847
private final Map<JavaType.Method, Set<NamedVariable>> methodReferencedVars = new HashMap<>();
4948
private final Map<JavaType.Method, Set<UnresolvedVar>> methodUnresolvedReferencedVars = new HashMap<>();
5049

5150
public JodaTimeScanner(JodaTimeRecipe.Accumulator acc) {
52-
this(acc, true);
53-
}
54-
55-
public JodaTimeScanner(JodaTimeRecipe.Accumulator acc, boolean safeMigrationOnly) {
5651
super(new LinkedList<>());
5752
this.acc = acc;
58-
this.safeMigrationOnly = safeMigrationOnly;
5953
}
6054

6155
@Override
@@ -73,9 +67,6 @@ public Javadoc visitReference(Javadoc.Reference reference, ExecutionContext ctx)
7367

7468
@Override
7569
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
76-
if (!safeMigrationOnly) { // skip scan if safe mode is disabled
77-
return cu;
78-
}
7970
super.visitCompilationUnit(cu, ctx);
8071
Set<NamedVariable> allReachable = new HashSet<>();
8172
for (NamedVariable var : acc.getUnsafeVars()) {

src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseAssignmentsToSwitchExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public String getDisplayName() {
4848
@Override
4949
public String getDescription() {
5050
return "Switch statements for which each case is assigning a value to the same variable can be converted to a switch expression that returns the value of the variable. " +
51-
"This is only applicable for Java 17 and later.";
51+
"This recipe is only applicable for Java 21 and later.";
5252
}
5353

5454
@Override
5555
public TreeVisitor<?, ExecutionContext> getVisitor() {
5656
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
57-
new UsesJavaVersion<>(17),
57+
new UsesJavaVersion<>(21),
5858
Preconditions.not(new KotlinFileChecker<>()),
5959
Preconditions.not(new GroovyFileChecker<>())
6060
);

src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseReturnsToSwitchExpression.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;
3232

3333
import java.util.List;
34+
import java.util.concurrent.atomic.AtomicReference;
3435

3536
import static org.openrewrite.Tree.randomId;
3637

@@ -44,31 +45,32 @@ public String getDisplayName() {
4445

4546
@Override
4647
public String getDescription() {
47-
return "Switch statements where each case returns a value can be converted to a switch expression that returns the value directly.";
48+
return "Switch statements where each case returns a value can be converted to a switch expression that returns the value directly. " +
49+
"This recipe is only applicable for Java 21 and later.";
4850
}
4951

5052
@Override
5153
public TreeVisitor<?, ExecutionContext> getVisitor() {
5254
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
53-
new UsesJavaVersion<>(14),
55+
new UsesJavaVersion<>(21),
5456
Preconditions.not(new KotlinFileChecker<>()),
5557
Preconditions.not(new GroovyFileChecker<>())
5658
);
5759
return Preconditions.check(preconditions, new JavaIsoVisitor<ExecutionContext>() {
5860
@Override
5961
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
6062
J.Block b = super.visitBlock(block, ctx);
63+
AtomicReference<Boolean> newReturn = new AtomicReference<>(false);
6164
return b.withStatements(ListUtils.map(b.getStatements(), statement -> {
65+
if (newReturn.get()) {
66+
return null; // Drop statements after the first converted switch expression
67+
}
6268
if (statement instanceof J.Switch) {
6369
J.Switch sw = (J.Switch) statement;
6470
if (canConvertToSwitchExpression(sw)) {
71+
newReturn.set(true);
6572
J.SwitchExpression switchExpression = convertToSwitchExpression(sw);
66-
return new J.Return(
67-
randomId(),
68-
sw.getPrefix(),
69-
Markers.EMPTY,
70-
switchExpression
71-
);
73+
return new J.Return(randomId(), sw.getPrefix(), Markers.EMPTY, switchExpression);
7274
}
7375
}
7476
return statement;

src/main/java/org/openrewrite/java/migrate/lang/SwitchExpressionYieldToArrow.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public String getDisplayName() {
4343

4444
@Override
4545
public String getDescription() {
46-
return "Convert switch expressions with colon cases and yield statements to arrow syntax.";
46+
return "Convert switch expressions with colon cases and yield statements to arrow syntax. " +
47+
"This recipe is only applicable for Java 21 and later.";
4748
}
4849

4950
@Override
5051
public TreeVisitor<?, ExecutionContext> getVisitor() {
5152
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
52-
new UsesJavaVersion<>(14),
53+
new UsesJavaVersion<>(21),
5354
Preconditions.not(new KotlinFileChecker<>()),
5455
Preconditions.not(new GroovyFileChecker<>())
5556
);

src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
106106
}
107107

108108
String content = contentSb.toString();
109+
if (content.contains("\r")) {
110+
// Carriage returns aren't yet carried over into text blocks, which always end with a newline \n
111+
return super.visitBinary(binary, ctx);
112+
}
109113

110114
if (!convertStringsWithoutNewlines && !containsNewLineInContent(content)) {
111115
return super.visitBinary(binary, ctx);

src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ tags:
2626
- jsr250
2727
- jakarta
2828
recipeList:
29-
# Add or update the jakarta.annotation-api to a maven project. This artifact still uses the javax.annotation name space.
29+
# Add or update the jakarta.annotation-api. Use version 1.3.x, as this version still kept the old javax namespace (starting from 2.x it's changed to jakarta).
3030
- org.openrewrite.java.dependencies.ChangeDependency:
3131
oldGroupId: javax.annotation
3232
oldArtifactId: javax.annotation-api
3333
newGroupId: jakarta.annotation
3434
newArtifactId: jakarta.annotation-api
3535
newVersion: 1.3.x
36+
# set explicitly even if managed, as higher versions do not have the javax namespace
37+
overrideManagedVersion: true
38+
changeManagedDependency: false
3639
# Jakarta EE recommends `provided` scope
3740
- org.openrewrite.maven.ChangeDependencyScope:
3841
groupId: jakarta.annotation
509 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)