• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 import com.google.auto.common.MoreElements;
22 import com.google.common.collect.ImmutableSet;
23 import com.squareup.javapoet.ClassName;
24 import javax.lang.model.element.PackageElement;
25 import javax.lang.model.element.TypeElement;
26 import javax.lang.model.util.Elements;
27 
28 /** Utility class for aggregating metadata. */
29 public final class AggregatedElements {
30 
31   /** Returns all aggregated elements in the aggregating package after validating them. */
from( String aggregatingPackage, ClassName aggregatingAnnotation, Elements elements)32   public static ImmutableSet<TypeElement> from(
33       String aggregatingPackage, ClassName aggregatingAnnotation, Elements elements) {
34     PackageElement packageElement = elements.getPackageElement(aggregatingPackage);
35 
36     if (packageElement == null) {
37       return ImmutableSet.of();
38     }
39 
40     ImmutableSet<TypeElement> aggregatedElements =
41         packageElement.getEnclosedElements().stream()
42             .map(MoreElements::asType)
43             .collect(toImmutableSet());
44 
45     ProcessorErrors.checkState(
46         !aggregatedElements.isEmpty(),
47         packageElement,
48         "No dependencies found. Did you remove code in package %s?",
49         packageElement);
50 
51     for (TypeElement aggregatedElement : aggregatedElements) {
52       ProcessorErrors.checkState(
53           Processors.hasAnnotation(aggregatedElement, aggregatingAnnotation),
54           aggregatedElement,
55           "Expected element, %s, to be annotated with @%s, but only found: %s.",
56           aggregatedElement.getSimpleName(),
57           aggregatingAnnotation,
58           aggregatedElement.getAnnotationMirrors());
59     }
60 
61     return aggregatedElements;
62   }
63 
AggregatedElements()64   private AggregatedElements() {}
65 }
66