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.GeneratorUtilities.findCrossProfileMethodsInClass; 19 import static java.util.stream.Collectors.toList; 20 21 import com.google.android.enterprise.connectedapps.annotations.CrossProfile; 22 import com.google.android.enterprise.connectedapps.processor.SupportedTypes; 23 import com.google.android.enterprise.connectedapps.processor.annotationdiscovery.AnnotationFinder; 24 import com.google.auto.value.AutoValue; 25 import com.google.common.collect.ImmutableCollection; 26 import com.google.common.collect.ImmutableList; 27 import java.util.Comparator; 28 import java.util.List; 29 import java.util.Optional; 30 import javax.lang.model.element.ExecutableElement; 31 import javax.lang.model.element.TypeElement; 32 33 /** A wrapper around basic information from a {@link CrossProfile} type annotation. */ 34 @AutoValue 35 public abstract class ValidatorCrossProfileTypeInfo { 36 crossProfileTypeElement()37 public abstract TypeElement crossProfileTypeElement(); 38 crossProfileMethods()39 public abstract ImmutableList<ExecutableElement> crossProfileMethods(); 40 connectorInfo()41 public abstract Optional<ConnectorInfo> connectorInfo(); 42 supportedTypes()43 public abstract SupportedTypes supportedTypes(); 44 parcelableWrapperClasses()45 public abstract ImmutableCollection<TypeElement> parcelableWrapperClasses(); 46 futureWrapperClasses()47 public abstract ImmutableCollection<TypeElement> futureWrapperClasses(); 48 additionalUsedTypes()49 public abstract ImmutableCollection<TypeElement> additionalUsedTypes(); 50 isStatic()51 public abstract boolean isStatic(); 52 create( Context context, TypeElement crossProfileTypeElement, SupportedTypes globalSupportedTypes)53 public static ValidatorCrossProfileTypeInfo create( 54 Context context, TypeElement crossProfileTypeElement, SupportedTypes globalSupportedTypes) { 55 CrossProfileAnnotationInfo annotationInfo = 56 AnnotationFinder.extractCrossProfileAnnotationInfo(context, crossProfileTypeElement); 57 58 Optional<ConnectorInfo> connectorInfo = 59 createConnectorInfo(context, annotationInfo, globalSupportedTypes); 60 61 List<ExecutableElement> crossProfileMethodElements = 62 findCrossProfileMethodsInClass(crossProfileTypeElement).stream() 63 .sorted(Comparator.comparing(i -> i.getSimpleName().toString())) 64 .collect(toList()); 65 66 SupportedTypes incomingSupportedTypes = 67 connectorInfo.isPresent() ? connectorInfo.get().supportedTypes() : globalSupportedTypes; 68 69 SupportedTypes supportedTypes = 70 incomingSupportedTypes 71 .asBuilder() 72 .addParcelableWrappers( 73 ParcelableWrapper.createCustomParcelableWrappers( 74 context, annotationInfo.parcelableWrapperClasses())) 75 .addFutureWrappers( 76 FutureWrapper.createCustomFutureWrappers( 77 context, annotationInfo.futureWrapperClasses())) 78 .build(); 79 80 return new AutoValue_ValidatorCrossProfileTypeInfo( 81 crossProfileTypeElement, 82 ImmutableList.copyOf(crossProfileMethodElements), 83 connectorInfo, 84 supportedTypes, 85 annotationInfo.parcelableWrapperClasses(), 86 annotationInfo.futureWrapperClasses(), 87 annotationInfo.additionalUsedTypes(), 88 annotationInfo.isStatic()); 89 } 90 createConnectorInfo( Context context, CrossProfileAnnotationInfo annotationInfo, SupportedTypes globalSupportedTypes)91 private static Optional<ConnectorInfo> createConnectorInfo( 92 Context context, 93 CrossProfileAnnotationInfo annotationInfo, 94 SupportedTypes globalSupportedTypes) { 95 if (annotationInfo.connectorIsDefault()) { 96 return Optional.empty(); 97 } else if (ConnectorInfo.isProfileConnector(context, annotationInfo.connectorClass())) { 98 return Optional.of( 99 ConnectorInfo.forProfileConnector( 100 context, annotationInfo.connectorClass(), globalSupportedTypes)); 101 } else if (ConnectorInfo.isUserConnector(context, annotationInfo.connectorClass())) { 102 return Optional.of( 103 ConnectorInfo.forUserConnector( 104 context, annotationInfo.connectorClass(), globalSupportedTypes)); 105 } 106 107 return Optional.of( 108 ConnectorInfo.invalid(context, annotationInfo.connectorClass(), globalSupportedTypes)); 109 } 110 } 111