| 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 | |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 23 | import com.google.auto.common.MoreTypes; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 24 | import com.google.auto.value.AutoValue; |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 25 | import com.google.common.base.Equivalence; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableList; |
| 27 | import com.google.common.collect.Maps; |
| 28 | import com.google.common.collect.Sets; |
| 29 | import com.squareup.javapoet.MethodSpec; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 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; |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 33 | import javax.lang.model.type.TypeMirror; |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 34 | |
| 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 | */ |
| 44 | final class ModifiableBindingMethods { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 45 | private final Map<BindingRequest, ModifiableBindingMethod> methods = Maps.newLinkedHashMap(); |
| 46 | private final Set<BindingRequest> finalizedMethods = Sets.newHashSet(); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 47 | |
| 48 | /** Register a method encapsulating a modifiable binding. */ |
| 49 | void addMethod( |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 50 | ModifiableBindingType type, |
| 51 | BindingRequest request, |
| 52 | TypeMirror returnType, |
| 53 | MethodSpec method, |
| 54 | boolean finalized) { |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 55 | checkArgument(type.isModifiable()); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 56 | if (finalized) { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 57 | finalizedMethods.add(request); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 58 | } |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 59 | methods.put( |
| 60 | request, ModifiableBindingMethod.create(type, request, returnType, method, finalized)); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 63 | /** 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. */ |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 69 | Optional<ModifiableBindingMethod> getMethod(BindingRequest request) { |
| 70 | return Optional.ofNullable(methods.get(request)); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| ronshapiro | fd7f1b3 | 2018-12-07 05:42:12 -0800 | [diff] [blame] | 73 | /** Returns all of the {@link ModifiableBindingMethod}s. */ |
| 74 | ImmutableList<ModifiableBindingMethod> allMethods() { |
| 75 | return ImmutableList.copyOf(methods.values()); |
| 76 | } |
| 77 | |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Mark the {@link ModifiableBindingMethod} as having been implemented, thus modifying the |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 80 | * binding. |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 81 | */ |
| 82 | void methodImplemented(ModifiableBindingMethod method) { |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 83 | if (method.finalized()) { |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 84 | checkState( |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 85 | finalizedMethods.add(method.request()), |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 86 | "Implementing and finalizing a modifiable binding method that has been marked as " |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 87 | + "finalized in the current subcomponent implementation. The binding is for a %s " |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 88 | + "of type %s.", |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 89 | method.request(), |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 90 | method.type()); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | /** Whether a given binding has been marked as finalized. */ |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 95 | boolean finalized(ModifiableBindingMethod method) { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 96 | return finalizedMethods.contains(method.request()); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @AutoValue |
| 100 | abstract static class ModifiableBindingMethod { |
| 101 | private static ModifiableBindingMethod create( |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 102 | ModifiableBindingType type, |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 103 | BindingRequest request, |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 104 | TypeMirror returnType, |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 105 | MethodSpec methodSpec, |
| 106 | boolean finalized) { |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 107 | return new AutoValue_ModifiableBindingMethods_ModifiableBindingMethod( |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 108 | type, request, MoreTypes.equivalence().wrap(returnType), methodSpec, finalized); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 109 | } |
| 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( |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 115 | unimplementedMethod.type(), |
| 116 | unimplementedMethod.request(), |
| 117 | unimplementedMethod.returnTypeWrapper(), |
| 118 | methodSpec, |
| 119 | finalized); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | abstract ModifiableBindingType type(); |
| 123 | |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 124 | abstract BindingRequest request(); |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 125 | |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 126 | final TypeMirror returnType() { |
| 127 | return returnTypeWrapper().get(); |
| 128 | } |
| 129 | |
| 130 | abstract Equivalence.Wrapper<TypeMirror> returnTypeWrapper(); |
| 131 | |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 132 | abstract MethodSpec methodSpec(); |
| 133 | |
| 134 | abstract boolean finalized(); |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 135 | |
| 136 | /** Whether a {@link ModifiableBindingMethod} is for the same binding request. */ |
| 137 | boolean fulfillsSameRequestAs(ModifiableBindingMethod other) { |
| 138 | return request().equals(other.request()); |
| 139 | } |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 140 | } |
| dstrasburg | 2e206ec | 2018-08-21 13:00:27 -0700 | [diff] [blame] | 141 | } |