From 11ab714207abb1f48688cea024ba23739a4af166 Mon Sep 17 00:00:00 2001 From: Xavier Garabito Date: Fri, 31 Jul 2026 17:00:58 -0700 Subject: [PATCH] Internal changes RELNOTES=N/A PiperOrigin-RevId: 957398461 --- .../internal/codegen/xprocessing/XTypes.java | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypes.java b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypes.java index 43404b507d2..9164e3cc019 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypes.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/xprocessing/XTypes.java @@ -26,6 +26,7 @@ import static com.google.common.base.CaseFormat.LOWER_CAMEL; import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Iterables.getOnlyElement; import static dagger.internal.codegen.extension.DaggerCollectors.toOptional; @@ -190,9 +191,9 @@ public static boolean isSubtype(XType type1, XType type2) { .isSubtype(toJavac(type1), toJavac(type2)); case KSP: if (isPrimitive(type1) || isPrimitive(type2)) { - // For primitive types we can't just check isAssignableTo since auto-boxing means boxed - // types are assignable to primitive (and vice versa) though neither are subtypes. - return type1.isSameType(type2); + // For primitive types we can't just check isAssignableTo since auto-boxing means boxed + // types are assignable to primitive (and vice versa) though neither are subtypes. + return type1.isSameType(type2); } return isAssignableTo(type1, type2); } @@ -207,7 +208,8 @@ public static TypeName erasedTypeName(XType type) { // The implementation used for KSP should technically also work in Javac but we avoid it to // avoid any possible regressions in Javac. return toXProcessing( - toJavac(processingEnv).getTypeUtils() // ALLOW_TYPES_ELEMENTS + toJavac(processingEnv) + .getTypeUtils() // ALLOW_TYPES_ELEMENTS .erasure(toJavac(type)), processingEnv) .getTypeName(); @@ -323,7 +325,8 @@ public static boolean isTypeOf(XType type, Collection classNames) { public static boolean isNullType(XType type) { XProcessingEnv.Backend backend = getProcessingEnv(type).getBackend(); switch (backend) { - case JAVAC: return toJavac(type).getKind().equals(TypeKind.NULL); + case JAVAC: + return toJavac(type).getKind().equals(TypeKind.NULL); // AFAICT, there's no way to actually get a "null" type in KSP's model case KSP: return false; @@ -341,8 +344,8 @@ public static boolean isNoType(XType type) { * *

In Java, this represents {@code ?} and {@code ? extends/super Foo}. * - *

In Kotlin, this represents explicit (a.k.a. use-site) variance, e.g. {@code *} or - * {@code out/in Foo}, but also includes types with implicit/effective variance, e.g. based on the + *

In Kotlin, this represents explicit (a.k.a. use-site) variance, e.g. {@code *} or {@code + * out/in Foo}, but also includes types with implicit/effective variance, e.g. based on the * declaration site variance of the original type parameter that this type argument represents. */ public static boolean isEffectivelyWildcard(XTypeArgument typeArgument) { @@ -578,9 +581,7 @@ public static XType requireInvariantType(XTypeArgument typeArgument) { */ public static void checkNotWildcard(XTypeArgument typeArgument) { checkArgument( - !isEffectivelyWildcard(typeArgument), - "Type argument is a wildcard: %s", - typeArgument); + !isEffectivelyWildcard(typeArgument), "Type argument is a wildcard: %s", typeArgument); } /** @@ -614,8 +615,7 @@ private static String toStableString(TypeName typeName) { if (typeName instanceof ClassName) { return ((ClassName) typeName).canonicalName(); } else if (typeName instanceof ArrayTypeName) { - return String.format( - "%s[]", toStableString(((ArrayTypeName) typeName).componentType)); + return String.format("%s[]", toStableString(((ArrayTypeName) typeName).componentType)); } else if (typeName instanceof ParameterizedTypeName) { ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName; return String.format( @@ -686,8 +686,8 @@ public static String getKindName(XType type) { } /** - * Iterates through the various types referenced within the given {@code type} and resolves it - * if needed. + * Iterates through the various types referenced within the given {@code type} and resolves it if + * needed. */ public static void resolveIfNeeded(XType type) { if (getProcessingEnv(type).getBackend() == XProcessingEnv.Backend.JAVAC) { @@ -702,13 +702,14 @@ public static void resolveIfNeeded(XType type) { * Returns the given {@code superTypeElement} as a member of the given {@code subType}. * *

For example, if we have {@code class A : B>} and {@code class B}: + * *

* - * @throws IllegalArgumentException if {@code superTypeElement} is not a supertype of - * {@code subType}. + * @throws IllegalArgumentException if {@code superTypeElement} is not a supertype of {@code + * subType}. */ public static XType asMemberOf(XTypeElement superTypeElement, XType subType) { if (superTypeElement.equals(subType.getTypeElement())) { @@ -724,8 +725,7 @@ public static XType asMemberOf(XTypeElement superTypeElement, XType subType) { throw new IllegalArgumentException( String.format( "%s is not a super type of %s", - XElements.toStableString(superTypeElement), - XTypes.toStableString(subType))); + XElements.toStableString(superTypeElement), XTypes.toStableString(subType))); } /** Returns {@code true} if the given type or any of its type arguments are type parameters. */ @@ -786,5 +786,14 @@ protected Void defaultAction(TypeMirror e, Set visited) { } } + public static XTypeElement requireTypeElement(XType type) { + XTypeElement typeElement = type.getTypeElement(); + checkNotNull( + typeElement, + "XType.getTypeElement() is required but got null for: %s", + toStableString(type)); + return typeElement; + } + private XTypes() {} }