From cb1df1f2202576dc0ecbe3da6d64693f23a3b541 Mon Sep 17 00:00:00 2001 From: Leland Takamine Date: Wed, 1 May 2019 21:21:18 -0700 Subject: [PATCH] Regression fix: Support for binding to interface types with binds factory methods. --- .../kotlin/motif/ast/compiler/CompilerType.kt | 9 ++++-- .../testcases/T051_binds_interface/A.java | 18 +++++++++++ .../testcases/T051_binds_interface/B.java | 18 +++++++++++ .../testcases/T051_binds_interface/Scope.java | 32 +++++++++++++++++++ .../testcases/T051_binds_interface/Test.java | 26 +++++++++++++++ .../T052_binds_interface_generic/A.java | 18 +++++++++++ .../T052_binds_interface_generic/B.java | 18 +++++++++++ .../T052_binds_interface_generic/Scope.java | 32 +++++++++++++++++++ .../T052_binds_interface_generic/Test.java | 26 +++++++++++++++ 9 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 tests/src/main/java/testcases/T051_binds_interface/A.java create mode 100644 tests/src/main/java/testcases/T051_binds_interface/B.java create mode 100644 tests/src/main/java/testcases/T051_binds_interface/Scope.java create mode 100644 tests/src/main/java/testcases/T051_binds_interface/Test.java create mode 100644 tests/src/main/java/testcases/T052_binds_interface_generic/A.java create mode 100644 tests/src/main/java/testcases/T052_binds_interface_generic/B.java create mode 100644 tests/src/main/java/testcases/T052_binds_interface_generic/Scope.java create mode 100644 tests/src/main/java/testcases/T052_binds_interface_generic/Test.java diff --git a/compiler/ast/src/main/kotlin/motif/ast/compiler/CompilerType.kt b/compiler/ast/src/main/kotlin/motif/ast/compiler/CompilerType.kt index 51eb8955..fb5b7730 100644 --- a/compiler/ast/src/main/kotlin/motif/ast/compiler/CompilerType.kt +++ b/compiler/ast/src/main/kotlin/motif/ast/compiler/CompilerType.kt @@ -81,9 +81,12 @@ class CompilerType( return type } - val superType = MoreTypes.nonObjectSuperclass(env.typeUtils, env.elementUtils, type).orNull() ?: return null - - return getMatchingSuperType(baseType, superType) + return env.typeUtils.directSupertypes(type) + .asSequence() + .mapNotNull { superType -> + getMatchingSuperType(baseType, superType as DeclaredType) + } + .firstOrNull() } override fun equals(other: Any?): Boolean { diff --git a/tests/src/main/java/testcases/T051_binds_interface/A.java b/tests/src/main/java/testcases/T051_binds_interface/A.java new file mode 100644 index 00000000..d33d750f --- /dev/null +++ b/tests/src/main/java/testcases/T051_binds_interface/A.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2018 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.T051_binds_interface; + +public class A implements B {} diff --git a/tests/src/main/java/testcases/T051_binds_interface/B.java b/tests/src/main/java/testcases/T051_binds_interface/B.java new file mode 100644 index 00000000..5dcd3b2e --- /dev/null +++ b/tests/src/main/java/testcases/T051_binds_interface/B.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2018 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.T051_binds_interface; + +interface B {} diff --git a/tests/src/main/java/testcases/T051_binds_interface/Scope.java b/tests/src/main/java/testcases/T051_binds_interface/Scope.java new file mode 100644 index 00000000..18ece7b6 --- /dev/null +++ b/tests/src/main/java/testcases/T051_binds_interface/Scope.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018 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.T051_binds_interface; + +@motif.Scope +public interface Scope { + + B b(); + + @motif.Objects + abstract class Objects { + abstract A a(); + + abstract B b(A a); + } + + @motif.Dependencies + interface Dependencies {} +} diff --git a/tests/src/main/java/testcases/T051_binds_interface/Test.java b/tests/src/main/java/testcases/T051_binds_interface/Test.java new file mode 100644 index 00000000..cdc57f67 --- /dev/null +++ b/tests/src/main/java/testcases/T051_binds_interface/Test.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2018 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.T051_binds_interface; + +import static com.google.common.truth.Truth.assertThat; + +public class Test { + + public static void run() { + Scope scope = new ScopeImpl(); + assertThat(scope.b()).isInstanceOf(A.class); + } +} diff --git a/tests/src/main/java/testcases/T052_binds_interface_generic/A.java b/tests/src/main/java/testcases/T052_binds_interface_generic/A.java new file mode 100644 index 00000000..2bb84274 --- /dev/null +++ b/tests/src/main/java/testcases/T052_binds_interface_generic/A.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2018 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.T052_binds_interface_generic; + +public class A implements B {} diff --git a/tests/src/main/java/testcases/T052_binds_interface_generic/B.java b/tests/src/main/java/testcases/T052_binds_interface_generic/B.java new file mode 100644 index 00000000..84409483 --- /dev/null +++ b/tests/src/main/java/testcases/T052_binds_interface_generic/B.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2018 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.T052_binds_interface_generic; + +interface B {} diff --git a/tests/src/main/java/testcases/T052_binds_interface_generic/Scope.java b/tests/src/main/java/testcases/T052_binds_interface_generic/Scope.java new file mode 100644 index 00000000..3958d021 --- /dev/null +++ b/tests/src/main/java/testcases/T052_binds_interface_generic/Scope.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018 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.T052_binds_interface_generic; + +@motif.Scope +public interface Scope { + + B b(); + + @motif.Objects + abstract class Objects { + abstract A a(); + + abstract B b(A a); + } + + @motif.Dependencies + interface Dependencies {} +} diff --git a/tests/src/main/java/testcases/T052_binds_interface_generic/Test.java b/tests/src/main/java/testcases/T052_binds_interface_generic/Test.java new file mode 100644 index 00000000..55d95791 --- /dev/null +++ b/tests/src/main/java/testcases/T052_binds_interface_generic/Test.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2018 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.T052_binds_interface_generic; + +import static com.google.common.truth.Truth.assertThat; + +public class Test { + + public static void run() { + Scope scope = new ScopeImpl(); + assertThat(scope.b()).isInstanceOf(A.class); + } +}