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.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 java.lang.annotation.Annotation; 28 import java.util.Set; 29 import javax.annotation.processing.Messager; 30 import javax.inject.Inject; 31 import javax.lang.model.element.Element; 32 import javax.lang.model.element.ElementKind; 33 import javax.lang.model.element.TypeElement; 34 import javax.lang.model.type.DeclaredType; 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 public class MapKeyProcessingStep extends TypeCheckingProcessingStep<TypeElement> { 41 private final Messager messager; 42 private final DaggerTypes types; 43 private final MapKeyValidator mapKeyValidator; 44 private final AnnotationCreatorGenerator annotationCreatorGenerator; 45 private final UnwrappedMapKeyGenerator unwrappedMapKeyGenerator; 46 47 @Inject MapKeyProcessingStep( Messager messager, DaggerTypes types, MapKeyValidator mapKeyValidator, AnnotationCreatorGenerator annotationCreatorGenerator, UnwrappedMapKeyGenerator unwrappedMapKeyGenerator)48 MapKeyProcessingStep( 49 Messager messager, 50 DaggerTypes types, 51 MapKeyValidator mapKeyValidator, 52 AnnotationCreatorGenerator annotationCreatorGenerator, 53 UnwrappedMapKeyGenerator unwrappedMapKeyGenerator) { 54 super(MoreElements::asType); 55 this.messager = messager; 56 this.types = types; 57 this.mapKeyValidator = mapKeyValidator; 58 this.annotationCreatorGenerator = annotationCreatorGenerator; 59 this.unwrappedMapKeyGenerator = unwrappedMapKeyGenerator; 60 } 61 62 @Override annotations()63 public Set<Class<? extends Annotation>> annotations() { 64 return ImmutableSet.<Class<? extends Annotation>>of(MapKey.class); 65 } 66 67 @Override process( TypeElement mapKeyAnnotationType, ImmutableSet<Class<? extends Annotation>> annotations)68 protected void process( 69 TypeElement mapKeyAnnotationType, ImmutableSet<Class<? extends Annotation>> annotations) { 70 ValidationReport<Element> mapKeyReport = mapKeyValidator.validate(mapKeyAnnotationType); 71 mapKeyReport.printMessagesTo(messager); 72 73 if (mapKeyReport.isClean()) { 74 MapKey mapkey = mapKeyAnnotationType.getAnnotation(MapKey.class); 75 if (!mapkey.unwrapValue()) { 76 annotationCreatorGenerator.generate(mapKeyAnnotationType, messager); 77 } else if (unwrappedValueKind(mapKeyAnnotationType).equals(ANNOTATION_TYPE)) { 78 unwrappedMapKeyGenerator.generate(mapKeyAnnotationType, messager); 79 } 80 } 81 } 82 unwrappedValueKind(TypeElement mapKeyAnnotationType)83 private ElementKind unwrappedValueKind(TypeElement mapKeyAnnotationType) { 84 DeclaredType unwrappedMapKeyType = 85 getUnwrappedMapKeyType(MoreTypes.asDeclared(mapKeyAnnotationType.asType()), types); 86 return unwrappedMapKeyType.asElement().getKind(); 87 } 88 } 89