• 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 dagger.internal.codegen.xprocessing.XTypes.isDeclared;
21 
22 import androidx.room.compiler.processing.XMessager;
23 import androidx.room.compiler.processing.XType;
24 import androidx.room.compiler.processing.XTypeElement;
25 import com.google.common.collect.ImmutableSet;
26 import com.squareup.javapoet.ClassName;
27 import dagger.MapKey;
28 import dagger.internal.codegen.javapoet.TypeNames;
29 import dagger.internal.codegen.validation.MapKeyValidator;
30 import dagger.internal.codegen.validation.TypeCheckingProcessingStep;
31 import dagger.internal.codegen.validation.ValidationReport;
32 import dagger.internal.codegen.writing.AnnotationCreatorGenerator;
33 import dagger.internal.codegen.writing.UnwrappedMapKeyGenerator;
34 import javax.inject.Inject;
35 
36 /**
37  * The annotation processor responsible for validating the mapKey annotation and auto-generate
38  * implementation of annotations marked with {@link MapKey @MapKey} where necessary.
39  */
40 final class MapKeyProcessingStep extends TypeCheckingProcessingStep<XTypeElement> {
41   private final XMessager messager;
42   private final MapKeyValidator mapKeyValidator;
43   private final AnnotationCreatorGenerator annotationCreatorGenerator;
44   private final UnwrappedMapKeyGenerator unwrappedMapKeyGenerator;
45 
46   @Inject
MapKeyProcessingStep( XMessager messager, MapKeyValidator mapKeyValidator, AnnotationCreatorGenerator annotationCreatorGenerator, UnwrappedMapKeyGenerator unwrappedMapKeyGenerator)47   MapKeyProcessingStep(
48       XMessager messager,
49       MapKeyValidator mapKeyValidator,
50       AnnotationCreatorGenerator annotationCreatorGenerator,
51       UnwrappedMapKeyGenerator unwrappedMapKeyGenerator) {
52     this.messager = messager;
53     this.mapKeyValidator = mapKeyValidator;
54     this.annotationCreatorGenerator = annotationCreatorGenerator;
55     this.unwrappedMapKeyGenerator = unwrappedMapKeyGenerator;
56   }
57 
58   @Override
annotationClassNames()59   public ImmutableSet<ClassName> annotationClassNames() {
60     return ImmutableSet.of(TypeNames.MAP_KEY);
61   }
62 
63   @Override
process(XTypeElement mapAnnotation, ImmutableSet<ClassName> annotations)64   protected void process(XTypeElement mapAnnotation, ImmutableSet<ClassName> annotations) {
65     ValidationReport mapKeyReport = mapKeyValidator.validate(mapAnnotation);
66     mapKeyReport.printMessagesTo(messager);
67 
68     if (mapKeyReport.isClean()) {
69       if (!mapAnnotation.getAnnotation(TypeNames.MAP_KEY).getAsBoolean("unwrapValue")) {
70         annotationCreatorGenerator.generate(mapAnnotation, messager);
71       } else if (isAnnotationType(getUnwrappedMapKeyType(mapAnnotation.getType()))) {
72         unwrappedMapKeyGenerator.generate(mapAnnotation, messager);
73       }
74     }
75   }
76 
isAnnotationType(XType type)77   private boolean isAnnotationType(XType type) {
78     return isDeclared(type) && type.getTypeElement().isAnnotationClass();
79   }
80 }
81