• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.writing;
18 
19 import androidx.room.compiler.processing.XFiler;
20 import androidx.room.compiler.processing.XProcessingEnv;
21 import androidx.room.compiler.processing.XTypeElement;
22 import dagger.MapKey;
23 import java.util.Set;
24 import javax.inject.Inject;
25 
26 /**
27  * Generates classes that create annotation instances for an unwrapped {@link MapKey} annotation
28  * type whose nested value is an annotation. The generated class will have a private empty
29  * constructor and a static method that creates each annotation type that is nested in the top-level
30  * annotation type.
31  *
32  * <p>So for an example {@link MapKey} annotation:
33  *
34  * <pre>
35  *   {@literal @MapKey}(unwrapValue = true)
36  *   {@literal @interface} Foo {
37  *     Bar bar();
38  *   }
39  *
40  *   {@literal @interface} Bar {
41  *     {@literal Class<?> baz();}
42  *   }
43  * </pre>
44  *
45  * the generated class will look like:
46  *
47  * <pre>
48  *   public final class FooCreator {
49  *     private FooCreator() {}
50  *
51  *     public static Bar createBar({@literal Class<?> baz}) { … }
52  *   }
53  * </pre>
54  */
55 public final class UnwrappedMapKeyGenerator extends AnnotationCreatorGenerator {
56 
57   @Inject
UnwrappedMapKeyGenerator(XFiler filer, XProcessingEnv processingEnv)58   UnwrappedMapKeyGenerator(XFiler filer, XProcessingEnv processingEnv) {
59     super(filer, processingEnv);
60   }
61 
62   @Override
annotationsToCreate(XTypeElement annotationElement)63   protected Set<XTypeElement> annotationsToCreate(XTypeElement annotationElement) {
64     Set<XTypeElement> nestedAnnotationElements = super.annotationsToCreate(annotationElement);
65     nestedAnnotationElements.remove(annotationElement);
66     return nestedAnnotationElements;
67   }
68 }
69