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; 18 19 import com.squareup.javapoet.ClassName; 20 21 /** 22 * Utility class for getting the generated component name. 23 * 24 * <p>This should not be used externally. 25 */ 26 public final class ComponentNames { ComponentNames()27 private ComponentNames() {} 28 29 /** Returns the name of the generated component wrapper. */ generatedComponentsWrapper(ClassName root)30 public static ClassName generatedComponentsWrapper(ClassName root) { 31 return Processors.append(Processors.getEnclosedClassName(root), "_HiltComponents"); 32 } 33 34 /** Returns the name of the generated component. */ generatedComponent(ClassName root, ClassName component)35 public static ClassName generatedComponent(ClassName root, ClassName component) { 36 return generatedComponentsWrapper(root).nestedClass(componentName(component)); 37 } 38 39 /** 40 * Returns the shortened component name by replacing the ending "Component" with "C" if it exists. 41 * 42 * <p>This is a hack because nested subcomponents in Dagger generate extremely long class names 43 * that hit the 256 character limit. 44 */ 45 // TODO(bcorso): See if this issue can be fixed in Dagger, e.g. by using static subcomponents. componentName(ClassName component)46 private static String componentName(ClassName component) { 47 // TODO(bcorso): How do we want to handle collisions across packages? Currently, we only handle 48 // collisions across enclosing elements since namespacing by package would likely lead to too 49 // long of class names. 50 // Note: This uses regex matching so we only match if the name ends in "Component" 51 return Processors.getEnclosedName(component).replaceAll("Component$", "C"); 52 } 53 } 54