| 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; |
| 28 | import dagger.model.Key; |
| 29 | import dagger.model.RequestKind; |
| 30 | import java.util.Map; |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 31 | import java.util.Optional; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 32 | import java.util.Set; |
| 33 | |
| 34 | /** |
| 35 | * A registry for those methods which each wrap a binding whose definition may be modified across |
| 36 | * each class in the class hierarchy implementing a subcomponent. Subcomponent implementations are |
| 37 | * spread across a class hierarchy when generating ahead-of-time subcomponents. There is one |
| 38 | * subcomponent implementation class for each of the subcomponent's ancestor components. An instance |
| 39 | * of {@link ModifiableBindingMethod} is associated with a single class in this hierarchy. For a |
| 40 | * given subcomponent implementation class we can use the {@link ModifiableBindingMethod}s of its |
| 41 | * superclasses to know what binding methods to attempt to modify. |
| 42 | */ |
| 43 | final class ModifiableBindingMethods { |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 44 | private final Map<KeyAndKind, ModifiableBindingMethod> methods = Maps.newLinkedHashMap(); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 45 | private final Set<KeyAndKind> finalizedMethods = Sets.newHashSet(); |
| 46 | |
| 47 | /** Register a method encapsulating a modifiable binding. */ |
| 48 | void addMethod( |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 49 | ModifiableBindingType type, Key key, RequestKind kind, MethodSpec method, boolean finalized) { |
| 50 | checkArgument(type.isModifiable()); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 51 | KeyAndKind keyAndKind = KeyAndKind.create(key, kind); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 52 | if (finalized) { |
| 53 | finalizedMethods.add(keyAndKind); |
| 54 | } |
| 55 | methods.put(keyAndKind, ModifiableBindingMethod.create(type, key, kind, method, finalized)); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 58 | /** Returns all {@link ModifiableBindingMethod}s that have not been marked as finalized. */ |
| 59 | ImmutableList<ModifiableBindingMethod> getNonFinalizedMethods() { |
| 60 | return methods.values().stream().filter(m -> !m.finalized()).collect(toImmutableList()); |
| 61 | } |
| 62 | |
| 63 | /** Returns the {@link ModifiableBindingMethod} for the given binding if present. */ |
| 64 | Optional<ModifiableBindingMethod> getMethod(Key key, RequestKind kind) { |
| 65 | return Optional.ofNullable(methods.get(KeyAndKind.create(key, kind))); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Mark the {@link ModifiableBindingMethod} as having been implemented, thus modifying the |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 70 | * binding. |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 71 | */ |
| 72 | void methodImplemented(ModifiableBindingMethod method) { |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 73 | if (method.finalized()) { |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 74 | checkState( |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 75 | finalizedMethods.add(KeyAndKind.create(method.key(), method.kind())), |
| 76 | "Implementing and finalizing a modifiable binding method that has been marked as " |
| 77 | + "finalized in the current subcomponent implementation. The binding is for a %s-%s " |
| 78 | + "of type %s.", |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 79 | method.key(), |
| 80 | method.kind(), |
| 81 | method.type()); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | /** Whether a given binding has been marked as finalized. */ |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 86 | boolean finalized(ModifiableBindingMethod method) { |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 87 | return finalizedMethods.contains(KeyAndKind.create(method.key(), method.kind())); |
| 88 | } |
| 89 | |
| 90 | @AutoValue |
| 91 | abstract static class ModifiableBindingMethod { |
| 92 | private static ModifiableBindingMethod create( |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 93 | ModifiableBindingType type, |
| 94 | Key key, |
| 95 | RequestKind kind, |
| 96 | MethodSpec methodSpec, |
| 97 | boolean finalized) { |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 98 | return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod( |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 99 | type, key, kind, methodSpec, finalized); |
| 100 | } |
| 101 | |
| 102 | /** Create a {@ModifiableBindingMethod} representing an implementation of an existing method. */ |
| 103 | static ModifiableBindingMethod implement( |
| 104 | ModifiableBindingMethod unimplementedMethod, MethodSpec methodSpec, boolean finalized) { |
| 105 | return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod( |
| 106 | unimplementedMethod.type(), |
| 107 | unimplementedMethod.key(), |
| 108 | unimplementedMethod.kind(), |
| 109 | methodSpec, |
| 110 | finalized); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | abstract ModifiableBindingType type(); |
| 114 | |
| 115 | abstract Key key(); |
| 116 | |
| 117 | abstract RequestKind kind(); |
| 118 | |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 119 | abstract MethodSpec methodSpec(); |
| 120 | |
| 121 | abstract boolean finalized(); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @AutoValue |
| 125 | abstract static class KeyAndKind { |
| 126 | private static KeyAndKind create(Key key, RequestKind kind) { |
| 127 | return new AutoValue_ModifiableBindingMethods_KeyAndKind(key, kind); |
| 128 | } |
| 129 | |
| 130 | abstract Key key(); |
| 131 | |
| 132 | abstract RequestKind kind(); |
| 133 | } |
| 134 | } |