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 20 import com.google.common.collect.ImmutableSet; 21 import com.google.common.collect.ImmutableSetMultimap; 22 import com.squareup.javapoet.ClassName; 23 import dagger.hilt.processor.internal.ProcessorErrors; 24 import javax.lang.model.util.Elements; 25 26 /** 27 * Extracts a multimap of aliases annotated with {@link dagger.hilt.migration.AliasOf} mapping them 28 * to scopes they are alias of. 29 */ 30 public final class AliasOfs { create(Elements elements, ImmutableSet<ClassName> defineComponentScopes)31 public static AliasOfs create(Elements elements, ImmutableSet<ClassName> defineComponentScopes) { 32 ImmutableSetMultimap.Builder<ClassName, ClassName> builder = ImmutableSetMultimap.builder(); 33 AliasOfPropagatedDataMetadata.from(elements) 34 .forEach( 35 metadata -> { 36 ClassName defineComponentScopeName = 37 ClassName.get(metadata.defineComponentScopeElement()); 38 ClassName aliasScopeName = ClassName.get(metadata.aliasElement()); 39 ProcessorErrors.checkState( 40 defineComponentScopes.contains(defineComponentScopeName), 41 metadata.aliasElement(), 42 "The scope %s cannot be an alias for %s. You can only have aliases of a scope" 43 + " defined directly on a @DefineComponent type.", 44 aliasScopeName, 45 defineComponentScopeName); 46 builder.put(defineComponentScopeName, aliasScopeName); 47 }); 48 return new AliasOfs(builder.build()); 49 } 50 51 private final ImmutableSetMultimap<ClassName, ClassName> defineComponentScopeToAliases; 52 AliasOfs(ImmutableSetMultimap<ClassName, ClassName> defineComponentScopeToAliases)53 private AliasOfs(ImmutableSetMultimap<ClassName, ClassName> defineComponentScopeToAliases) { 54 this.defineComponentScopeToAliases = defineComponentScopeToAliases; 55 } 56 getAliasesFor(ClassName defineComponentScope)57 public ImmutableSet<ClassName> getAliasesFor(ClassName defineComponentScope) { 58 return defineComponentScopeToAliases.get(defineComponentScope); 59 } 60 } 61