From ff6653582743ddeb38d59aa9b4877187cfbb2a79 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Wed, 2 Nov 2022 16:59:25 -0700 Subject: [PATCH] Internal changes RELNOTES=N/A PiperOrigin-RevId: 485731743 --- ...chingProviderDependencyRepresentation.java | 22 ++++------ .../binds/AccessesExposedComponent.java | 2 +- javatests/dagger/functional/binds/BUILD | 44 +++++++++++++++++++ .../dagger/functional/binds/NeedsFactory.java | 37 ++++++++++++++++ .../functional/binds/SimpleBindingModule.java | 2 - .../functional/binds/SomeQualifier.java | 28 ++++++++++++ .../functional/binds/TestComponent.java | 1 - 7 files changed, 118 insertions(+), 18 deletions(-) create mode 100644 javatests/dagger/functional/binds/BUILD create mode 100644 javatests/dagger/functional/binds/NeedsFactory.java create mode 100644 javatests/dagger/functional/binds/SomeQualifier.java diff --git a/java/dagger/internal/codegen/writing/ExperimentalSwitchingProviderDependencyRepresentation.java b/java/dagger/internal/codegen/writing/ExperimentalSwitchingProviderDependencyRepresentation.java index e50f2d72262..19de34549ba 100644 --- a/java/dagger/internal/codegen/writing/ExperimentalSwitchingProviderDependencyRepresentation.java +++ b/java/dagger/internal/codegen/writing/ExperimentalSwitchingProviderDependencyRepresentation.java @@ -16,11 +16,10 @@ package dagger.internal.codegen.writing; -import static androidx.room.compiler.processing.compat.XConverters.toJavac; -import static androidx.room.compiler.processing.compat.XConverters.toXProcessing; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; import static dagger.internal.codegen.langmodel.Accessibility.isRawTypeAccessible; import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom; +import static dagger.internal.codegen.xprocessing.XProcessingEnvs.rewrapType; import androidx.room.compiler.processing.XProcessingEnv; import androidx.room.compiler.processing.XType; @@ -62,7 +61,10 @@ final class ExperimentalSwitchingProviderDependencyRepresentation { this.bindsTypeChecker = bindsTypeChecker; this.type = isDelegateSetValuesBinding() - ? erasure(processingEnv.requireType(TypeNames.COLLECTION)) + // For convience we allow @Binds @ElementsIntoSet from Collection => Set so that List + // can be contributed without converting to a Set first. Thus, here we rewrap the + // contributed type from Set => Collection to reflect this. + ? rewrapType(binding.contributedType(), TypeNames.COLLECTION, processingEnv) : binding.contributedType(); } @@ -82,8 +84,8 @@ Expression getDependencyExpression(RequestKind requestKind, ProvisionBinding req if (usesExplicitTypeCast(expression, requestKind)) { return expression.castTo(type); } - if (usesErasedTypeCast(requestKind)) { - return expression.castTo(erasure(type)); + if (usesRawTypeCast(requestKind)) { + return expression.castTo(type.getRawType()); } return expression; } @@ -111,21 +113,13 @@ private boolean usesExplicitTypeCast(Expression expression, RequestKind requestK && isTypeAccessibleFrom(type, shardImplementation.name().packageName()); } - private boolean usesErasedTypeCast(RequestKind requestKind) { + private boolean usesRawTypeCast(RequestKind requestKind) { // If a type has inaccessible type arguments, then cast to raw type. return requestKind.equals(RequestKind.INSTANCE) && !isTypeAccessibleFrom(type, shardImplementation.name().packageName()) && isRawTypeAccessible(type, shardImplementation.name().packageName()); } - // TODO(bcorso): This is the last usage of erasure that needs to be cleaned up. Currently, it's - // only used for the experimental mode, so leaving it for now. - private XType erasure(XType type) { - return toXProcessing( - toJavac(processingEnv).getTypeUtils().erasure(toJavac(type)), // ALLOW_TYPES_ELEMENTS - processingEnv); - } - @AssistedFactory static interface Factory { ExperimentalSwitchingProviderDependencyRepresentation create(ProvisionBinding binding); diff --git a/javatests/dagger/functional/binds/AccessesExposedComponent.java b/javatests/dagger/functional/binds/AccessesExposedComponent.java index 075ae960557..596f653c87c 100644 --- a/javatests/dagger/functional/binds/AccessesExposedComponent.java +++ b/javatests/dagger/functional/binds/AccessesExposedComponent.java @@ -29,7 +29,7 @@ * accessible from the component, but the left-hand-side is. If the right-hand-side is represented * as a Provider (e.g. because it is scoped), then the raw {@code Provider.get()} will return {@link * Object}, which must be downcasted to the type accessible from the component. See {@code - * instanceRequiresCast()} in {@link dagger.internal.codegen.DelegateRequestRepresentation}. + * instanceRequiresCast()} in {@code DelegateRequestRepresentation}. */ @Singleton @Component(modules = ExposedModule.class) diff --git a/javatests/dagger/functional/binds/BUILD b/javatests/dagger/functional/binds/BUILD new file mode 100644 index 00000000000..a55d67b4fa8 --- /dev/null +++ b/javatests/dagger/functional/binds/BUILD @@ -0,0 +1,44 @@ +# Copyright (C) 2022 The Dagger Authors. +# +# 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. + +# Description: +# Functional tests for Dagger Producers + +load( + "//:build_defs.bzl", + "DOCLINT_HTML_AND_SYNTAX", + "DOCLINT_REFERENCES", + "JAVA_RELEASE_MIN", +) +load("//:test_defs.bzl", "GenJavaTests") + +package(default_visibility = ["//:src"]) + +GenJavaTests( + name = "binds", + srcs = glob(["**/*.java"]), + javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES, + lib_javacopts = JAVA_RELEASE_MIN, + deps = [ + "//:dagger_with_compiler", + "//third_party/java/auto:factory", + "//third_party/java/guava/base", + "//third_party/java/guava/collect", + "//third_party/java/guava/util/concurrent", + "//third_party/java/jsr305_annotations", + "//third_party/java/jsr330_inject", + "//third_party/java/junit", + "//third_party/java/truth", + ], +) diff --git a/javatests/dagger/functional/binds/NeedsFactory.java b/javatests/dagger/functional/binds/NeedsFactory.java new file mode 100644 index 00000000000..6000d62efca --- /dev/null +++ b/javatests/dagger/functional/binds/NeedsFactory.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2015 The Dagger Authors. + * + * 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 dagger.functional.binds; + +import com.google.auto.factory.AutoFactory; +import javax.inject.Inject; + +public class NeedsFactory { + @Inject + NeedsFactory( + @SuppressWarnings("unused") NeedsFactory_SomethingFactory somethingFactory, + @SuppressWarnings("unused") SomethingFactoryImpl somethingFactoryImpl) {} + + public interface SomethingFactory {} + + @AutoFactory(implementing = SomethingFactory.class, allowSubclasses = true) + static class Something {} + + public static final class SomethingFactoryImpl extends NeedsFactory_SomethingFactory { + @Inject SomethingFactoryImpl() {} + } +} + diff --git a/javatests/dagger/functional/binds/SimpleBindingModule.java b/javatests/dagger/functional/binds/SimpleBindingModule.java index e8c3ca66f3c..47a23e8abe2 100644 --- a/javatests/dagger/functional/binds/SimpleBindingModule.java +++ b/javatests/dagger/functional/binds/SimpleBindingModule.java @@ -20,8 +20,6 @@ import dagger.Module; import dagger.Provides; import dagger.Reusable; -import dagger.functional.NeedsFactory; -import dagger.functional.SomeQualifier; import dagger.multibindings.ElementsIntoSet; import dagger.multibindings.IntKey; import dagger.multibindings.IntoMap; diff --git a/javatests/dagger/functional/binds/SomeQualifier.java b/javatests/dagger/functional/binds/SomeQualifier.java new file mode 100644 index 00000000000..f1c4540678a --- /dev/null +++ b/javatests/dagger/functional/binds/SomeQualifier.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 The Dagger Authors. + * + * 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 dagger.functional.binds; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import javax.inject.Qualifier; + +@Documented +@Retention(RUNTIME) +@Qualifier +public @interface SomeQualifier {} diff --git a/javatests/dagger/functional/binds/TestComponent.java b/javatests/dagger/functional/binds/TestComponent.java index 5e459be2ccf..4a6d09d5faa 100644 --- a/javatests/dagger/functional/binds/TestComponent.java +++ b/javatests/dagger/functional/binds/TestComponent.java @@ -17,7 +17,6 @@ package dagger.functional.binds; import dagger.Component; -import dagger.functional.SomeQualifier; import dagger.functional.binds.subpackage.ExposedModule; import java.util.Map; import java.util.Set;