Skip to content

Commit

Permalink
Replace XElements.isFromJavaSource() and XElements.isFromKotlinSource…
Browse files Browse the repository at this point in the history
…() 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
  • Loading branch information
kuanyingchou authored and Dagger Team committed Oct 11, 2023
1 parent 3c8d28c commit 4593c0a
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 43 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
}
Expand Down
3 changes: 1 addition & 2 deletions java/dagger/internal/codegen/binding/Nullability.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 0 additions & 39 deletions java/dagger/internal/codegen/xprocessing/XElements.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {}
}

0 comments on commit 4593c0a

Please sign in to comment.