Skip to content

Commit

Permalink
Add backwards compatibility constructors (#4933)
Browse files Browse the repository at this point in the history
* Add backwards compatibility constructors

For the two recent changes to `J.Case` and `J.SwitchExpression` which both added new fields, the constructor changed and no constructor was added for API backwards compatibility.

This commit adds the corresponding constructors and annotates them both as `@Deprecated` and as `@ScheduledForRemoval` (from the JetBrains annotations). For the latter we provide a date value for the `inVersion` attribute as in `@ScheduledForRemoval(inVersion = "2025-05-01")`. This will allow us to implement some simple tooling to weed these constructors out again, without this chore getting forgotten.

* Polish
  • Loading branch information
knutwannheden authored Jan 22, 2025
1 parent 2aadb97 commit 354101d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
Expand All @@ -42,6 +43,7 @@
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -1113,6 +1115,12 @@ public Case(UUID id, Space prefix, Markers markers, Type type, @Deprecated @Null
this.body = body;
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2025-05-01")
public Case(UUID id, Space prefix, Markers markers, Type type, @Deprecated @Nullable Expression pattern, JContainer<Expression> expressions, JContainer<Statement> statements, @Nullable JRightPadded<J> body) {
this(id, prefix, markers, type, pattern, expressions, null, null, statements, body);
}

@Override
public <P> J acceptJava(JavaVisitor<P> v, P p) {
return v.visitCase(this, p);
Expand Down Expand Up @@ -5096,6 +5104,7 @@ public CoordinateBuilder.Statement getCoordinates() {
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@Data
@RequiredArgsConstructor
final class SwitchExpression implements J, Expression, TypedTree {
@With
@EqualsAndHashCode.Include
Expand All @@ -5118,6 +5127,21 @@ final class SwitchExpression implements J, Expression, TypedTree {
@Getter
JavaType type;

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2025-05-01")
public SwitchExpression(UUID id, Space prefix, Markers markers, ControlParentheses<Expression> selector, Block cases) {
this(id, prefix, markers, selector, cases, new JavaVisitor<AtomicReference<@Nullable JavaType>>() {
@Override
public J visitBlock(Block block, AtomicReference<@Nullable JavaType> javaType) {
if (!block.getStatements().isEmpty()) {
Case caze = (Case) block.getStatements().get(0);
javaType.set(caze.getExpressions().isEmpty() ? null : caze.getExpressions().get(0).getType());
}
return block;
}
}.reduce(cases, new AtomicReference<>()).get());
}

@Override
public <P> J acceptJava(JavaVisitor<P> v, P p) {
return v.visitSwitchExpression(this, p);
Expand Down

0 comments on commit 354101d

Please sign in to comment.