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