From 4593c0acd3c18564689edfc93c2fde7408275d4f Mon Sep 17 00:00:00 2001 From: Kuan-Ying Chou Date: Wed, 11 Oct 2023 09:18:31 -0700 Subject: [PATCH] Replace XElements.isFromJavaSource() and XElements.isFromKotlinSource() with the version in XProcessing This also fixes a Github issue where we use `XConverters.toKS()` to get the underlying KSAnnotated and its origin but it doesn't work for synthetic elements like SyntheticExecutableParameterElement. The XProcessing version fixes this by 1. Making these functions member functions of XMemberContainer so that we can skip the implementation in most element types and we can't miss any XMemberContainer subtypes. 2. Simply consider that the member container is from Kotlin if it's KspFileMemberContainer or KspSyntheticFileMemberContainer as these are generated from Kotlin top-level properties. RELNOTES=N/A PiperOrigin-RevId: 572595483 --- .../AndroidEntryPointMetadata.java | 3 +- .../internal/codegen/binding/Nullability.java | 3 +- .../codegen/xprocessing/XElements.java | 39 ------------------- 3 files changed, 2 insertions(+), 43 deletions(-) diff --git a/java/dagger/hilt/android/processor/internal/androidentrypoint/AndroidEntryPointMetadata.java b/java/dagger/hilt/android/processor/internal/androidentrypoint/AndroidEntryPointMetadata.java index 59a90f1659c..71ad88eee37 100644 --- a/java/dagger/hilt/android/processor/internal/androidentrypoint/AndroidEntryPointMetadata.java +++ b/java/dagger/hilt/android/processor/internal/androidentrypoint/AndroidEntryPointMetadata.java @@ -23,7 +23,6 @@ import static dagger.hilt.processor.internal.HiltCompilerOptions.isAndroidSuperClassValidationDisabled; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; -import static dagger.internal.codegen.xprocessing.XElements.isFromKotlinSource; import static dagger.internal.codegen.xprocessing.XTypes.isDeclared; import androidx.room.compiler.processing.XAnnotation; @@ -159,7 +158,7 @@ public ParameterSpec componentManagerParam() { public Modifier[] generatedClassModifiers() { // Note XElement#isPublic() refers to the jvm visibility. Since "internal" visibility is // represented as public in the jvm, we have to check XElement#isInternal() explicitly. - return isFromKotlinSource(element()) && element().isPublic() && !element().isInternal() + return element().isFromKotlin() && element().isPublic() && !element().isInternal() ? new Modifier[] {Modifier.ABSTRACT, Modifier.PUBLIC} : new Modifier[] {Modifier.ABSTRACT}; } diff --git a/java/dagger/internal/codegen/binding/Nullability.java b/java/dagger/internal/codegen/binding/Nullability.java index 9ad9b83ea2b..82914222667 100644 --- a/java/dagger/internal/codegen/binding/Nullability.java +++ b/java/dagger/internal/codegen/binding/Nullability.java @@ -22,7 +22,6 @@ import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName; import static dagger.internal.codegen.xprocessing.XElements.asMethod; import static dagger.internal.codegen.xprocessing.XElements.asVariable; -import static dagger.internal.codegen.xprocessing.XElements.isFromJavaSource; import androidx.room.compiler.processing.XAnnotation; import androidx.room.compiler.processing.XElement; @@ -62,7 +61,7 @@ public static Nullability of(XElement element) { * explicit {@code ?} usages on kotlin types. */ private static boolean isKotlinTypeNullable(XElement element) { - if (isFromJavaSource(element)) { + if (element.getClosestMemberContainer().isFromJava()) { // Note: Technically, it isn't possible for Java sources to have nullable types like in Kotlin // sources, but for some reason KSP treats certain types as nullable if they have a // specific @Nullable (TYPE_USE target) annotation. Thus, to avoid inconsistencies with KAPT, diff --git a/java/dagger/internal/codegen/xprocessing/XElements.java b/java/dagger/internal/codegen/xprocessing/XElements.java index d48834bb9a3..552be65f9e9 100644 --- a/java/dagger/internal/codegen/xprocessing/XElements.java +++ b/java/dagger/internal/codegen/xprocessing/XElements.java @@ -49,7 +49,6 @@ import androidx.room.compiler.processing.XVariableElement; import com.google.common.collect.ImmutableSet; import com.google.devtools.ksp.symbol.KSAnnotated; -import com.google.devtools.ksp.symbol.Origin; import com.squareup.javapoet.ClassName; import java.util.Collection; import java.util.Optional; @@ -384,43 +383,5 @@ public static String packageName(XElement element) { return element.getClosestMemberContainer().asClassName().getPackageName(); } - /** Returns true if the source of the given type element is Java. */ - public static boolean isFromJavaSource(XElement element) { - XProcessingEnv processingEnv = getProcessingEnv(element); - switch (processingEnv.getBackend()) { - case KSP: - // Check the origin of the declaration to determine the source kind. - Origin origin = toKS(element).getOrigin(); - return origin == Origin.JAVA || origin == Origin.JAVA_LIB; - case JAVAC: - // If the element has kotlin metadata then the source is Kotlin. - return !hasKotlinMetadata(element); - } - throw new AssertionError("Unhandled backend kind: " + processingEnv.getBackend()); - } - - /** Returns true if the source of the given type element is Kotlin. */ - public static boolean isFromKotlinSource(XElement element) { - XProcessingEnv processingEnv = getProcessingEnv(element); - switch (processingEnv.getBackend()) { - case KSP: - // Check the origin of the declaration to determine the source kind. - Origin origin = toKS(element).getOrigin(); - return origin == Origin.KOTLIN || origin == Origin.KOTLIN_LIB; - case JAVAC: - // If the element has kotlin metadata then the source is Kotlin. - return hasKotlinMetadata(element); - } - throw new AssertionError("Unhandled backend kind: " + processingEnv.getBackend()); - } - - /** - * Returns {@code true} if the given element (or its nearest enclosing type element) is annotated - * with {@code kotlin.Metadata}. - */ - private static boolean hasKotlinMetadata(XElement element) { - return closestEnclosingTypeElement(element).hasAnnotation(ClassName.get("kotlin", "Metadata")); - } - private XElements() {} }