| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package dagger.internal.codegen; |
| 18 | |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkState; |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 21 | import static dagger.internal.codegen.DaggerStreams.toImmutableList; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 22 | |
| 23 | import com.google.auto.value.AutoValue; |
| 24 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.Maps; |
| 26 | import com.google.common.collect.Sets; |
| 27 | import com.squareup.javapoet.MethodSpec; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 28 | import java.util.Map; |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 29 | import java.util.Optional; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 30 | import 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 | */ |
| 41 | final class ModifiableBindingMethods { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 42 | private final Map<BindingRequest, ModifiableBindingMethod> methods = Maps.newLinkedHashMap(); |
| 43 | private final Set<BindingRequest> finalizedMethods = Sets.newHashSet(); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 44 | |
| 45 | /** Register a method encapsulating a modifiable binding. */ |
| 46 | void addMethod( |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 47 | ModifiableBindingType type, BindingRequest request, MethodSpec method, boolean finalized) { |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 48 | checkArgument(type.isModifiable()); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 49 | if (finalized) { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 50 | finalizedMethods.add(request); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 51 | } |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 52 | methods.put(request, ModifiableBindingMethod.create(type, request, method, finalized)); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 55 | /** 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. */ |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 61 | Optional<ModifiableBindingMethod> getMethod(BindingRequest request) { |
| 62 | return Optional.ofNullable(methods.get(request)); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Mark the {@link ModifiableBindingMethod} as having been implemented, thus modifying the |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 67 | * binding. |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 68 | */ |
| 69 | void methodImplemented(ModifiableBindingMethod method) { |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 70 | if (method.finalized()) { |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 71 | checkState( |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 72 | finalizedMethods.add(method.request()), |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 73 | "Implementing and finalizing a modifiable binding method that has been marked as " |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 74 | + "finalized in the current subcomponent implementation. The binding is for a %s " |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 75 | + "of type %s.", |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 76 | method.request(), |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 77 | method.type()); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | /** Whether a given binding has been marked as finalized. */ |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 82 | boolean finalized(ModifiableBindingMethod method) { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 83 | return finalizedMethods.contains(method.request()); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @AutoValue |
| 87 | abstract static class ModifiableBindingMethod { |
| 88 | private static ModifiableBindingMethod create( |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 89 | ModifiableBindingType type, |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 90 | BindingRequest request, |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 91 | MethodSpec methodSpec, |
| 92 | boolean finalized) { |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 93 | return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod( |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 94 | type, request, methodSpec, finalized); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 95 | } |
| 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( |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 101 | unimplementedMethod.type(), unimplementedMethod.request(), methodSpec, finalized); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | abstract ModifiableBindingType type(); |
| 105 | |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 106 | abstract BindingRequest request(); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 107 | |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 108 | abstract MethodSpec methodSpec(); |
| 109 | |
| 110 | abstract boolean finalized(); |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame^] | 111 | |
| 112 | /** Whether a {@link ModifiableBindingMethod} is for the same binding request. */ |
| 113 | boolean fulfillsSameRequestAs(ModifiableBindingMethod other) { |
| 114 | return request().equals(other.request()); |
| 115 | } |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 116 | } |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 117 | } |