From e599c58a7134e9e954d44b71fb81fa8a35b46e10 Mon Sep 17 00:00:00 2001 From: Kun Li Date: Mon, 11 Dec 2023 11:02:00 -0800 Subject: [PATCH] Revert "Deprecate Property#setter, getter and isSetterFirst fileds, Intorduce accessors as an replacement" This reverts commit 97311bb1e7f7c9696fb5aa7a45bc717509a7bdf8. --- .../org/openrewrite/kotlin/KotlinVisitor.java | 3 ++- .../kotlin/internal/KotlinPrinter.java | 8 ++++++- .../internal/KotlinTreeParserVisitor.java | 23 +++---------------- .../java/org/openrewrite/kotlin/tree/K.java | 13 ++++------- .../kotlin/KotlinTypeMappingTest.java | 20 ++++++++-------- 5 files changed, 26 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/KotlinVisitor.java b/src/main/java/org/openrewrite/kotlin/KotlinVisitor.java index da83784d4..aafa11530 100644 --- a/src/main/java/org/openrewrite/kotlin/KotlinVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/KotlinVisitor.java @@ -276,7 +276,8 @@ public J visitProperty(K.Property property, P p) { pr = pr.withVariableDeclarations(visitAndCast(pr.getVariableDeclarations(), p)); pr = pr.getPadding().withReceiver(visitRightPadded(pr.getPadding().getReceiver(), p)); - pr = pr.withAccessors(visitContainer(pr.getAccessors(), p)); + pr = pr.withGetter(visitAndCast(pr.getGetter(), p)); + pr = pr.withSetter(visitAndCast(pr.getSetter(), p)); return pr; } diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java index e5dbee8b4..e8e907972 100755 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java @@ -395,7 +395,13 @@ public J visitProperty(K.Property property, PrintOutputCapture

p) { delegate.visitContainer("where", property.getTypeConstraints().getPadding().getConstraints(), JContainer.Location.TYPE_PARAMETERS, ",", "", p); } - visitContainer(property.getAccessors(), p); + if (property.isSetterFirst()) { + visit(property.getSetter(), p); + visit(property.getGetter(), p); + } else { + visit(property.getGetter(), p); + visit(property.getSetter(), p); + } afterSyntax(property, p); return property; diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index 9db4330b7..bb08d12f4 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -2892,22 +2892,6 @@ public J visitProperty(KtProperty property, ExecutionContext data) { } if (getter != null || setter != null || receiver != null || typeConstraints != null) { - List> rps = new ArrayList<>(2); - - if (setter != null) { - rps.add(padRight(setter, Space.EMPTY)); - } - - if (getter != null) { - rps.add(padRight(getter, Space.EMPTY)); - } - - if (!isSetterFirst) { - Collections.reverse(rps); - } - - JContainer accessors = JContainer.build(rps); - return new K.Property( randomId(), deepPrefix(property), @@ -2915,10 +2899,9 @@ public J visitProperty(KtProperty property, ExecutionContext data) { typeParameters, variableDeclarations.withPrefix(Space.EMPTY), typeConstraints, - accessors, - null, - null, - false, + getter, + setter, + isSetterFirst, receiver ); } else { diff --git a/src/main/java/org/openrewrite/kotlin/tree/K.java b/src/main/java/org/openrewrite/kotlin/tree/K.java index 178d17af4..075abbd30 100644 --- a/src/main/java/org/openrewrite/kotlin/tree/K.java +++ b/src/main/java/org/openrewrite/kotlin/tree/K.java @@ -15,6 +15,7 @@ */ package org.openrewrite.kotlin.tree; +import com.fasterxml.jackson.annotation.JsonCreator; import lombok.*; import lombok.experimental.FieldDefaults; import lombok.experimental.NonFinal; @@ -754,7 +755,7 @@ public String toString() { } } - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "DeprecatedIsStillUsed"}) @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor @@ -1110,6 +1111,7 @@ public FunctionType withParameters(@Nullable JContainer parameters) { } } + @SuppressWarnings("DeprecatedIsStillUsed") @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @AllArgsConstructor(access = AccessLevel.PRIVATE) @@ -1440,17 +1442,12 @@ public List getTypeParameters() { @Nullable TypeConstraints typeConstraints; - JContainer accessors; - - @Deprecated // Use `accessors` field instead @Nullable J.MethodDeclaration getter; - @Deprecated // Use `accessors` field instead @Nullable J.MethodDeclaration setter; - @Deprecated // Use `accessors` field instead boolean isSetterFirst; @Nullable @@ -1504,7 +1501,7 @@ public JContainer getTypeParameters() { public Property withTypeParameters(@Nullable JContainer typeParameters) { return t.typeParameters == typeParameters ? t : new Property(t.id, t.prefix, t.markers, typeParameters, - t.variableDeclarations, t.typeConstraints, t.accessors ,null, null, false, t.receiver); + t.variableDeclarations, t.typeConstraints, t.getter, t.setter, t.isSetterFirst, t.receiver); } @Nullable @@ -1515,7 +1512,7 @@ public JRightPadded getReceiver() { @Nullable public Property withReceiver(@Nullable JRightPadded receiver) { return t.receiver == receiver ? t : new Property(t.id, t.prefix, t.markers, t.typeParameters, - t.variableDeclarations, t.typeConstraints, t.accessors, null, null, false, receiver); + t.variableDeclarations, t.typeConstraints, t.getter, t.setter, t.isSetterFirst, receiver); } } } diff --git a/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java b/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java index 850b11bdf..cb047009f 100644 --- a/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java +++ b/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java @@ -112,20 +112,18 @@ void fieldType() { assertThat(id.getType()).isInstanceOf(JavaType.Class.class); assertThat(id.getType().toString()).isEqualTo("kotlin.Int"); - J.MethodDeclaration getter = property.getAccessors().getElements().stream().filter(x -> x.getName().getSimpleName().equals("get")).findFirst().orElse(null); - JavaType.FullyQualified declaringType = getter.getMethodType().getDeclaringType(); + JavaType.FullyQualified declaringType = property.getGetter().getMethodType().getDeclaringType(); assertThat(declaringType.getFullyQualifiedName()).isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat"); - assertThat(getter.getMethodType().getName()).isEqualTo("get"); - assertThat(getter.getMethodType().getReturnType()).isEqualTo(id.getType()); - assertThat(getter.getName().getType()).isEqualTo(getter.getMethodType()); - assertThat(getter.getMethodType().toString().substring(declaringType.toString().length())).isEqualTo("{name=get,return=kotlin.Int,parameters=[]}"); + assertThat(property.getGetter().getMethodType().getName()).isEqualTo("get"); + assertThat(property.getGetter().getMethodType().getReturnType()).isEqualTo(id.getType()); + assertThat(property.getGetter().getName().getType()).isEqualTo(property.getGetter().getMethodType()); + assertThat(property.getGetter().getMethodType().toString().substring(declaringType.toString().length())).isEqualTo("{name=get,return=kotlin.Int,parameters=[]}"); - J.MethodDeclaration setter = property.getAccessors().getElements().stream().filter(x -> x.getName().getSimpleName().equals("set")).findFirst().orElse(null); - declaringType = setter.getMethodType().getDeclaringType(); + declaringType = property.getSetter().getMethodType().getDeclaringType(); assertThat(declaringType.getFullyQualifiedName()).isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat"); - assertThat(setter.getMethodType().getName()).isEqualTo("set"); - assertThat(setter.getMethodType()).isEqualTo(setter.getName().getType()); - assertThat(setter.getMethodType().toString().substring(declaringType.toString().length())).isEqualTo("{name=set,return=kotlin.Unit,parameters=[kotlin.Int]}"); + assertThat(property.getSetter().getMethodType().getName()).isEqualTo("set"); + assertThat(property.getSetter().getMethodType()).isEqualTo(property.getSetter().getName().getType()); + assertThat(property.getSetter().getMethodType().toString().substring(declaringType.toString().length())).isEqualTo("{name=set,return=kotlin.Unit,parameters=[kotlin.Int]}"); } @Test