• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.aliasof;
18 
19 import androidx.room.compiler.processing.XAnnotation;
20 import androidx.room.compiler.processing.XElement;
21 import androidx.room.compiler.processing.XProcessingEnv;
22 import androidx.room.compiler.processing.XType;
23 import androidx.room.compiler.processing.XTypeElement;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableSet;
26 import com.squareup.javapoet.ClassName;
27 import dagger.hilt.processor.internal.BaseProcessingStep;
28 import dagger.hilt.processor.internal.ClassNames;
29 import dagger.hilt.processor.internal.ProcessorErrors;
30 import dagger.internal.codegen.extension.DaggerStreams;
31 import dagger.internal.codegen.xprocessing.XElements;
32 
33 /** Processes the annotations annotated with {@link dagger.hilt.migration.AliasOf} */
34 public final class AliasOfProcessingStep extends BaseProcessingStep {
35 
AliasOfProcessingStep(XProcessingEnv env)36   public AliasOfProcessingStep(XProcessingEnv env) {
37     super(env);
38   }
39 
40   @Override
annotationClassNames()41   public ImmutableSet<ClassName> annotationClassNames() {
42     return ImmutableSet.of(ClassNames.ALIAS_OF);
43   }
44 
45   @Override
processEach(ClassName annotation, XElement element)46   public void processEach(ClassName annotation, XElement element) {
47     ProcessorErrors.checkState(
48         element.hasAnnotation(ClassNames.SCOPE),
49         element,
50         "%s should only be used on scopes." + " However, it was found annotating %s",
51         annotation.simpleName(),
52         XElements.toStableString(element));
53 
54     XAnnotation xAnnotation = element.getAnnotation(ClassNames.ALIAS_OF);
55 
56     ImmutableList<XTypeElement> defineComponentScopes =
57         xAnnotation.getAsTypeList("value").stream()
58             .map(XType::getTypeElement)
59             .collect(DaggerStreams.toImmutableList());
60 
61     ProcessorErrors.checkState(
62         defineComponentScopes.size() >= 1,
63         element,
64         "@AliasOf annotation %s must declare at least one scope to alias.",
65         xAnnotation.getClassName());
66 
67     new AliasOfPropagatedDataGenerator(XElements.asTypeElement(element), defineComponentScopes)
68         .generate();
69   }
70 }
71