1 /* 2 * Copyright (C) 2021 The Dagger Authors. 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 * http://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 17 package dagger.hilt.processor.internal; 18 19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 20 import static javax.lang.model.element.Modifier.PUBLIC; 21 22 import com.google.auto.common.MoreElements; 23 import com.google.common.collect.ImmutableSet; 24 import com.squareup.javapoet.ClassName; 25 import java.util.Optional; 26 import javax.lang.model.element.PackageElement; 27 import javax.lang.model.element.TypeElement; 28 import javax.lang.model.util.Elements; 29 30 /** Utility class for aggregating metadata. */ 31 public final class AggregatedElements { 32 33 /** Returns the class name of the proxy or {@link Optional#empty()} if a proxy is not needed. */ aggregatedElementProxyName(TypeElement aggregatedElement)34 public static Optional<ClassName> aggregatedElementProxyName(TypeElement aggregatedElement) { 35 if (aggregatedElement.getModifiers().contains(PUBLIC)) { 36 // Public aggregated elements do not have proxies. 37 return Optional.empty(); 38 } 39 ClassName name = ClassName.get(aggregatedElement); 40 // To avoid going over the class name size limit, just prepend a single character. 41 return Optional.of(name.peerClass("_" + name.simpleName())); 42 } 43 44 /** Returns back the set of input {@code aggregatedElements} with all proxies unwrapped. */ unwrapProxies( ImmutableSet<TypeElement> aggregatedElements, Elements elements)45 public static ImmutableSet<TypeElement> unwrapProxies( 46 ImmutableSet<TypeElement> aggregatedElements, Elements elements) { 47 return aggregatedElements.stream() 48 .map(aggregatedElement -> unwrapProxy(aggregatedElement, elements)) 49 .collect(toImmutableSet()); 50 } 51 unwrapProxy(TypeElement element, Elements elements)52 private static TypeElement unwrapProxy(TypeElement element, Elements elements) { 53 return Processors.hasAnnotation(element, ClassNames.AGGREGATED_ELEMENT_PROXY) 54 ? Processors.getAnnotationClassValue( 55 elements, 56 Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_ELEMENT_PROXY), 57 "value") 58 : element; 59 } 60 61 /** Returns all aggregated elements in the aggregating package after validating them. */ from( String aggregatingPackage, ClassName aggregatingAnnotation, Elements elements)62 public static ImmutableSet<TypeElement> from( 63 String aggregatingPackage, ClassName aggregatingAnnotation, Elements elements) { 64 PackageElement packageElement = elements.getPackageElement(aggregatingPackage); 65 66 if (packageElement == null) { 67 return ImmutableSet.of(); 68 } 69 70 ImmutableSet<TypeElement> aggregatedElements = 71 packageElement.getEnclosedElements().stream() 72 .map(MoreElements::asType) 73 // We're only interested in returning the original deps here. Proxies will be generated 74 // (if needed) and swapped just before generating @ComponentTreeDeps. 75 .filter( 76 element -> !Processors.hasAnnotation(element, ClassNames.AGGREGATED_ELEMENT_PROXY)) 77 .collect(toImmutableSet()); 78 79 ProcessorErrors.checkState( 80 !aggregatedElements.isEmpty(), 81 packageElement, 82 "No dependencies found. Did you remove code in package %s?", 83 packageElement); 84 85 for (TypeElement aggregatedElement : aggregatedElements) { 86 ProcessorErrors.checkState( 87 Processors.hasAnnotation(aggregatedElement, aggregatingAnnotation), 88 aggregatedElement, 89 "Expected element, %s, to be annotated with @%s, but only found: %s.", 90 aggregatedElement.getSimpleName(), 91 aggregatingAnnotation, 92 aggregatedElement.getAnnotationMirrors()); 93 } 94 95 return aggregatedElements; 96 } 97 AggregatedElements()98 private AggregatedElements() {} 99 } 100