Skip to content

Commit

Permalink
Revert "Deprecate Property#setter, getter and isSetterFirst fileds, I…
Browse files Browse the repository at this point in the history
…ntorduce accessors as an replacement"

This reverts commit 97311bb.
  • Loading branch information
kunli2 committed Dec 11, 2023
1 parent 4a573ae commit e599c58
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/openrewrite/kotlin/KotlinVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,13 @@ public J visitProperty(K.Property property, PrintOutputCapture<P> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2892,33 +2892,16 @@ public J visitProperty(KtProperty property, ExecutionContext data) {
}

if (getter != null || setter != null || receiver != null || typeConstraints != null) {
List<JRightPadded<J.MethodDeclaration>> 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<J.MethodDeclaration> accessors = JContainer.build(rps);

return new K.Property(
randomId(),
deepPrefix(property),
markers,
typeParameters,
variableDeclarations.withPrefix(Space.EMPTY),
typeConstraints,
accessors,
null,
null,
false,
getter,
setter,
isSetterFirst,
receiver
);
} else {
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/org/openrewrite/kotlin/tree/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -754,7 +755,7 @@ public String toString() {
}
}

@SuppressWarnings("unused")
@SuppressWarnings({"unused", "DeprecatedIsStillUsed"})
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -1110,6 +1111,7 @@ public FunctionType withParameters(@Nullable JContainer<TypeTree> parameters) {
}
}

@SuppressWarnings("DeprecatedIsStillUsed")
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -1440,17 +1442,12 @@ public List<TypeParameter> getTypeParameters() {
@Nullable
TypeConstraints typeConstraints;

JContainer<J.MethodDeclaration> 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
Expand Down Expand Up @@ -1504,7 +1501,7 @@ public JContainer<TypeParameter> getTypeParameters() {

public Property withTypeParameters(@Nullable JContainer<TypeParameter> 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
Expand All @@ -1515,7 +1512,7 @@ public JRightPadded<Expression> getReceiver() {
@Nullable
public Property withReceiver(@Nullable JRightPadded<Expression> 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);
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e599c58

Please sign in to comment.