aboutsummaryrefslogtreecommitdiff
path: root/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/annotationdiscovery/AnnotationFinder.java
blob: 4923567e6be89fe73893c8c856e7a32dc1185e94 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright 2021 Google LLC
 *
 * 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
 *
 *   https://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 com.google.android.enterprise.connectedapps.processor.annotationdiscovery;

import static java.util.stream.Collectors.toSet;
import static javax.lang.model.element.ElementKind.METHOD;

import com.google.android.enterprise.connectedapps.annotations.CrossProfile;
import com.google.android.enterprise.connectedapps.annotations.CrossProfileCallback;
import com.google.android.enterprise.connectedapps.annotations.CrossProfileConfiguration;
import com.google.android.enterprise.connectedapps.annotations.CrossProfileConfigurations;
import com.google.android.enterprise.connectedapps.annotations.CrossProfileProvider;
import com.google.android.enterprise.connectedapps.annotations.CrossUser;
import com.google.android.enterprise.connectedapps.annotations.CrossUserCallback;
import com.google.android.enterprise.connectedapps.annotations.CrossUserConfiguration;
import com.google.android.enterprise.connectedapps.annotations.CrossUserConfigurations;
import com.google.android.enterprise.connectedapps.annotations.CrossUserProvider;
import com.google.android.enterprise.connectedapps.processor.ValidationMessageFormatter;
import com.google.android.enterprise.connectedapps.processor.containers.CrossProfileAnnotationInfo;
import com.google.android.enterprise.connectedapps.processor.containers.CrossProfileCallbackAnnotationInfo;
import com.google.android.enterprise.connectedapps.processor.containers.CrossProfileConfigurationAnnotationInfo;
import com.google.android.enterprise.connectedapps.processor.containers.CrossProfileConfigurationsAnnotationInfo;
import com.google.android.enterprise.connectedapps.processor.containers.CrossProfileProviderAnnotationInfo;
import com.google.android.enterprise.connectedapps.processor.containers.CrossProfileTestAnnotationInfo;
import com.google.android.enterprise.connectedapps.testing.annotations.CrossProfileTest;
import com.google.android.enterprise.connectedapps.testing.annotations.CrossUserTest;
import com.google.common.collect.ImmutableList;
import java.lang.annotation.Annotation;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

/** Helper methods to discover all cross-profile annotations of a specific type on elements. */
public final class AnnotationFinder {

  private static final AnnotationStrings CROSS_PROFILE_ANNOTATION_STRINGS =
      AnnotationStrings.builder()
          .setCrossProfileAnnotationClass(CrossProfile.class)
          .setCrossProfileCallbackAnnotationClass(CrossProfileCallback.class)
          .setCrossProfileConfigurationAnnotationClass(CrossProfileConfiguration.class)
          .setCrossProfileConfigurationsAnnotationClass(CrossProfileConfigurations.class)
          .setCrossProfileProviderAnnotationClass(CrossProfileProvider.class)
          .setCrossProfileTestAnnotationClass(CrossProfileTest.class)
          .build();

  private static final AnnotationStrings CROSS_USER_ANNOTATION_STRINGS =
      AnnotationStrings.builder()
          .setCrossProfileAnnotationClass(CrossUser.class)
          .setCrossProfileCallbackAnnotationClass(CrossUserCallback.class)
          .setCrossProfileProviderAnnotationClass(CrossUserProvider.class)
          .setCrossProfileConfigurationAnnotationClass(CrossUserConfiguration.class)
          .setCrossProfileConfigurationsAnnotationClass(CrossUserConfigurations.class)
          .setCrossProfileTestAnnotationClass(CrossUserTest.class)
          .build();

  private static final ImmutableList<AnnotationStrings> SUPPORTED_ANNOTATIONS =
      ImmutableList.of(CROSS_PROFILE_ANNOTATION_STRINGS, CROSS_USER_ANNOTATION_STRINGS);

  private static final AnnotationStrings DEFAULT_ANNOTATIONS = CROSS_PROFILE_ANNOTATION_STRINGS;

  private static final Set<Class<? extends Annotation>> crossProfileAnnotations =
      annotationsOfType(AnnotationClasses::crossProfileAnnotationClass);

  private static final Set<Class<? extends Annotation>> crossProfileCallbackAnnotations =
      annotationsOfType(AnnotationClasses::crossProfileCallbackAnnotationClass);

  private static final Set<Class<? extends Annotation>> crossProfileConfigurationAnnotations =
      annotationsOfType(AnnotationClasses::crossProfileConfigurationAnnotationClass);

  private static final Set<Class<? extends Annotation>> crossProfileConfigurationsAnnotations =
      annotationsOfType(AnnotationClasses::crossProfileConfigurationsAnnotationClass);

  private static final Set<Class<? extends Annotation>> crossProfileProviderAnnotations =
      annotationsOfType(AnnotationClasses::crossProfileProviderAnnotationClass);

  private static final Set<Class<? extends Annotation>> crossProfileTestAnnotations =
      annotationsOfType(AnnotationClasses::crossProfileTestAnnotationClass);

  public static Iterable<AnnotationStrings> annotationStrings() {
    return SUPPORTED_ANNOTATIONS;
  }

  public static AnnotationNames crossProfileAnnotationNames() {
    return CROSS_PROFILE_ANNOTATION_STRINGS;
  }

  public static AnnotationNames crossUserAnnotationNames() {
    return CROSS_USER_ANNOTATION_STRINGS;
  }

  public static ValidationMessageFormatter validationMessageFormatterFor(Element element) {
    return ValidationMessageFormatter.forAnnotations(annotationNamesFor(element));
  }

  private static AnnotationNames annotationNamesFor(Element element) {
    for (AnnotationStrings annotationStrings : SUPPORTED_ANNOTATIONS) {
      if (hasAnyAnnotationsOfClass(element, annotationStrings)) {
        return annotationStrings;
      }
    }

    return DEFAULT_ANNOTATIONS;
  }

  public static ValidationMessageFormatter validationMessageFormatterForClass(
      TypeElement typeElement) {
    return ValidationMessageFormatter.forAnnotations(annotationNamesForClass(typeElement));
  }

  public static AnnotationNames annotationNamesForClass(TypeElement typeElement) {
    for (AnnotationStrings annotationStrings : SUPPORTED_ANNOTATIONS) {
      if (hasAnyAnnotationsOfClass(typeElement, annotationStrings)) {
        return annotationStrings;
      }

      for (ExecutableElement method :
          typeElement.getEnclosedElements().stream()
              .filter(element -> element.getKind() == METHOD)
              .map(element -> (ExecutableElement) element)
              .collect(toSet())) {
        if (hasAnyAnnotationsOfClass(method, annotationStrings)) {
          return annotationStrings;
        }
      }
    }

    return DEFAULT_ANNOTATIONS;
  }

  private static boolean hasAnyAnnotationsOfClass(
      Element element, AnnotationClasses annotationClasses) {
    return hasAnnotationOfClass(element, annotationClasses.crossProfileAnnotationClass())
        || hasAnnotationOfClass(element, annotationClasses.crossProfileCallbackAnnotationClass())
        || hasAnnotationOfClass(element, annotationClasses.crossProfileProviderAnnotationClass())
        || hasAnnotationOfClass(
            element, annotationClasses.crossProfileConfigurationAnnotationClass())
        || hasAnnotationOfClass(
            element, annotationClasses.crossProfileConfigurationsAnnotationClass())
        || hasAnnotationOfClass(element, annotationClasses.crossProfileTestAnnotationClass());
  }

  private static boolean hasAnnotationOfClass(
      Element element, Class<? extends Annotation> annotationClass) {
    return element.getAnnotation(annotationClass) != null;
  }

  public static CrossProfileAnnotationInfo extractCrossProfileAnnotationInfo(
      Element annotatedElement, Types types, Elements elements) {
    return new CrossProfileAnnotationInfoExtractor()
        .extractAnnotationInfo(SUPPORTED_ANNOTATIONS, annotatedElement, types, elements);
  }

  public static CrossProfileCallbackAnnotationInfo extractCrossProfileCallbackAnnotationInfo(
      Element annotatedElement, Types types, Elements elements) {
    return new CrossProfileCallbackAnnotationInfoExtractor()
        .extractAnnotationInfo(SUPPORTED_ANNOTATIONS, annotatedElement, types, elements);
  }

  public static CrossProfileConfigurationAnnotationInfo
      extractCrossProfileConfigurationAnnotationInfo(
          Element annotatedElement, Types types, Elements elements) {
    return new CrossProfileConfigurationAnnotationInfoExtractor()
        .extractAnnotationInfo(SUPPORTED_ANNOTATIONS, annotatedElement, types, elements);
  }

  public static CrossProfileConfigurationsAnnotationInfo
      extractCrossProfileConfigurationsAnnotationInfo(
          Element annotatedElement, Types types, Elements elements) {
    return new CrossProfileConfigurationsAnnotationInfoExtractor()
        .extractAnnotationInfo(SUPPORTED_ANNOTATIONS, annotatedElement, types, elements);
  }

  public static CrossProfileProviderAnnotationInfo extractCrossProfileProviderAnnotationInfo(
      Element annotatedElement, Types types, Elements elements) {
    return new CrossProfileProviderAnnotationInfoExtractor()
        .extractAnnotationInfo(SUPPORTED_ANNOTATIONS, annotatedElement, types, elements);
  }

  public static CrossProfileTestAnnotationInfo extractCrossProfileTestAnnotationInfo(
      Element annotatedElement, Types types, Elements elements) {
    return new CrossProfileTestAnnotationInfoExtractor()
        .extractAnnotationInfo(SUPPORTED_ANNOTATIONS, annotatedElement, types, elements);
  }

  public static boolean hasCrossProfileAnnotation(Element element) {
    return hasAnyAnnotations(element, crossProfileAnnotations);
  }

  public static boolean hasCrossProfileCallbackAnnotation(Element element) {
    return hasAnyAnnotations(element, crossProfileCallbackAnnotations);
  }

  public static boolean hasCrossProfileConfigurationAnnotation(Element element) {
    return hasAnyAnnotations(element, crossProfileConfigurationAnnotations);
  }

  public static boolean hasCrossProfileConfigurationsAnnotation(Element element) {
    return hasAnyAnnotations(element, crossProfileConfigurationsAnnotations);
  }

  public static boolean hasCrossProfileProviderAnnotation(Element element) {
    return hasAnyAnnotations(element, crossProfileProviderAnnotations);
  }

  private static boolean hasAnyAnnotations(
      Element element, Set<Class<? extends Annotation>> annotations) {
    return annotations.stream().anyMatch(annotation -> element.getAnnotation(annotation) != null);
  }

  public static Stream<? extends Element> elementsAnnotatedWithCrossProfile(
      RoundEnvironment roundEnv) {
    return findElementsContainingAnnotations(roundEnv, crossProfileAnnotations);
  }

  public static Stream<? extends Element> elementsAnnotatedWithCrossProfileCallback(
      RoundEnvironment roundEnv) {
    return findElementsContainingAnnotations(roundEnv, crossProfileCallbackAnnotations);
  }

  public static Stream<? extends Element> elementsAnnotatedWithCrossProfileConfiguration(
      RoundEnvironment roundEnv) {
    return findElementsContainingAnnotations(roundEnv, crossProfileConfigurationAnnotations);
  }

  public static Stream<? extends Element> elementsAnnotatedWithCrossProfileConfigurations(
      RoundEnvironment roundEnv) {
    return findElementsContainingAnnotations(roundEnv, crossProfileConfigurationsAnnotations);
  }

  public static Stream<? extends Element> elementsAnnotatedWithCrossProfileProvider(
      RoundEnvironment roundEnv) {
    return findElementsContainingAnnotations(roundEnv, crossProfileProviderAnnotations);
  }

  public static Stream<? extends Element> elementsAnnotatedWithCrossProfileTest(
      RoundEnvironment roundEnv) {
    return findElementsContainingAnnotations(roundEnv, crossProfileTestAnnotations);
  }

  private static Stream<? extends Element> findElementsContainingAnnotations(
      RoundEnvironment roundEnv, Set<Class<? extends Annotation>> annotations) {
    return annotations.stream()
        .flatMap(annotation -> roundEnv.getElementsAnnotatedWith(annotation).stream());
  }

  private static Set<Class<? extends Annotation>> annotationsOfType(
      Function<AnnotationClasses, Class<? extends Annotation>> annotationClassGetter) {
    return SUPPORTED_ANNOTATIONS.stream().map(annotationClassGetter).collect(toSet());
  }

  private AnnotationFinder() {}
}