Skip to content

Commit

Permalink
pass in resolved type info
Browse files Browse the repository at this point in the history
This avoids the infinite poetry in
uber#255
  • Loading branch information
davissuber committed Oct 27, 2023
1 parent 29d56d8 commit b2a75d4
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 20 deletions.
28 changes: 8 additions & 20 deletions compiler/src/main/kotlin/motif/compiler/XFunSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<XType>
): FunSpec.Builder {
var modifiers: Set<Modifier> = method.modifiers.toMutableSet()
require(
Modifier.PRIVATE !in modifiers &&
Expand All @@ -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] =
Expand Down
24 changes: 24 additions & 0 deletions tests/src/main/java/testcases/KT006_reference_self/Child.kt
Original file line number Diff line number Diff line change
@@ -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<Bar>

}
22 changes: 22 additions & 0 deletions tests/src/main/java/testcases/KT006_reference_self/Foo.kt
Original file line number Diff line number Diff line change
@@ -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<T>
class Bar : Foo<Bar>
interface ObjectComponent<T: Foo<T>> {
fun generic(someFoo: T): Foo<T>
}
57 changes: 57 additions & 0 deletions tests/src/main/java/testcases/KT006_reference_self/GRAPH.txt
Original file line number Diff line number Diff line change
@@ -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<Bar> | Objects.generic ----
[ Required ]
Bar
[ Provided By ]
* Scope | Objects.bar
[ Consumed By ]


31 changes: 31 additions & 0 deletions tests/src/main/java/testcases/KT006_reference_self/Scope.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
26 changes: 26 additions & 0 deletions tests/src/main/java/testcases/KT006_reference_self/Test.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit b2a75d4

Please sign in to comment.