• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
20 
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.ImmutableSetMultimap;
23 import com.squareup.javapoet.ClassName;
24 import dagger.hilt.processor.internal.ComponentDescriptor;
25 import dagger.hilt.processor.internal.ProcessorErrors;
26 
27 /**
28  * Extracts a multimap of aliases annotated with {@link dagger.hilt.migration.AliasOf} mapping them
29  * to scopes they are alias of.
30  */
31 public final class AliasOfs {
create( ImmutableSet<AliasOfPropagatedDataMetadata> metadatas, ImmutableSet<ComponentDescriptor> componentDescriptors)32   public static AliasOfs create(
33       ImmutableSet<AliasOfPropagatedDataMetadata> metadatas,
34       ImmutableSet<ComponentDescriptor> componentDescriptors) {
35     ImmutableSet<ClassName> defineComponentScopes =
36         componentDescriptors.stream()
37             .flatMap(descriptor -> descriptor.scopes().stream())
38             .collect(toImmutableSet());
39 
40     ImmutableSetMultimap.Builder<ClassName, ClassName> builder = ImmutableSetMultimap.builder();
41     metadatas.forEach(
42         metadata -> {
43           ClassName aliasScopeName = metadata.aliasElement().getClassName();
44           metadata
45               .defineComponentScopeElements()
46               .forEach(
47                   defineComponentScope -> {
48                     ClassName defineComponentScopeName = defineComponentScope.getClassName();
49                     ProcessorErrors.checkState(
50                         defineComponentScopes.contains(defineComponentScopeName),
51                         metadata.aliasElement(),
52                         "The scope %s cannot be an alias for %s. You can only have aliases of a"
53                             + " scope defined directly on a @DefineComponent type.",
54                         aliasScopeName,
55                         defineComponentScopeName);
56                     builder.put(defineComponentScopeName, aliasScopeName);
57                   });
58         });
59     return new AliasOfs(builder.build());
60   }
61 
62   private final ImmutableSetMultimap<ClassName, ClassName> defineComponentScopeToAliases;
63 
AliasOfs(ImmutableSetMultimap<ClassName, ClassName> defineComponentScopeToAliases)64   private AliasOfs(ImmutableSetMultimap<ClassName, ClassName> defineComponentScopeToAliases) {
65     this.defineComponentScopeToAliases = defineComponentScopeToAliases;
66   }
67 
getAliasesFor(ClassName defineComponentScope)68   public ImmutableSet<ClassName> getAliasesFor(ClassName defineComponentScope) {
69     return defineComponentScopeToAliases.get(defineComponentScope);
70   }
71 }
72