• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 
19 import static dagger.internal.codegen.binding.MapKeys.getUnwrappedMapKeyType;
20 import static javax.lang.model.element.ElementKind.ANNOTATION_TYPE;
21 
22 import com.google.auto.common.MoreElements;
23 import com.google.auto.common.MoreTypes;
24 import com.google.common.collect.ImmutableSet;
25 import dagger.MapKey;
26 import dagger.internal.codegen.langmodel.DaggerTypes;
27 import dagger.internal.codegen.validation.MapKeyValidator;
28 import dagger.internal.codegen.validation.TypeCheckingProcessingStep;
29 import dagger.internal.codegen.validation.ValidationReport;
30 import dagger.internal.codegen.writing.AnnotationCreatorGenerator;
31 import dagger.internal.codegen.writing.UnwrappedMapKeyGenerator;
32 import java.lang.annotation.Annotation;
33 import java.util.Set;
34 import javax.annotation.processing.Messager;
35 import javax.inject.Inject;
36 import javax.lang.model.element.Element;
37 import javax.lang.model.element.ElementKind;
38 import javax.lang.model.element.TypeElement;
39 import javax.lang.model.type.DeclaredType;
40 
41 /**
42  * The annotation processor responsible for validating the mapKey annotation and auto-generate
43  * implementation of annotations marked with {@link MapKey @MapKey} where necessary.
44  */
45 final class MapKeyProcessingStep extends TypeCheckingProcessingStep<TypeElement> {
46   private final Messager messager;
47   private final DaggerTypes types;
48   private final MapKeyValidator mapKeyValidator;
49   private final AnnotationCreatorGenerator annotationCreatorGenerator;
50   private final UnwrappedMapKeyGenerator unwrappedMapKeyGenerator;
51 
52   @Inject
MapKeyProcessingStep( Messager messager, DaggerTypes types, MapKeyValidator mapKeyValidator, AnnotationCreatorGenerator annotationCreatorGenerator, UnwrappedMapKeyGenerator unwrappedMapKeyGenerator)53   MapKeyProcessingStep(
54       Messager messager,
55       DaggerTypes types,
56       MapKeyValidator mapKeyValidator,
57       AnnotationCreatorGenerator annotationCreatorGenerator,
58       UnwrappedMapKeyGenerator unwrappedMapKeyGenerator) {
59     super(MoreElements::asType);
60     this.messager = messager;
61     this.types = types;
62     this.mapKeyValidator = mapKeyValidator;
63     this.annotationCreatorGenerator = annotationCreatorGenerator;
64     this.unwrappedMapKeyGenerator = unwrappedMapKeyGenerator;
65   }
66 
67   @Override
annotations()68   public Set<Class<? extends Annotation>> annotations() {
69     return ImmutableSet.<Class<? extends Annotation>>of(MapKey.class);
70   }
71 
72   @Override
process( TypeElement mapKeyAnnotationType, ImmutableSet<Class<? extends Annotation>> annotations)73   protected void process(
74       TypeElement mapKeyAnnotationType, ImmutableSet<Class<? extends Annotation>> annotations) {
75     ValidationReport<Element> mapKeyReport = mapKeyValidator.validate(mapKeyAnnotationType);
76     mapKeyReport.printMessagesTo(messager);
77 
78     if (mapKeyReport.isClean()) {
79       MapKey mapkey = mapKeyAnnotationType.getAnnotation(MapKey.class);
80       if (!mapkey.unwrapValue()) {
81         annotationCreatorGenerator.generate(mapKeyAnnotationType, messager);
82       } else if (unwrappedValueKind(mapKeyAnnotationType).equals(ANNOTATION_TYPE)) {
83         unwrappedMapKeyGenerator.generate(mapKeyAnnotationType, messager);
84       }
85     }
86   }
87 
unwrappedValueKind(TypeElement mapKeyAnnotationType)88   private ElementKind unwrappedValueKind(TypeElement mapKeyAnnotationType) {
89     DeclaredType unwrappedMapKeyType =
90         getUnwrappedMapKeyType(MoreTypes.asDeclared(mapKeyAnnotationType.asType()), types);
91     return unwrappedMapKeyType.asElement().getKind();
92   }
93 }
94