aboutsummaryrefslogtreecommitdiff
path: root/java/dagger/internal/codegen/base/ComponentAnnotation.java
blob: c9b3580c1c9e5653235c9256d27eb016c1331068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (C) 2019 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.internal.codegen.base;

import static com.google.auto.common.AnnotationMirrors.getAnnotationValue;
import static com.google.auto.common.MoreElements.asType;
import static com.google.auto.common.MoreTypes.asTypeElements;
import static com.google.auto.common.MoreTypes.isTypeOf;
import static dagger.internal.codegen.base.MoreAnnotationValues.asAnnotationValues;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.javapoet.TypeNames.PRODUCER_MODULE;
import static dagger.internal.codegen.langmodel.DaggerElements.getAnyAnnotation;

import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.Component;
import dagger.Subcomponent;
import dagger.producers.ProductionComponent;
import dagger.producers.ProductionSubcomponent;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

/**
 * A {@code @Component}, {@code @Subcomponent}, {@code @ProductionComponent}, or
 * {@code @ProductionSubcomponent} annotation, or a {@code @Module} or {@code @ProducerModule}
 * annotation that is being treated as a component annotation when validating full binding graphs
 * for modules.
 */
public abstract class ComponentAnnotation {
  /** The root component annotation types. */
  private static final ImmutableSet<Class<? extends Annotation>> ROOT_COMPONENT_ANNOTATIONS =
     ImmutableSet.of(Component.class, ProductionComponent.class);

  /** The subcomponent annotation types. */
  private static final ImmutableSet<Class<? extends Annotation>> SUBCOMPONENT_ANNOTATIONS =
     ImmutableSet.of(Subcomponent.class, ProductionSubcomponent.class);

  /** All component annotation types. */
  private static final ImmutableSet<Class<? extends Annotation>> ALL_COMPONENT_ANNOTATIONS =
     ImmutableSet.<Class<? extends Annotation>>builder()
         .addAll(ROOT_COMPONENT_ANNOTATIONS)
         .addAll(SUBCOMPONENT_ANNOTATIONS)
         .build();

  /** The annotation itself. */
  public abstract AnnotationMirror annotation();

  /** The simple name of the annotation type. */
  public String simpleName() {
    return MoreAnnotationMirrors.simpleName(annotation()).toString();
  }

  /**
   * Returns {@code true} if the annotation is a {@code @Subcomponent} or
   * {@code @ProductionSubcomponent}.
   */
  public abstract boolean isSubcomponent();

  /**
   * Returns {@code true} if the annotation is a {@code @ProductionComponent},
   * {@code @ProductionSubcomponent}, or {@code @ProducerModule}.
   */
  public abstract boolean isProduction();

  /**
   * Returns {@code true} if the annotation is a real component annotation and not a module
   * annotation.
   */
  public abstract boolean isRealComponent();

  /** The values listed as {@code dependencies}. */
  public abstract ImmutableList<AnnotationValue> dependencyValues();

  /** The types listed as {@code dependencies}. */
  public ImmutableList<TypeMirror> dependencyTypes() {
    return dependencyValues().stream().map(MoreAnnotationValues::asType).collect(toImmutableList());
  }

  /**
   * The types listed as {@code dependencies}.
   *
   * @throws IllegalArgumentException if any of {@link #dependencyTypes()} are error types
   */
  public ImmutableList<TypeElement> dependencies() {
    return asTypeElements(dependencyTypes()).asList();
  }

  /** The values listed as {@code modules}. */
  public abstract ImmutableList<AnnotationValue> moduleValues();

  /** The types listed as {@code modules}. */
  public ImmutableList<TypeMirror> moduleTypes() {
    return moduleValues().stream().map(MoreAnnotationValues::asType).collect(toImmutableList());
  }

  /**
   * The types listed as {@code modules}.
   *
   * @throws IllegalArgumentException if any of {@link #moduleTypes()} are error types
   */
  public ImmutableSet<TypeElement> modules() {
    return asTypeElements(moduleTypes());
  }

  protected final ImmutableList<AnnotationValue> getAnnotationValues(String parameterName) {
    return asAnnotationValues(getAnnotationValue(annotation(), parameterName));
  }

  /**
   * Returns an object representing a root component annotation, not a subcomponent annotation, if
   * one is present on {@code typeElement}.
   */
  public static Optional<ComponentAnnotation> rootComponentAnnotation(TypeElement typeElement) {
    return anyComponentAnnotation(typeElement, ROOT_COMPONENT_ANNOTATIONS);
  }

  /**
   * Returns an object representing a subcomponent annotation, if one is present on {@code
   * typeElement}.
   */
  public static Optional<ComponentAnnotation> subcomponentAnnotation(TypeElement typeElement) {
    return anyComponentAnnotation(typeElement, SUBCOMPONENT_ANNOTATIONS);
  }

  /**
   * Returns an object representing a root component or subcomponent annotation, if one is present
   * on {@code typeElement}.
   */
  public static Optional<ComponentAnnotation> anyComponentAnnotation(TypeElement typeElement) {
    return anyComponentAnnotation(typeElement, ALL_COMPONENT_ANNOTATIONS);
  }

  private static Optional<ComponentAnnotation> anyComponentAnnotation(
      TypeElement typeElement, Collection<Class<? extends Annotation>> annotations) {
    return getAnyAnnotation(typeElement, annotations).map(ComponentAnnotation::componentAnnotation);
  }

  /** Returns {@code true} if the argument is a component annotation. */
  public static boolean isComponentAnnotation(AnnotationMirror annotation) {
    return ALL_COMPONENT_ANNOTATIONS.stream()
        .anyMatch(annotationClass -> isTypeOf(annotationClass, annotation.getAnnotationType()));
  }

  /** Creates an object representing a component or subcomponent annotation. */
  public static ComponentAnnotation componentAnnotation(AnnotationMirror annotation) {
    RealComponentAnnotation.Builder annotationBuilder =
        RealComponentAnnotation.builder().annotation(annotation);

    if (isTypeOf(Component.class, annotation.getAnnotationType())) {
      return annotationBuilder.isProduction(false).isSubcomponent(false).build();
    }
    if (isTypeOf(Subcomponent.class, annotation.getAnnotationType())) {
      return annotationBuilder.isProduction(false).isSubcomponent(true).build();
    }
    if (isTypeOf(ProductionComponent.class, annotation.getAnnotationType())) {
      return annotationBuilder.isProduction(true).isSubcomponent(false).build();
    }
    if (isTypeOf(ProductionSubcomponent.class, annotation.getAnnotationType())) {
      return annotationBuilder.isProduction(true).isSubcomponent(true).build();
    }
    throw new IllegalArgumentException(
        annotation
            + " must be a Component, Subcomponent, ProductionComponent, "
            + "or ProductionSubcomponent annotation");
  }

  /** Creates a fictional component annotation representing a module. */
  public static ComponentAnnotation fromModuleAnnotation(ModuleAnnotation moduleAnnotation) {
    return new AutoValue_ComponentAnnotation_FictionalComponentAnnotation(moduleAnnotation);
  }

  /** The root component annotation types. */
  public static ImmutableSet<Class<? extends Annotation>> rootComponentAnnotations() {
    return ROOT_COMPONENT_ANNOTATIONS;
  }

  /** The subcomponent annotation types. */
  public static ImmutableSet<Class<? extends Annotation>> subcomponentAnnotations() {
    return SUBCOMPONENT_ANNOTATIONS;
  }

  /** All component annotation types. */
  public static ImmutableSet<Class<? extends Annotation>> allComponentAnnotations() {
    return ALL_COMPONENT_ANNOTATIONS;
  }

  /**
   * An actual component annotation.
   *
   * @see FictionalComponentAnnotation
   */
  @AutoValue
  abstract static class RealComponentAnnotation extends ComponentAnnotation {

    @Override
    @Memoized
    public ImmutableList<AnnotationValue> dependencyValues() {
      return isSubcomponent() ? ImmutableList.of() : getAnnotationValues("dependencies");
    }

    @Override
    @Memoized
    public ImmutableList<TypeMirror> dependencyTypes() {
      return super.dependencyTypes();
    }

    @Override
    @Memoized
    public ImmutableList<TypeElement> dependencies() {
      return super.dependencies();
    }

    @Override
    public boolean isRealComponent() {
      return true;
    }

    @Override
    @Memoized
    public ImmutableList<AnnotationValue> moduleValues() {
      return getAnnotationValues("modules");
    }

    @Override
    @Memoized
    public ImmutableList<TypeMirror> moduleTypes() {
      return super.moduleTypes();
    }

    @Override
    @Memoized
    public ImmutableSet<TypeElement> modules() {
      return super.modules();
    }

    static Builder builder() {
      return new AutoValue_ComponentAnnotation_RealComponentAnnotation.Builder();
    }

    @AutoValue.Builder
    interface Builder {
      Builder annotation(AnnotationMirror annotation);

      Builder isSubcomponent(boolean isSubcomponent);

      Builder isProduction(boolean isProduction);

      RealComponentAnnotation build();
    }
  }

  /**
   * A fictional component annotation used to represent modules or other collections of bindings as
   * a component.
   */
  @AutoValue
  abstract static class FictionalComponentAnnotation extends ComponentAnnotation {

    @Override
    public AnnotationMirror annotation() {
      return moduleAnnotation().annotation();
    }

    @Override
    public boolean isSubcomponent() {
      return false;
    }

    @Override
    public boolean isProduction() {
      return ClassName.get(asType(moduleAnnotation().annotation().getAnnotationType().asElement()))
          .equals(PRODUCER_MODULE);
    }

    @Override
    public boolean isRealComponent() {
      return false;
    }

    @Override
    public ImmutableList<AnnotationValue> dependencyValues() {
      return ImmutableList.of();
    }

    @Override
    public ImmutableList<AnnotationValue> moduleValues() {
      return moduleAnnotation().includesAsAnnotationValues();
    }

    @Override
    @Memoized
    public ImmutableList<TypeMirror> moduleTypes() {
      return super.moduleTypes();
    }

    @Override
    @Memoized
    public ImmutableSet<TypeElement> modules() {
      return super.modules();
    }

    public abstract ModuleAnnotation moduleAnnotation();
  }
}