From 44f1760cf56c5d55b5930a807f4f7e2e7ccc43dd Mon Sep 17 00:00:00 2001 From: daviss Date: Fri, 27 Oct 2023 11:11:35 -0700 Subject: [PATCH] pass in resolved type info This avoids the infinite poetry in https://github.com/uber/motif/issues/255 --- .../main/kotlin/motif/compiler/XFunSpec.kt | 28 +++------ .../testcases/KT006_reference_self/Child.kt | 24 ++++++++ .../testcases/KT006_reference_self/Foo.kt | 22 +++++++ .../testcases/KT006_reference_self/GRAPH.txt | 57 +++++++++++++++++++ .../testcases/KT006_reference_self/Scope.kt | 31 ++++++++++ .../testcases/KT006_reference_self/Test.java | 26 +++++++++ 6 files changed, 168 insertions(+), 20 deletions(-) create mode 100644 tests/src/main/java/testcases/KT006_reference_self/Child.kt create mode 100644 tests/src/main/java/testcases/KT006_reference_self/Foo.kt create mode 100644 tests/src/main/java/testcases/KT006_reference_self/GRAPH.txt create mode 100644 tests/src/main/java/testcases/KT006_reference_self/Scope.kt create mode 100644 tests/src/main/java/testcases/KT006_reference_self/Test.java diff --git a/compiler/src/main/kotlin/motif/compiler/XFunSpec.kt b/compiler/src/main/kotlin/motif/compiler/XFunSpec.kt index 22547fca..8ce264d5 100644 --- a/compiler/src/main/kotlin/motif/compiler/XFunSpec.kt +++ b/compiler/src/main/kotlin/motif/compiler/XFunSpec.kt @@ -23,13 +23,10 @@ import com.squareup.kotlinpoet.AnnotationSpec import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.ParameterSpec -import com.squareup.kotlinpoet.javapoet.KotlinPoetJavaPoetPreview -import com.squareup.kotlinpoet.javapoet.toKTypeName import com.uber.xprocessing.ext.modifiers import javax.lang.model.element.Modifier import motif.compiler.KotlinTypeWorkaround.javaToKotlinType -@OptIn(KotlinPoetJavaPoetPreview::class) object XFunSpec { /** Copied from [FunSpec.overriding] and modified to leverage [javaToKotlinType]& XProcessing. */ fun overriding( @@ -50,23 +47,13 @@ object XFunSpec { env.requireType(method.returnType.typeName) } - val builder = overriding(methodElement) - builder.returns(javaToKotlinType(returnType)) - - var i = 0 - val size = builder.parameters.size - val resolvedParameterTypes = method.parameterTypes - while (i < size) { - val parameter = builder.parameters[i] - val type = javaToKotlinType(resolvedParameterTypes[i]) - builder.parameters[i] = parameter.toBuilder(parameter.name, type).build() - i++ - } - - return builder + return overriding(methodElement, method.parameterTypes).returns(javaToKotlinType(returnType)) } - private fun overriding(method: XMethodElement): FunSpec.Builder { + private fun overriding( + method: XMethodElement, + resolvedParameterTypes: List + ): FunSpec.Builder { var modifiers: Set = method.modifiers.toMutableSet() require( Modifier.PRIVATE !in modifiers && @@ -90,9 +77,10 @@ object XFunSpec { .forEach { funBuilder.addTypeVariable(it) } */ - method.parameters.forEach { + method.parameters.forEachIndexed { index, parameter -> funBuilder.addParameter( - ParameterSpec.builder(it.name, it.type.typeName.toKTypeName()).build()) + ParameterSpec.builder(parameter.name, javaToKotlinType(resolvedParameterTypes[index])) + .build()) } if (method.isVarArgs()) { funBuilder.parameters[funBuilder.parameters.lastIndex] = diff --git a/tests/src/main/java/testcases/KT006_reference_self/Child.kt b/tests/src/main/java/testcases/KT006_reference_self/Child.kt new file mode 100644 index 00000000..0ccfeed6 --- /dev/null +++ b/tests/src/main/java/testcases/KT006_reference_self/Child.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package testcases.KT006_reference_self + +@motif.Scope +interface Child { + + @motif.Objects + abstract class Objects : ObjectComponent + +} \ No newline at end of file diff --git a/tests/src/main/java/testcases/KT006_reference_self/Foo.kt b/tests/src/main/java/testcases/KT006_reference_self/Foo.kt new file mode 100644 index 00000000..b68ef420 --- /dev/null +++ b/tests/src/main/java/testcases/KT006_reference_self/Foo.kt @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package testcases.KT006_reference_self + +interface Foo +class Bar : Foo +interface ObjectComponent> { + fun generic(someFoo: T): Foo +} \ No newline at end of file diff --git a/tests/src/main/java/testcases/KT006_reference_self/GRAPH.txt b/tests/src/main/java/testcases/KT006_reference_self/GRAPH.txt new file mode 100644 index 00000000..33bc45a4 --- /dev/null +++ b/tests/src/main/java/testcases/KT006_reference_self/GRAPH.txt @@ -0,0 +1,57 @@ +######################################################################## +# # +# This file is auto-generated by running the Motif compiler tests and # +# serves a as validation of graph correctness. IntelliJ plugin tests # +# also rely on this file to ensure that the plugin graph understanding # +# is equivalent to the compiler's. # +# # +# - Do not edit manually. # +# - Commit changes to source control. # +# - Since this file is autogenerated, code review changes carefully to # +# ensure correctness. # +# # +######################################################################## + + ------- +| Scope | + ------- + + ==== Required ==== + + ==== Provides ==== + + ---- Bar | Objects.bar ---- + [ Required ] + [ Consumed By ] + * Child | Objects.generic(someFoo) + + ---- Scope | implicit ---- + [ Required ] + [ Consumed By ] + + ------- + | Child | + ------- + + ==== Required ==== + + ---- Bar ---- + [ Provided By ] + * Scope | Objects.bar + [ Consumed By ] + * Child | Objects.generic(someFoo) + + ==== Provides ==== + + ---- Child | implicit ---- + [ Required ] + [ Consumed By ] + + ---- Foo | Objects.generic ---- + [ Required ] + Bar + [ Provided By ] + * Scope | Objects.bar + [ Consumed By ] + + diff --git a/tests/src/main/java/testcases/KT006_reference_self/Scope.kt b/tests/src/main/java/testcases/KT006_reference_self/Scope.kt new file mode 100644 index 00000000..6138d23c --- /dev/null +++ b/tests/src/main/java/testcases/KT006_reference_self/Scope.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package testcases.KT006_reference_self + +import motif.Expose + +@motif.Scope +interface Scope { + + fun child(): Child + + @motif.Objects + abstract class Objects { + + @Expose + fun bar(): Bar = Bar() + } +} \ No newline at end of file diff --git a/tests/src/main/java/testcases/KT006_reference_self/Test.java b/tests/src/main/java/testcases/KT006_reference_self/Test.java new file mode 100644 index 00000000..21ed2131 --- /dev/null +++ b/tests/src/main/java/testcases/KT006_reference_self/Test.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package testcases.KT006_reference_self; + +import static com.google.common.truth.Truth.assertThat; + +public class Test { + + public static void run() { + Scope scope = new ScopeImpl(); + assertThat(scope.child()).isNotNull(); + } +}