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 java.util.stream.Collectors.toList; 19 20 import com.google.android.enterprise.connectedapps.annotations.CrossProfileCallback; 21 import com.google.auto.value.AutoValue; 22 import java.util.Comparator; 23 import java.util.List; 24 import java.util.Set; 25 import java.util.stream.Collectors; 26 import javax.lang.model.element.Element; 27 import javax.lang.model.element.ElementKind; 28 import javax.lang.model.element.ExecutableElement; 29 import javax.lang.model.element.Name; 30 import javax.lang.model.element.TypeElement; 31 import javax.lang.model.type.TypeMirror; 32 33 /** Wrapper of a {@link CrossProfileCallback} annotated interface. */ 34 @AutoValue 35 public abstract class CrossProfileCallbackInterfaceInfo { 36 interfaceElement()37 public abstract TypeElement interfaceElement(); 38 simpleName()39 public Name simpleName() { 40 return interfaceElement().getSimpleName(); 41 } 42 isSimple()43 public boolean isSimple() { 44 List<ExecutableElement> methods = methods(); 45 return methods.size() == 1 && methods.get(0).getParameters().size() < 2; 46 } 47 methods()48 public List<ExecutableElement> methods() { 49 return interfaceElement().getEnclosedElements().stream() 50 .filter(e -> e instanceof ExecutableElement) 51 .map(e -> (ExecutableElement) e) 52 .filter(e -> e.getKind() == ElementKind.METHOD) 53 .sorted(Comparator.comparing(e -> e.getSimpleName().toString())) 54 .collect(toList()); 55 } 56 getIdentifier(ExecutableElement method)57 public int getIdentifier(ExecutableElement method) { 58 return methods().indexOf(method); 59 } 60 61 /** Get all types used by methods on this interface. */ argumentTypes()62 public Set<TypeMirror> argumentTypes() { 63 return methods().stream() 64 .flatMap(m -> m.getParameters().stream()) 65 .map(Element::asType) 66 .collect(Collectors.toSet()); 67 } 68 create(TypeElement interfaceElement)69 public static CrossProfileCallbackInterfaceInfo create(TypeElement interfaceElement) { 70 return new AutoValue_CrossProfileCallbackInterfaceInfo(interfaceElement); 71 } 72 } 73