blob: e19abb9d6dd8abbdadf8f4f27d75deeec920cf03 [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
ronshapiroaeec8dc2018-12-03 13:59:03 -080023import com.google.auto.common.MoreTypes;
dstrasburg2e206ec2018-08-21 13:00:27 -070024import com.google.auto.value.AutoValue;
ronshapiroaeec8dc2018-12-03 13:59:03 -080025import com.google.common.base.Equivalence;
dstrasburg2e206ec2018-08-21 13:00:27 -070026import com.google.common.collect.ImmutableList;
27import com.google.common.collect.Maps;
28import com.google.common.collect.Sets;
29import com.squareup.javapoet.MethodSpec;
dstrasburg2e206ec2018-08-21 13:00:27 -070030import java.util.Map;
dstrasburg6d2a7ad2018-09-05 08:05:51 -070031import java.util.Optional;
dstrasburg2e206ec2018-08-21 13:00:27 -070032import java.util.Set;
ronshapiroaeec8dc2018-12-03 13:59:03 -080033import javax.lang.model.type.TypeMirror;
dstrasburg2e206ec2018-08-21 13:00:27 -070034
35/**
36 * A registry for those methods which each wrap a binding whose definition may be modified across
37 * each class in the class hierarchy implementing a subcomponent. Subcomponent implementations are
38 * spread across a class hierarchy when generating ahead-of-time subcomponents. There is one
39 * subcomponent implementation class for each of the subcomponent's ancestor components. An instance
40 * of {@link ModifiableBindingMethod} is associated with a single class in this hierarchy. For a
41 * given subcomponent implementation class we can use the {@link ModifiableBindingMethod}s of its
42 * superclasses to know what binding methods to attempt to modify.
43 */
44final class ModifiableBindingMethods {
cgdecker60a5dde2018-09-07 08:44:34 -070045 private final Map<BindingRequest, ModifiableBindingMethod> methods = Maps.newLinkedHashMap();
46 private final Set<BindingRequest> finalizedMethods = Sets.newHashSet();
dstrasburg2e206ec2018-08-21 13:00:27 -070047
48 /** Register a method encapsulating a modifiable binding. */
49 void addMethod(
ronshapiroaeec8dc2018-12-03 13:59:03 -080050 ModifiableBindingType type,
51 BindingRequest request,
52 TypeMirror returnType,
53 MethodSpec method,
54 boolean finalized) {
dstrasburg6d2a7ad2018-09-05 08:05:51 -070055 checkArgument(type.isModifiable());
dstrasburg6d2a7ad2018-09-05 08:05:51 -070056 if (finalized) {
cgdecker60a5dde2018-09-07 08:44:34 -070057 finalizedMethods.add(request);
dstrasburg6d2a7ad2018-09-05 08:05:51 -070058 }
ronshapiroaeec8dc2018-12-03 13:59:03 -080059 methods.put(
60 request, ModifiableBindingMethod.create(type, request, returnType, method, finalized));
dstrasburg2e206ec2018-08-21 13:00:27 -070061 }
62
dstrasburg6d2a7ad2018-09-05 08:05:51 -070063 /** Returns all {@link ModifiableBindingMethod}s that have not been marked as finalized. */
64 ImmutableList<ModifiableBindingMethod> getNonFinalizedMethods() {
65 return methods.values().stream().filter(m -> !m.finalized()).collect(toImmutableList());
66 }
67
68 /** Returns the {@link ModifiableBindingMethod} for the given binding if present. */
cgdecker60a5dde2018-09-07 08:44:34 -070069 Optional<ModifiableBindingMethod> getMethod(BindingRequest request) {
70 return Optional.ofNullable(methods.get(request));
dstrasburg2e206ec2018-08-21 13:00:27 -070071 }
72
ronshapirofd7f1b32018-12-07 05:42:12 -080073 /** Returns all of the {@link ModifiableBindingMethod}s. */
74 ImmutableList<ModifiableBindingMethod> allMethods() {
75 return ImmutableList.copyOf(methods.values());
76 }
77
dstrasburg2e206ec2018-08-21 13:00:27 -070078 /**
79 * Mark the {@link ModifiableBindingMethod} as having been implemented, thus modifying the
dstrasburg6d2a7ad2018-09-05 08:05:51 -070080 * binding.
dstrasburg2e206ec2018-08-21 13:00:27 -070081 */
82 void methodImplemented(ModifiableBindingMethod method) {
dstrasburg6d2a7ad2018-09-05 08:05:51 -070083 if (method.finalized()) {
dstrasburg2e206ec2018-08-21 13:00:27 -070084 checkState(
cgdecker60a5dde2018-09-07 08:44:34 -070085 finalizedMethods.add(method.request()),
dstrasburg6d2a7ad2018-09-05 08:05:51 -070086 "Implementing and finalizing a modifiable binding method that has been marked as "
cgdecker60a5dde2018-09-07 08:44:34 -070087 + "finalized in the current subcomponent implementation. The binding is for a %s "
dstrasburg6d2a7ad2018-09-05 08:05:51 -070088 + "of type %s.",
cgdecker60a5dde2018-09-07 08:44:34 -070089 method.request(),
dstrasburg2e206ec2018-08-21 13:00:27 -070090 method.type());
dstrasburg2e206ec2018-08-21 13:00:27 -070091 }
92 }
93
94 /** Whether a given binding has been marked as finalized. */
dstrasburg6d2a7ad2018-09-05 08:05:51 -070095 boolean finalized(ModifiableBindingMethod method) {
cgdecker60a5dde2018-09-07 08:44:34 -070096 return finalizedMethods.contains(method.request());
dstrasburg2e206ec2018-08-21 13:00:27 -070097 }
98
99 @AutoValue
100 abstract static class ModifiableBindingMethod {
101 private static ModifiableBindingMethod create(
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700102 ModifiableBindingType type,
cgdecker60a5dde2018-09-07 08:44:34 -0700103 BindingRequest request,
ronshapiroaeec8dc2018-12-03 13:59:03 -0800104 TypeMirror returnType,
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700105 MethodSpec methodSpec,
106 boolean finalized) {
dstrasburg2e206ec2018-08-21 13:00:27 -0700107 return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod(
ronshapiroaeec8dc2018-12-03 13:59:03 -0800108 type, request, MoreTypes.equivalence().wrap(returnType), methodSpec, finalized);
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700109 }
110
111 /** Create a {@ModifiableBindingMethod} representing an implementation of an existing method. */
112 static ModifiableBindingMethod implement(
113 ModifiableBindingMethod unimplementedMethod, MethodSpec methodSpec, boolean finalized) {
114 return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod(
ronshapiroaeec8dc2018-12-03 13:59:03 -0800115 unimplementedMethod.type(),
116 unimplementedMethod.request(),
117 unimplementedMethod.returnTypeWrapper(),
118 methodSpec,
119 finalized);
dstrasburg2e206ec2018-08-21 13:00:27 -0700120 }
121
122 abstract ModifiableBindingType type();
123
cgdecker60a5dde2018-09-07 08:44:34 -0700124 abstract BindingRequest request();
dstrasburg2e206ec2018-08-21 13:00:27 -0700125
ronshapiroaeec8dc2018-12-03 13:59:03 -0800126 final TypeMirror returnType() {
127 return returnTypeWrapper().get();
128 }
129
130 abstract Equivalence.Wrapper<TypeMirror> returnTypeWrapper();
131
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700132 abstract MethodSpec methodSpec();
133
134 abstract boolean finalized();
dstrasburg09adeae2018-09-07 12:11:06 -0700135
136 /** Whether a {@link ModifiableBindingMethod} is for the same binding request. */
137 boolean fulfillsSameRequestAs(ModifiableBindingMethod other) {
138 return request().equals(other.request());
139 }
dstrasburg2e206ec2018-08-21 13:00:27 -0700140 }
dstrasburg2e206ec2018-08-21 13:00:27 -0700141}