Skip to content

Commit

Permalink
Internal changes
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 485731743
  • Loading branch information
bcorso authored and Dagger Team committed Nov 3, 2022
1 parent 6866d93 commit ff66535
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 18 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T> => Collection<T> to reflect this.
? rewrapType(binding.contributedType(), TypeNames.COLLECTION, processingEnv)
: binding.contributedType();
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions 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",
],
)
37 changes: 37 additions & 0 deletions 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() {}
}
}

2 changes: 0 additions & 2 deletions javatests/dagger/functional/binds/SimpleBindingModule.java
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions 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 {}
1 change: 0 additions & 1 deletion javatests/dagger/functional/binds/TestComponent.java
Expand Up @@ -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;
Expand Down

0 comments on commit ff66535

Please sign in to comment.