blob: 6183ae4d98041f140d54b0aa625a6abc8abac4c0 [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
73 /**
74 * Mark the {@link ModifiableBindingMethod} as having been implemented, thus modifying the
dstrasburg6d2a7ad2018-09-05 08:05:51 -070075 * binding.
dstrasburg2e206ec2018-08-21 13:00:27 -070076 */
77 void methodImplemented(ModifiableBindingMethod method) {
dstrasburg6d2a7ad2018-09-05 08:05:51 -070078 if (method.finalized()) {
dstrasburg2e206ec2018-08-21 13:00:27 -070079 checkState(
cgdecker60a5dde2018-09-07 08:44:34 -070080 finalizedMethods.add(method.request()),
dstrasburg6d2a7ad2018-09-05 08:05:51 -070081 "Implementing and finalizing a modifiable binding method that has been marked as "
cgdecker60a5dde2018-09-07 08:44:34 -070082 + "finalized in the current subcomponent implementation. The binding is for a %s "
dstrasburg6d2a7ad2018-09-05 08:05:51 -070083 + "of type %s.",
cgdecker60a5dde2018-09-07 08:44:34 -070084 method.request(),
dstrasburg2e206ec2018-08-21 13:00:27 -070085 method.type());
dstrasburg2e206ec2018-08-21 13:00:27 -070086 }
87 }
88
89 /** Whether a given binding has been marked as finalized. */
dstrasburg6d2a7ad2018-09-05 08:05:51 -070090 boolean finalized(ModifiableBindingMethod method) {
cgdecker60a5dde2018-09-07 08:44:34 -070091 return finalizedMethods.contains(method.request());
dstrasburg2e206ec2018-08-21 13:00:27 -070092 }
93
94 @AutoValue
95 abstract static class ModifiableBindingMethod {
96 private static ModifiableBindingMethod create(
dstrasburg6d2a7ad2018-09-05 08:05:51 -070097 ModifiableBindingType type,
cgdecker60a5dde2018-09-07 08:44:34 -070098 BindingRequest request,
ronshapiroaeec8dc2018-12-03 13:59:03 -080099 TypeMirror returnType,
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700100 MethodSpec methodSpec,
101 boolean finalized) {
dstrasburg2e206ec2018-08-21 13:00:27 -0700102 return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod(
ronshapiroaeec8dc2018-12-03 13:59:03 -0800103 type, request, MoreTypes.equivalence().wrap(returnType), methodSpec, finalized);
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700104 }
105
106 /** Create a {@ModifiableBindingMethod} representing an implementation of an existing method. */
107 static ModifiableBindingMethod implement(
108 ModifiableBindingMethod unimplementedMethod, MethodSpec methodSpec, boolean finalized) {
109 return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod(
ronshapiroaeec8dc2018-12-03 13:59:03 -0800110 unimplementedMethod.type(),
111 unimplementedMethod.request(),
112 unimplementedMethod.returnTypeWrapper(),
113 methodSpec,
114 finalized);
dstrasburg2e206ec2018-08-21 13:00:27 -0700115 }
116
117 abstract ModifiableBindingType type();
118
cgdecker60a5dde2018-09-07 08:44:34 -0700119 abstract BindingRequest request();
dstrasburg2e206ec2018-08-21 13:00:27 -0700120
ronshapiroaeec8dc2018-12-03 13:59:03 -0800121 final TypeMirror returnType() {
122 return returnTypeWrapper().get();
123 }
124
125 abstract Equivalence.Wrapper<TypeMirror> returnTypeWrapper();
126
dstrasburg6d2a7ad2018-09-05 08:05:51 -0700127 abstract MethodSpec methodSpec();
128
129 abstract boolean finalized();
dstrasburg09adeae2018-09-07 12:11:06 -0700130
131 /** Whether a {@link ModifiableBindingMethod} is for the same binding request. */
132 boolean fulfillsSameRequestAs(ModifiableBindingMethod other) {
133 return request().equals(other.request());
134 }
dstrasburg2e206ec2018-08-21 13:00:27 -0700135 }
dstrasburg2e206ec2018-08-21 13:00:27 -0700136}