From bca5be545f722f8e2d5597b0d1e6a3006fe215dc Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 19 Dec 2024 11:09:05 +0100 Subject: [PATCH] Use `getParentTreeCursor()` in `Assertions` --- .../java/org/openrewrite/kotlin/Assertions.java | 16 ++++++---------- .../kotlin/KotlinTypeMappingTest.java | 16 ++++++++-------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/Assertions.java b/src/main/java/org/openrewrite/kotlin/Assertions.java index 7e10c00c..a7f4145b 100644 --- a/src/main/java/org/openrewrite/kotlin/Assertions.java +++ b/src/main/java/org/openrewrite/kotlin/Assertions.java @@ -478,18 +478,15 @@ private boolean inImport() { } private boolean isClassName() { - Cursor parent = getCursor().getParent(); - return parent != null && parent.getValue() instanceof J.ClassDeclaration; + return getCursor().getParentTreeCursor().getValue() instanceof J.ClassDeclaration; } private boolean isMethodName() { - Cursor parent = getCursor().getParent(); - return parent != null && parent.getValue() instanceof J.MethodDeclaration; + return getCursor().getParentTreeCursor().getValue() instanceof J.MethodDeclaration; } private boolean isMethodInvocationName() { - Cursor parent = getCursor().getParent(); - return parent != null && parent.getValue() instanceof J.MethodInvocation; + return getCursor().getParentTreeCursor().getValue() instanceof J.MethodInvocation; } private boolean isFieldAccess(J.Identifier ident) { @@ -515,8 +512,7 @@ private boolean isNewClass(J.Identifier ident) { } private boolean isTypeParameter() { - return getCursor().getParent() != null && - getCursor().getParent().getValue() instanceof J.TypeParameter; + return getCursor().getParentTreeCursor().getValue() instanceof J.TypeParameter; } private boolean isMemberReference(J.Identifier ident) { @@ -550,8 +546,8 @@ private boolean isLabel() { } private boolean isAnnotationField(J.Identifier ident) { - Cursor parent = getCursor().getParent(); - return parent != null && parent.getValue() instanceof J.Assignment && + Cursor parent = getCursor().getParentTreeCursor(); + return parent.getValue() instanceof J.Assignment && (ident == ((J.Assignment) parent.getValue()).getVariable() && getCursor().firstEnclosing(J.Annotation.class) != null); } diff --git a/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java b/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java index 04743d05..48e4f5d4 100644 --- a/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java +++ b/src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java @@ -343,7 +343,7 @@ void javaLangObject() { JavaType.Class c = (JavaType.Class) firstMethodParameter("javaType"); assertThat(c.getFullyQualifiedName()).isEqualTo("java.lang.Object"); assertThat(c.getSupertype()).isNull(); - assertThat(c.getMethods()).hasSize(12); + assertThat(c.getMethods()).hasSize(13); // Assert generic type parameters have the correct type bounds. JavaType.Method method = c.getMethods().stream().filter(it -> "getClass".equals(it.getName())).findFirst().orElse(null); @@ -395,7 +395,7 @@ void coneFlexibleType() { kotlin( """ import java.lang.invoke.TypeDescriptor.OfField - + abstract class Foo : OfField> """, spec -> spec.afterRecipe(cu -> { AtomicBoolean found = new AtomicBoolean(false); @@ -737,7 +737,7 @@ void parameterizedType() { kotlin( """ import java.util.ArrayList - + class Foo { val l: ArrayList = ArrayList() } @@ -862,7 +862,7 @@ void privateToThisModifier() { @file:Suppress("UNUSED_VARIABLE") class A(t: T) { private val t: T = t // visibility for t is PRIVATE_TO_THIS - + fun test() { val x: T = t // correct val y: T = this.t // also correct @@ -882,7 +882,7 @@ void javaTypeFullyQualified() { @file:Suppress("UNUSED_PARAMETER") open class Object class Test(name: String, any: Any) - + fun foo(name: String) = Test(name, object : Object() {}) """ @@ -937,7 +937,7 @@ void packageStaticModifier() { kotlin( """ import java.rmi.server.RemoteStub - + class A : RemoteStub() """ ) @@ -976,7 +976,7 @@ interface A interface B interface C interface D - + class KotlinTypeGoat where S : A, T : D, S : B, T : C """, spec -> spec.afterRecipe(cu -> { AtomicBoolean found = new AtomicBoolean(false); @@ -1611,7 +1611,7 @@ class C } } } - + val x = A.B.A.C() """, spec -> spec.afterRecipe(cu -> {