Skip to content

Commit d456afc

Browse files
bcorsoDagger Team
authored andcommitted
Change the Binding class hierarchy to reflect BindingKind rather than BindingType.
This change is necessary to decouple the `BindingType` from the binding class hierarchy, which currently prevents us from creating a binding without already having access to its dependencies. RELNOTES=N/A PiperOrigin-RevId: 649081259
1 parent 537acb9 commit d456afc

File tree

65 files changed

+1868
-785
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1868
-785
lines changed

java/dagger/internal/codegen/DelegateComponentProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
import dagger.internal.codegen.base.SourceFileHjarGenerator;
3636
import dagger.internal.codegen.binding.BindingGraphFactory;
3737
import dagger.internal.codegen.binding.ComponentDescriptor;
38+
import dagger.internal.codegen.binding.ContributionBinding;
3839
import dagger.internal.codegen.binding.InjectBindingRegistry;
3940
import dagger.internal.codegen.binding.MembersInjectionBinding;
4041
import dagger.internal.codegen.binding.ModuleDescriptor;
4142
import dagger.internal.codegen.binding.MonitoringModules;
4243
import dagger.internal.codegen.binding.ProductionBinding;
43-
import dagger.internal.codegen.binding.ProvisionBinding;
4444
import dagger.internal.codegen.bindinggraphvalidation.BindingGraphValidationModule;
4545
import dagger.internal.codegen.compileroption.CompilerOptions;
4646
import dagger.internal.codegen.componentgenerator.ComponentGeneratorModule;
@@ -74,7 +74,7 @@ final class DelegateComponentProcessor {
7474
new XProcessingEnvConfig.Builder().disableAnnotatedElementValidation(true).build();
7575

7676
@Inject InjectBindingRegistry injectBindingRegistry;
77-
@Inject SourceFileGenerator<ProvisionBinding> factoryGenerator;
77+
@Inject SourceFileGenerator<ContributionBinding> factoryGenerator;
7878
@Inject SourceFileGenerator<MembersInjectionBinding> membersInjectorGenerator;
7979
@Inject ImmutableList<XProcessingStep> processingSteps;
8080
@Inject ValidationBindingGraphPlugins validationBindingGraphPlugins;
@@ -204,7 +204,7 @@ interface ProcessingRoundCacheModule {
204204
@Module
205205
interface SourceFileGeneratorsModule {
206206
@Provides
207-
static SourceFileGenerator<ProvisionBinding> factoryGenerator(
207+
static SourceFileGenerator<ContributionBinding> factoryGenerator(
208208
FactoryGenerator generator,
209209
CompilerOptions compilerOptions,
210210
XProcessingEnv processingEnv) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (C) 2024 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.internal.codegen.binding;
18+
19+
import com.google.auto.value.AutoValue;
20+
import com.google.auto.value.extension.memoized.Memoized;
21+
import com.google.common.collect.ImmutableSet;
22+
import com.google.errorprone.annotations.CheckReturnValue;
23+
import dagger.internal.codegen.base.ContributionType;
24+
import dagger.internal.codegen.model.BindingKind;
25+
import dagger.internal.codegen.model.DependencyRequest;
26+
import dagger.internal.codegen.model.Key;
27+
import dagger.internal.codegen.model.RequestKind;
28+
29+
/** A binding for a {@link BindingKind#ASSISTED_FACTORY}. */
30+
@CheckReturnValue
31+
@AutoValue
32+
public abstract class AssistedFactoryBinding extends ContributionBinding {
33+
@Override
34+
public BindingKind kind() {
35+
return BindingKind.ASSISTED_FACTORY;
36+
}
37+
38+
@Override
39+
public BindingType bindingType() {
40+
return BindingType.PROVISION;
41+
}
42+
43+
@Override
44+
public ContributionType contributionType() {
45+
return ContributionType.UNIQUE;
46+
}
47+
48+
@Override
49+
public Nullability nullability() {
50+
return Nullability.NOT_NULLABLE;
51+
}
52+
53+
@Override
54+
@Memoized
55+
public ImmutableSet<DependencyRequest> dependencies() {
56+
return ImmutableSet.of(
57+
DependencyRequest.builder()
58+
.key(assistedInjectKey())
59+
.kind(RequestKind.PROVIDER)
60+
.build());
61+
}
62+
63+
/** Returns the key for the associated {@code @AssistedInject} binding. */
64+
public abstract Key assistedInjectKey();
65+
66+
@Override
67+
public abstract Builder toBuilder();
68+
69+
@Memoized
70+
@Override
71+
public abstract int hashCode();
72+
73+
// TODO(ronshapiro,dpb): simplify the equality semantics
74+
@Override
75+
public abstract boolean equals(Object obj);
76+
77+
static Builder builder() {
78+
return new AutoValue_AssistedFactoryBinding.Builder();
79+
}
80+
81+
/** A {@link AssistedFactoryBinding} builder. */
82+
@AutoValue.Builder
83+
abstract static class Builder
84+
extends ContributionBinding.Builder<AssistedFactoryBinding, Builder> {
85+
abstract Builder assistedInjectKey(Key assistedInjectKey);
86+
}
87+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (C) 2024 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.internal.codegen.binding;
18+
19+
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
20+
21+
import com.google.auto.value.AutoValue;
22+
import com.google.auto.value.extension.memoized.Memoized;
23+
import com.google.common.collect.ImmutableSet;
24+
import com.google.common.collect.ImmutableSortedSet;
25+
import com.google.errorprone.annotations.CheckReturnValue;
26+
import dagger.internal.codegen.base.ContributionType;
27+
import dagger.internal.codegen.binding.MembersInjectionBinding.InjectionSite;
28+
import dagger.internal.codegen.model.BindingKind;
29+
import dagger.internal.codegen.model.DependencyRequest;
30+
31+
/** A binding for a {@link BindingKind#ASSISTED_INJECTION}. */
32+
@CheckReturnValue
33+
@AutoValue
34+
public abstract class AssistedInjectionBinding extends ContributionBinding {
35+
@Override
36+
public BindingKind kind() {
37+
return BindingKind.ASSISTED_INJECTION;
38+
}
39+
40+
@Override
41+
public BindingType bindingType() {
42+
return BindingType.PROVISION;
43+
}
44+
45+
@Override
46+
public ContributionType contributionType() {
47+
return ContributionType.UNIQUE;
48+
}
49+
50+
@Override
51+
public Nullability nullability() {
52+
return Nullability.NOT_NULLABLE;
53+
}
54+
55+
/** Dependencies necessary to invoke the {@code @Inject} annotated constructor. */
56+
public abstract ImmutableSet<DependencyRequest> constructorDependencies();
57+
58+
/** {@link InjectionSite}s for all {@code @Inject} members. */
59+
public abstract ImmutableSortedSet<InjectionSite> injectionSites();
60+
61+
@Override
62+
@Memoized
63+
public ImmutableSet<DependencyRequest> dependencies() {
64+
return ImmutableSet.<DependencyRequest>builder()
65+
.addAll(constructorDependencies())
66+
.addAll(
67+
injectionSites().stream()
68+
.flatMap(i -> i.dependencies().stream())
69+
.collect(toImmutableSet()))
70+
.build();
71+
}
72+
73+
@Override
74+
public abstract Builder toBuilder();
75+
76+
@Memoized
77+
@Override
78+
public abstract int hashCode();
79+
80+
// TODO(ronshapiro,dpb): simplify the equality semantics
81+
@Override
82+
public abstract boolean equals(Object obj);
83+
84+
static Builder builder() {
85+
return new AutoValue_AssistedInjectionBinding.Builder();
86+
}
87+
88+
/** A {@link AssistedInjectionBinding} builder. */
89+
@AutoValue.Builder
90+
abstract static class Builder
91+
extends ContributionBinding.Builder<AssistedInjectionBinding, Builder> {
92+
abstract Builder constructorDependencies(Iterable<DependencyRequest> constructorDependencies);
93+
94+
abstract Builder injectionSites(ImmutableSortedSet<InjectionSite> injectionSites);
95+
}
96+
}

java/dagger/internal/codegen/binding/Binding.java

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616

1717
package dagger.internal.codegen.binding;
1818

19-
import static com.google.common.base.Suppliers.memoize;
20-
import static dagger.internal.codegen.xprocessing.XElements.isAbstract;
21-
import static dagger.internal.codegen.xprocessing.XElements.isStatic;
22-
23-
import com.google.common.base.Supplier;
2419
import com.google.common.collect.ImmutableSet;
25-
import com.google.common.collect.Sets;
2620
import dagger.internal.codegen.model.BindingKind;
2721
import dagger.internal.codegen.model.DependencyRequest;
2822
import dagger.internal.codegen.model.Scope;
@@ -40,12 +34,7 @@ public abstract class Binding extends BindingDeclaration {
4034
* Returns {@code true} if using this binding requires an instance of the {@link
4135
* #contributingModule()}.
4236
*/
43-
public boolean requiresModuleInstance() {
44-
return contributingModule().isPresent()
45-
&& bindingElement().isPresent()
46-
&& !isAbstract(bindingElement().get())
47-
&& !isStatic(bindingElement().get());
48-
}
37+
public abstract boolean requiresModuleInstance();
4938

5039
/**
5140
* Returns {@code true} if this binding may provide {@code null} instead of an instance of {@link
@@ -65,38 +54,8 @@ public final FrameworkType frameworkType() {
6554
return FrameworkType.forBindingType(bindingType());
6655
}
6756

68-
/**
69-
* The explicit set of {@link DependencyRequest dependencies} required to satisfy this binding as
70-
* defined by the user-defined injection sites.
71-
*/
72-
public abstract ImmutableSet<DependencyRequest> explicitDependencies();
73-
74-
/**
75-
* The set of {@link DependencyRequest dependencies} that are added by the framework rather than a
76-
* user-defined injection site. This returns an unmodifiable set.
77-
*/
78-
public ImmutableSet<DependencyRequest> implicitDependencies() {
79-
return ImmutableSet.of();
80-
}
81-
82-
private final Supplier<ImmutableSet<DependencyRequest>> dependencies =
83-
memoize(
84-
() -> {
85-
ImmutableSet<DependencyRequest> implicitDependencies = implicitDependencies();
86-
return ImmutableSet.copyOf(
87-
implicitDependencies.isEmpty()
88-
? explicitDependencies()
89-
: Sets.union(implicitDependencies, explicitDependencies()));
90-
});
91-
92-
/**
93-
* The set of {@link DependencyRequest dependencies} required to satisfy this binding. This is the
94-
* union of {@link #explicitDependencies()} and {@link #implicitDependencies()}. This returns an
95-
* unmodifiable set.
96-
*/
97-
public final ImmutableSet<DependencyRequest> dependencies() {
98-
return dependencies.get();
99-
}
57+
/** The set of {@link DependencyRequest dependencies} required to satisfy this binding. */
58+
public abstract ImmutableSet<DependencyRequest> dependencies();
10059

10160
/**
10261
* If this binding's key's type parameters are different from those of the {@link
@@ -105,7 +64,6 @@ public final ImmutableSet<DependencyRequest> dependencies() {
10564
*/
10665
public abstract Optional<? extends Binding> unresolved();
10766

108-
public Optional<Scope> scope() {
109-
return Optional.empty();
110-
}
67+
/** Returns the optional scope used on the binding. */
68+
public abstract Optional<Scope> scope();
11169
}

0 commit comments

Comments
 (0)