forked from square/dagger
/
ComponentCreatorAnnotation.java
144 lines (124 loc) · 5.32 KB
/
ComponentCreatorAnnotation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (C) 2019 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dagger.internal.codegen.base;
import static com.google.common.base.Ascii.toUpperCase;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.extension.DaggerStreams.valuesOf;
import static java.util.stream.Collectors.mapping;
import androidx.room.compiler.processing.XTypeElement;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.internal.codegen.javapoet.TypeNames;
import java.util.stream.Collector;
import java.util.stream.Stream;
/** Simple representation of a component creator annotation type. */
public enum ComponentCreatorAnnotation {
COMPONENT_BUILDER(TypeNames.COMPONENT_BUILDER),
COMPONENT_FACTORY(TypeNames.COMPONENT_FACTORY),
SUBCOMPONENT_BUILDER(TypeNames.SUBCOMPONENT_BUILDER),
SUBCOMPONENT_FACTORY(TypeNames.SUBCOMPONENT_FACTORY),
PRODUCTION_COMPONENT_BUILDER(TypeNames.PRODUCTION_COMPONENT_BUILDER),
PRODUCTION_COMPONENT_FACTORY(TypeNames.PRODUCTION_COMPONENT_FACTORY),
PRODUCTION_SUBCOMPONENT_BUILDER(TypeNames.PRODUCTION_SUBCOMPONENT_BUILDER),
PRODUCTION_SUBCOMPONENT_FACTORY(TypeNames.PRODUCTION_SUBCOMPONENT_FACTORY),
;
private final ClassName annotation;
private final ComponentCreatorKind creatorKind;
private final ClassName componentAnnotation;
ComponentCreatorAnnotation(ClassName annotation) {
this.annotation = annotation;
this.creatorKind = ComponentCreatorKind.valueOf(toUpperCase(annotation.simpleName()));
this.componentAnnotation = annotation.enclosingClassName();
}
/** The actual annotation type. */
public ClassName annotation() {
return annotation;
}
/** The component annotation type that encloses this creator annotation type. */
public final ClassName componentAnnotation() {
return componentAnnotation;
}
/** Returns {@code true} if the creator annotation is for a subcomponent. */
public final boolean isSubcomponentCreatorAnnotation() {
return componentAnnotation().simpleName().endsWith("Subcomponent");
}
/**
* Returns {@code true} if the creator annotation is for a production component or subcomponent.
*/
public final boolean isProductionCreatorAnnotation() {
return componentAnnotation().simpleName().startsWith("Production");
}
/** The creator kind the annotation is associated with. */
// TODO(dpb): Remove ComponentCreatorKind.
public ComponentCreatorKind creatorKind() {
return creatorKind;
}
@Override
public final String toString() {
return annotation().canonicalName();
}
/** Returns all component creator annotations. */
public static ImmutableSet<ClassName> allCreatorAnnotations() {
return stream().collect(toAnnotationClasses());
}
/** Returns all root component creator annotations. */
public static ImmutableSet<ClassName> rootComponentCreatorAnnotations() {
return stream()
.filter(
componentCreatorAnnotation ->
!componentCreatorAnnotation.isSubcomponentCreatorAnnotation())
.collect(toAnnotationClasses());
}
/** Returns all subcomponent creator annotations. */
public static ImmutableSet<ClassName> subcomponentCreatorAnnotations() {
return stream()
.filter(
componentCreatorAnnotation ->
componentCreatorAnnotation.isSubcomponentCreatorAnnotation())
.collect(toAnnotationClasses());
}
/** Returns all production component creator annotations. */
public static ImmutableSet<ClassName> productionCreatorAnnotations() {
return stream()
.filter(
componentCreatorAnnotation ->
componentCreatorAnnotation.isProductionCreatorAnnotation())
.collect(toAnnotationClasses());
}
/** Returns the legal creator annotations for the given {@code componentAnnotation}. */
public static ImmutableSet<ClassName> creatorAnnotationsFor(
ComponentAnnotation componentAnnotation) {
return stream()
.filter(
creatorAnnotation ->
creatorAnnotation
.componentAnnotation()
.simpleName()
.equals(componentAnnotation.simpleName()))
.collect(toAnnotationClasses());
}
/** Returns all creator annotations present on the given {@code type}. */
public static ImmutableSet<ComponentCreatorAnnotation> getCreatorAnnotations(XTypeElement type) {
return stream().filter(cca -> type.hasAnnotation(cca.annotation())).collect(toImmutableSet());
}
private static Stream<ComponentCreatorAnnotation> stream() {
return valuesOf(ComponentCreatorAnnotation.class);
}
private static Collector<ComponentCreatorAnnotation, ?, ImmutableSet<ClassName>>
toAnnotationClasses() {
return mapping(ComponentCreatorAnnotation::annotation, toImmutableSet());
}
}