blob: d83a5fc9b9bfc9039683e507221450043dd43679 [file] [log] [blame]
dstrasburg2e206ec2018-08-21 13:00:27 -07001/*
2 * Copyright (C) 2018 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
17package dagger.internal.codegen;
18
dstrasburg6d2a7ad2018-09-05 08:05:51 -070019import static com.google.common.base.Preconditions.checkArgument;
dstrasburg2e206ec2018-08-21 13:00:27 -070020import static com.google.common.base.Preconditions.checkState;
dstrasburg6d2a7ad2018-09-05 08:05:51 -070021import static dagger.internal.codegen.DaggerStreams.toImmutableList;
dstrasburg2e206ec2018-08-21 13:00:27 -070022
23import com.google.auto.value.AutoValue;
24import com.google.common.collect.ImmutableList;
25import com.google.common.collect.Maps;
26import com.google.common.collect.Sets;
27import com.squareup.javapoet.MethodSpec;
dstrasburg2e206ec2018-08-21 13:00:27 -070028import java.util.Map;
dstrasburg6d2a7ad2018-09-05 08:05:51 -070029import java.util.Optional;
dstrasburg2e206ec2018-08-21 13:00:27 -070030import java.util.Set;
31
32/**
33 * A registry for those methods which each wrap a binding whose definition may be modified across
34 * each class in the class hierarchy implementing a subcomponent. Subcomponent implementations are
35 * spread across a class hierarchy when generating ahead-of-time subcomponents. There is one
36 * subcomponent implementation class for each of the subcomponent's ancestor components. An instance
37 * of {@link ModifiableBindingMethod} is associated with a single class in this hierarchy. For a
38 * given subcomponent implementation class we can use the {@link ModifiableBindingMethod}s of its
39 * superclasses to know what binding methods to attempt to modify.
40 */
41final class ModifiableBindingMethods {
cgdecker60a5dde2018-09-07 08:44:34 -070042 private final Map<BindingRequest, ModifiableBindingMethod> methods = Maps.newLinkedHashMap();
43 private final Set<BindingRequest> finalizedMethods = Sets.newHashSet();
dstrasburg2e206ec2018-08-21 13:00:27 -070044
45 /** Register a method encapsulating a modifiable binding. */
46 void addMethod(
cgdecker60a5dde2018-09-07 08:44:34 -070047 ModifiableBindingType type, BindingRequest request, MethodSpec method, boolean finalized) {
dstrasburg6d2a7ad2018-09-05 08:05:51 -070048 checkArgument(type.isModifiable());
dstrasburg6d2a7ad2018-09-05 08:05:51 -070049 if (finalized) {
cgdecker60a5dde2018-09-07 08:44:34 -070050 finalizedMethods.add(request);
dstrasburg6d2a7ad2018-09-05 08:05:51 -070051 }
cgdecker60a5dde2018-09-07 08:44:34 -070052 methods.put(request, ModifiableBindingMethod.create(type, request, method, finalized));
dstrasburg2e206ec2018-08-21 13:00:27 -070053 }
54
dstrasburg6d2a7ad2018-09-05 08:05:51 -070055 /** Returns all {@link ModifiableBindingMethod}s that have not been marked as finalized. */
56 ImmutableList<ModifiableBindingMethod> getNonFinalizedMethods() {
57 return methods.values().stream().filter(m -> !m.finalized()).collect(toImmutableList());
58 }
59
60 /** Returns the {@link ModifiableBindingMethod} for the given binding if present. */
cgdecker60a5dde2018-09-07 08:44:34 -070061 Optional<ModifiableBindingMethod> getMethod(BindingRequest request) {
62 return Optional.ofNullable(methods.get(request));
dstrasburg2e206ec2018-08-21 13:00:27 -070063 }
64
65 /**
66 * Mark the {@link ModifiableBindingMethod} as having been implemented, thus modifying the
dstrasburg6d2a7ad2018-09-05 08:05:51 -070067 * binding.
dstrasburg2e206ec2018-08-21 13:00:27 -070068 */
69 void methodImplemented(ModifiableBindingMethod method) {
dstrasburg6d2a7ad2018-09-05 08:05:51 -070070 if (method.finalized()) {
dstrasburg2e206ec2018-08-21 13:00:27 -070071 checkState(
cgdecker60a5dde2018-09-07 08:44:34 -070072 finalizedMethods.add(method.request()),
dstrasburg6d2a7ad2018-09-05 08:05:51 -070073 "Implementing and finalizing a modifiable binding method that has been marked as "
cgdecker60a5dde2018-09-07 08:44:34 -070074 + "finalized in the current subcomponent implementation. The binding is for a %s "
dstrasburg6d2a7ad2018-09-05 08:05:51 -070075 + "of type %s.",
cgdecker60a5dde2018-09-07 08:44:34 -070076 method.request(),
dstrasburg2e206ec2018-08-21 13:00:27 -070077 method.type());
dstrasburg2e206ec2018-08-21 13:00:27 -070078 }
79 }
80
81 /** Whether a given binding has been marked as finalized. */
dstrasburg6d2a7ad2018-09-05 08:05:51 -070082 boolean finalized(ModifiableBindingMethod method) {
cgdecker60a5dde2018-09-07 08:44:34 -070083 return finalizedMethods.contains(method.request());
dstrasburg2e206ec2018-08-21 13:00:27 -070084 }
85
86 @AutoValue
87 abstract static class ModifiableBindingMethod {
88 private static ModifiableBindingMethod create(
dstrasburg6d2a7ad2018-09-05 08:05:51 -070089 ModifiableBindingType type,
cgdecker60a5dde2018-09-07 08:44:34 -070090 BindingRequest request,
dstrasburg6d2a7ad2018-09-05 08:05:51 -070091 MethodSpec methodSpec,
92 boolean finalized) {
dstrasburg2e206ec2018-08-21 13:00:27 -070093 return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod(
cgdecker60a5dde2018-09-07 08:44:34 -070094 type, request, methodSpec, finalized);
dstrasburg6d2a7ad2018-09-05 08:05:51 -070095 }
96
97 /** Create a {@ModifiableBindingMethod} representing an implementation of an existing method. */
98 static ModifiableBindingMethod implement(
99 ModifiableBindingMethod unimplementedMethod, MethodSpec methodSpec, boolean finalized) {
100 return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod(
cgdecker60a5dde2018-09-07 08:44:34 -0700101 unimplementedMethod.type(), unimplementedMethod.request(), methodSpec, finalized);
dstrasburg2e206ec2018-08-21 13:00:27 -0700102 }
103
104 abstract ModifiableBindingType type();
105
cgdecker60a5dde2018-09-07 08:44:34 -0700106 abstract BindingRequest request();
dstrasburg2e206ec2018-08-21 13:00:27 -0700107
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700108 abstract MethodSpec methodSpec();
109
110 abstract boolean finalized();
dstrasburg09adeae2018-09-07 12:11:06 -0700111
112 /** Whether a {@link ModifiableBindingMethod} is for the same binding request. */
113 boolean fulfillsSameRequestAs(ModifiableBindingMethod other) {
114 return request().equals(other.request());
115 }
dstrasburg2e206ec2018-08-21 13:00:27 -0700116 }
dstrasburg2e206ec2018-08-21 13:00:27 -0700117}