• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
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  *   https://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 package com.google.android.enterprise.connectedapps.processor.containers;
17 
18 import static com.google.android.enterprise.connectedapps.processor.CommonClassNames.SERVICE_CLASSNAME;
19 
20 import com.google.android.enterprise.connectedapps.annotations.CrossProfileConfiguration;
21 import com.google.android.enterprise.connectedapps.processor.annotationdiscovery.AnnotationFinder;
22 import com.google.auto.value.AutoValue;
23 import com.google.common.collect.ImmutableCollection;
24 import com.google.common.collect.ImmutableSet;
25 import com.squareup.javapoet.ClassName;
26 import java.util.Optional;
27 import javax.lang.model.element.Element;
28 import javax.lang.model.element.TypeElement;
29 
30 /** A wrapper around basic information from a {@link CrossProfileConfiguration} annotation. */
31 @AutoValue
32 public abstract class ValidatorCrossProfileConfigurationInfo {
33 
configurationElement()34   public abstract TypeElement configurationElement();
35 
providerClassElements()36   public abstract ImmutableCollection<TypeElement> providerClassElements();
37 
serviceSuperclass()38   public abstract ClassName serviceSuperclass();
39 
serviceClass()40   public abstract Optional<TypeElement> serviceClass();
41 
connector()42   public abstract Optional<TypeElement> connector();
43 
createMultipleFromElement( Context context, TypeElement annotatedElement)44   public static ImmutableSet<ValidatorCrossProfileConfigurationInfo> createMultipleFromElement(
45       Context context, TypeElement annotatedElement) {
46     ImmutableSet<CrossProfileConfigurationAnnotationInfo> infos =
47         AnnotationFinder.extractCrossProfileConfigurationsAnnotationInfo(context, annotatedElement)
48             .configurations();
49     ImmutableSet.Builder<ValidatorCrossProfileConfigurationInfo> configurations =
50         ImmutableSet.builder();
51 
52     if (infos.isEmpty()) {
53       configurations.add(createFromElement(context, annotatedElement));
54     } else {
55       for (CrossProfileConfigurationAnnotationInfo info : infos) {
56         configurations.add(createFromAnnotationInfo(info, annotatedElement));
57       }
58     }
59 
60     return configurations.build();
61   }
62 
createFromElement( Context context, TypeElement annotatedElement)63   public static ValidatorCrossProfileConfigurationInfo createFromElement(
64       Context context, TypeElement annotatedElement) {
65     CrossProfileConfigurationAnnotationInfo annotationInfo =
66         extractFromCrossProfileConfigurationAnnotation(context, annotatedElement);
67 
68     return createFromAnnotationInfo(annotationInfo, annotatedElement);
69   }
70 
createFromAnnotationInfo( CrossProfileConfigurationAnnotationInfo annotationInfo, TypeElement annotatedElement)71   private static ValidatorCrossProfileConfigurationInfo createFromAnnotationInfo(
72       CrossProfileConfigurationAnnotationInfo annotationInfo, TypeElement annotatedElement) {
73     ClassName serviceSuperclass =
74         serviceSuperclassIsDefault(annotationInfo.serviceSuperclass())
75             ? SERVICE_CLASSNAME
76             : ClassName.get(annotationInfo.serviceSuperclass());
77 
78     TypeElement serviceClass =
79         serviceClassIsDefault(annotationInfo.serviceClass()) ? null : annotationInfo.serviceClass();
80 
81     Optional<TypeElement> connector =
82         connectorIsDefault(annotationInfo.connector())
83             ? Optional.empty()
84             : Optional.of(annotationInfo.connector());
85 
86     return new AutoValue_ValidatorCrossProfileConfigurationInfo(
87         annotatedElement,
88         ImmutableSet.copyOf(annotationInfo.providerClasses()),
89         serviceSuperclass,
90         Optional.ofNullable(serviceClass),
91         connector);
92   }
93 
serviceSuperclassIsDefault(TypeElement serviceSuperclass)94   private static boolean serviceSuperclassIsDefault(TypeElement serviceSuperclass) {
95     // CrossProfileConfiguration.class is the default specified serviceSuperclass
96     return serviceSuperclass
97         .asType()
98         .toString()
99         .equals(CrossProfileConfiguration.class.getCanonicalName());
100   }
101 
serviceClassIsDefault(TypeElement serviceClass)102   private static boolean serviceClassIsDefault(TypeElement serviceClass) {
103     // CrossProfileConfiguration.class is the default specified serviceClass
104     return serviceClass
105         .asType()
106         .toString()
107         .equals(CrossProfileConfiguration.class.getCanonicalName());
108   }
109 
connectorIsDefault(TypeElement connector)110   private static boolean connectorIsDefault(TypeElement connector) {
111     // CrossProfileConfiguration.class is the default specified connector
112     return connector.asType().toString().equals(CrossProfileConfiguration.class.getCanonicalName());
113   }
114 
115   private static CrossProfileConfigurationAnnotationInfo
extractFromCrossProfileConfigurationAnnotation(Context context, Element annotatedElement)116       extractFromCrossProfileConfigurationAnnotation(Context context, Element annotatedElement) {
117     return AnnotationFinder.extractCrossProfileConfigurationAnnotationInfo(
118         context, annotatedElement);
119   }
120 }
121