1 /* 2 * Copyright (C) 2022 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.internal.codegen.xprocessing; 18 19 import static dagger.internal.codegen.xprocessing.XElements.getSimpleName; 20 21 import androidx.room.compiler.processing.XExecutableParameterElement; 22 import androidx.room.compiler.processing.XType; 23 import androidx.room.compiler.processing.XTypeElement; 24 import com.squareup.javapoet.ParameterSpec; 25 import com.squareup.javapoet.TypeSpec; 26 27 // TODO(bcorso): Consider moving these methods into XProcessing library. 28 /** A utility class for JavaPoet types to interface with XProcessing types. */ 29 public final class JavaPoetExt { 30 31 /** 32 * Configures the given {@link TypeSpec.Builder} so that it fully qualifies all classes nested in 33 * the given {@link XTypeElement} and all classes nested within any super type of the given {@link 34 * XTypeElement}. 35 * 36 * @see TypeSpec.Builder#avoidClashesWithNestedClasses(Class) 37 */ avoidClashesWithNestedClasses( TypeSpec.Builder builder, XTypeElement typeElement)38 public static TypeSpec.Builder avoidClashesWithNestedClasses( 39 TypeSpec.Builder builder, XTypeElement typeElement) { 40 typeElement 41 .getEnclosedTypeElements() 42 .forEach(nestedTypeElement -> builder.alwaysQualify(getSimpleName(nestedTypeElement))); 43 44 typeElement.getType().getSuperTypes().stream() 45 .filter(XTypes::isDeclared) 46 .map(XType::getTypeElement) 47 .forEach(superType -> avoidClashesWithNestedClasses(builder, superType)); 48 49 return builder; 50 } 51 toParameterSpec(XExecutableParameterElement param)52 public static ParameterSpec toParameterSpec(XExecutableParameterElement param) { 53 return ParameterSpec.builder(param.getType().getTypeName(), param.getJvmName()).build(); 54 } 55 JavaPoetExt()56 private JavaPoetExt() {} 57 } 58