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.toImmutableList; 20 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 21 22 import androidx.room.compiler.processing.XProcessingEnv; 23 import androidx.room.compiler.processing.XTypeElement; 24 import com.google.common.collect.ImmutableCollection; 25 import com.google.common.collect.ImmutableSet; 26 import com.squareup.javapoet.ClassName; 27 import dagger.internal.codegen.xprocessing.XAnnotations; 28 import java.util.Optional; 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(XTypeElement aggregatedElement)34 public static Optional<ClassName> aggregatedElementProxyName(XTypeElement aggregatedElement) { 35 if (aggregatedElement.isPublic() && !aggregatedElement.isInternal()) { 36 // Public aggregated elements do not have proxies. 37 return Optional.empty(); 38 } 39 ClassName name = aggregatedElement.getClassName(); 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( ImmutableCollection<XTypeElement> aggregatedElements)45 public static ImmutableSet<XTypeElement> unwrapProxies( 46 ImmutableCollection<XTypeElement> aggregatedElements) { 47 return aggregatedElements.stream() 48 .map(AggregatedElements::unwrapProxy) 49 .collect(toImmutableSet()); 50 } 51 unwrapProxy(XTypeElement element)52 private static XTypeElement unwrapProxy(XTypeElement element) { 53 return element.hasAnnotation(ClassNames.AGGREGATED_ELEMENT_PROXY) 54 ? XAnnotations.getAsTypeElement( 55 element.getAnnotation(ClassNames.AGGREGATED_ELEMENT_PROXY), "value") 56 : element; 57 } 58 59 /** Returns all aggregated elements in the aggregating package after validating them. */ from( String aggregatingPackage, ClassName aggregatingAnnotation, XProcessingEnv env)60 public static ImmutableSet<XTypeElement> from( 61 String aggregatingPackage, ClassName aggregatingAnnotation, XProcessingEnv env) { 62 ImmutableSet<XTypeElement> aggregatedElements = 63 env.getTypeElementsFromPackage(aggregatingPackage).stream() 64 // We're only interested in returning the original deps here. Proxies will be generated 65 // (if needed) and swapped just before generating @ComponentTreeDeps. 66 .filter(element -> !element.hasAnnotation(ClassNames.AGGREGATED_ELEMENT_PROXY)) 67 .collect(toImmutableSet()); 68 69 for (XTypeElement aggregatedElement : aggregatedElements) { 70 ProcessorErrors.checkState( 71 aggregatedElement.hasAnnotation(aggregatingAnnotation), 72 aggregatedElement, 73 "Expected element, %s, to be annotated with @%s, but only found: %s.", 74 aggregatedElement.getName(), 75 aggregatingAnnotation, 76 aggregatedElement.getAllAnnotations().stream() 77 .map(XAnnotations::toStableString) 78 .collect(toImmutableList())); 79 } 80 81 return aggregatedElements; 82 } 83 AggregatedElements()84 private AggregatedElements() {} 85 } 86