rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

`ChangePackage` and `ChangeType` leave old type information in place.

Open traceyyoshima opened this issue 3 years ago • 0 comments

The recipes only change the initial JavaType of the Tree.

Post full type attribution, the target type may exist many times in a JavaType.

The JavaType is not fully visited so old type information will be left in place. Fully visiting a JavaType may be expensive and requires an UnsafeJavaTypeVisitor.

    @Test
    fun updateMethodType() = assertChanged(
        dependsOn = arrayOf("""
            package org.openrewrite;
            public class Test {
            }
            """,
            """
            package org.foo;
            
            import org.openrewrite.Test;
            
            public class Example {
                public static Test method(Test test) {
                    return test;
                }
            }
        """),
        before = """
            import org.openrewrite.Test;
            import org.foo.Example;
            
            public class A {
                Test local = Example.method(null);
            }
        """,
        after = """
            import org.openrewrite.test.Test;
            import org.foo.Example;
            
            public class A {
                Test local = Example.method(null);
            }
        """,
        afterConditions = { cu ->
            val methodType = cu.typesInUse.usedMethods.toTypedArray()[0]
            assertThat(methodType.returnType.asFullyQualified()!!.fullyQualifiedName).isEqualTo("org.openrewrite.test.Test")
            assertThat(methodType.parameterTypes[0].asFullyQualified()!!.fullyQualifiedName).isEqualTo("org.openrewrite.test.Test")
        }
    )

The declaringType of methodType will contain the old type information.

traceyyoshima avatar Jul 06 '22 23:07 traceyyoshima