Skip to content

Commit

Permalink
Rename #java() to #javac() for Dagger SPI model types.
Browse files Browse the repository at this point in the history
This CL also updates the `javac()` and `ksp()` methods to throw if they are used with the wrong backend.

RELNOTES=N/A
PiperOrigin-RevId: 559565413
  • Loading branch information
bcorso authored and Dagger Team committed Aug 23, 2023
1 parent b7d97ba commit 7107431
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 107 deletions.
35 changes: 14 additions & 21 deletions java/dagger/hilt/processor/internal/DaggerModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ import com.squareup.javapoet.ClassName
import dagger.spi.model.DaggerAnnotation
import dagger.spi.model.DaggerElement
import dagger.spi.model.DaggerProcessingEnv
import dagger.spi.model.DaggerProcessingEnv.Backend.JAVAC
import dagger.spi.model.DaggerProcessingEnv.Backend.KSP
import dagger.spi.model.DaggerType


fun DaggerType.hasAnnotation(className: ClassName): Boolean =
when (checkNotNull(backend())) {
DaggerProcessingEnv.Backend.JAVAC -> {
val javaType = checkNotNull(java())
Processors.hasAnnotation(MoreTypes.asTypeElement(javaType), className)
}
DaggerProcessingEnv.Backend.KSP -> {
val kspType = checkNotNull(ksp())
kspType.declaration.hasAnnotation(className.canonicalName())
JAVAC -> {
val javac = javac()
Processors.hasAnnotation(MoreTypes.asTypeElement(javac), className)
}
KSP -> ksp().declaration.hasAnnotation(className.canonicalName())
}

fun KSDeclaration.hasAnnotation(annotationName: String): Boolean =
Expand All @@ -45,26 +44,20 @@ fun KSDeclaration.hasAnnotation(annotationName: String): Boolean =

fun DaggerElement.hasAnnotation(className: ClassName) =
when (checkNotNull(backend())) {
DaggerProcessingEnv.Backend.JAVAC -> {
val javaElement = checkNotNull(java())
Processors.hasAnnotation(javaElement, className)
}
DaggerProcessingEnv.Backend.KSP -> {
val kspAnnotated = checkNotNull(ksp())
kspAnnotated.hasAnnotation(className)
JAVAC -> {
val javac = javac()
Processors.hasAnnotation(javac, className)
}
KSP -> ksp().hasAnnotation(className)
}

fun DaggerAnnotation.getQualifiedName() =
when (checkNotNull(backend())) {
DaggerProcessingEnv.Backend.JAVAC -> {
val javaAnnotation = checkNotNull(java())
MoreTypes.asTypeElement(javaAnnotation.annotationType).qualifiedName.toString()
}
DaggerProcessingEnv.Backend.KSP -> {
val kspAnnotation = checkNotNull(ksp())
kspAnnotation.annotationType.resolve().declaration.qualifiedName!!.asString()
JAVAC -> {
val javac = javac()
MoreTypes.asTypeElement(javac.annotationType).qualifiedName.toString()
}
KSP -> ksp().annotationType.resolve().declaration.qualifiedName!!.asString()
}

private fun KSAnnotated.hasAnnotation(className: ClassName) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static androidx.room.compiler.processing.compat.XConverters.toKS;
import static androidx.room.compiler.processing.compat.XConverters.toKSResolver;
import static com.google.common.base.Preconditions.checkState;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
Expand Down Expand Up @@ -269,7 +270,7 @@ static ComponentNode create(
componentNode.isSubcomponent(),
componentNode.isRealComponent(),
componentNode.entryPoints().stream()
.map(request -> SpiModelBindingGraphConverter.toSpiModel(request))
.map(SpiModelBindingGraphConverter::toSpiModel)
.collect(toImmutableSet()),
componentNode.scopes().stream()
.map(SpiModelBindingGraphConverter::toSpiModel)
Expand All @@ -292,7 +293,7 @@ static Binding create(dagger.internal.codegen.model.Binding binding, XProcessing
toSpiModel(binding.key()),
toSpiModel(binding.componentPath()),
binding.dependencies().stream()
.map(request -> SpiModelBindingGraphConverter.toSpiModel(request))
.map(SpiModelBindingGraphConverter::toSpiModel)
.collect(toImmutableSet()),
binding.bindingElement().map(element -> toSpiModel(element.xprocessing())),
binding.contributingModule().map(module -> toSpiModel(module.xprocessing())),
Expand Down Expand Up @@ -436,12 +437,14 @@ public static DaggerElement from(XElement element) {
abstract XElement element();

@Override
public Element java() {
public Element javac() {
checkIsJavac(backend());
return toJavac(element());
}

@Override
public KSAnnotated ksp() {
checkIsKsp(backend());
return toKS(element());
}

Expand All @@ -465,12 +468,14 @@ public static DaggerTypeElement from(XTypeElement element) {
abstract XTypeElement element();

@Override
public TypeElement java() {
public TypeElement javac() {
checkIsJavac(backend());
return toJavac(element());
}

@Override
public KSClassDeclaration ksp() {
checkIsKsp(backend());
return toKS(element());
}

Expand All @@ -495,12 +500,14 @@ public static DaggerType from(XType type) {
abstract Equivalence.Wrapper<XType> type();

@Override
public TypeMirror java() {
public TypeMirror javac() {
checkIsJavac(backend());
return toJavac(type().get());
}

@Override
public KSType ksp() {
checkIsKsp(backend());
return toKS(type().get());
}

Expand Down Expand Up @@ -530,12 +537,14 @@ public DaggerTypeElement annotationTypeElement() {
}

@Override
public AnnotationMirror java() {
public AnnotationMirror javac() {
checkIsJavac(backend());
return toJavac(annotation().get());
}

@Override
public KSAnnotation ksp() {
checkIsKsp(backend());
return toKS(annotation().get());
}

Expand All @@ -560,12 +569,14 @@ public static DaggerExecutableElement from(XExecutableElement executableElement)
abstract XExecutableElement executableElement();

@Override
public ExecutableElement java() {
public ExecutableElement javac() {
checkIsJavac(backend());
return toJavac(executableElement());
}

@Override
public KSFunctionDeclaration ksp() {
checkIsKsp(backend());
return toKS(executableElement());
}

Expand All @@ -592,12 +603,14 @@ public static DaggerProcessingEnv from(XProcessingEnv env) {
}

@Override
public ProcessingEnvironment java() {
public ProcessingEnvironment javac() {
checkIsJavac(backend());
return toJavac(env);
}

@Override
public SymbolProcessorEnvironment ksp() {
checkIsKsp(backend());
return toKS(env);
}

Expand All @@ -612,6 +625,18 @@ public DaggerProcessingEnv.Backend backend() {
}
}

private static void checkIsJavac(DaggerProcessingEnv.Backend backend) {
checkState(
backend == DaggerProcessingEnv.Backend.JAVAC,
"Expected JAVAC backend but was: %s", backend);
}

private static void checkIsKsp(DaggerProcessingEnv.Backend backend) {
checkState(
backend == DaggerProcessingEnv.Backend.KSP,
"Expected KSP backend but was: %s", backend);
}

private static DaggerProcessingEnv.Backend getBackend(XProcessingEnv env) {
switch (env.getBackend()) {
case JAVAC:
Expand Down
23 changes: 0 additions & 23 deletions java/dagger/spi/model/CompilerEnvironment.java

This file was deleted.

26 changes: 11 additions & 15 deletions java/dagger/spi/model/DaggerAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.devtools.ksp.symbol.KSAnnotation;
import com.google.errorprone.annotations.DoNotMock;
import javax.annotation.Nullable;
import javax.lang.model.element.AnnotationMirror;

/** Wrapper type for an annotation. */
Expand All @@ -27,22 +26,19 @@ public abstract class DaggerAnnotation {
public abstract DaggerTypeElement annotationTypeElement();

/**
* java representation for the annotation, returns {@code null} if the annotation isn't a java
* element.
* Returns the Javac representation for the annotation.
*
* @throws IllegalStateException if the current backend isn't Javac.
*/
@Nullable
public abstract AnnotationMirror java();
public abstract AnnotationMirror javac();

/** KSP declaration for the annotation, returns {@code null} not using KSP. */
@Nullable
/**
* Returns the KSP representation for the annotation.
*
* @throws IllegalStateException if the current backend isn't KSP.
*/
public abstract KSAnnotation ksp();

public DaggerProcessingEnv.Backend backend() {
if (java() != null) {
return DaggerProcessingEnv.Backend.JAVAC;
} else if (ksp() != null) {
return DaggerProcessingEnv.Backend.KSP;
}
throw new AssertionError("Unexpected backend");
}
/** Returns the backend used in this compilation. */
public abstract DaggerProcessingEnv.Backend backend();
}
15 changes: 9 additions & 6 deletions java/dagger/spi/model/DaggerElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@

import com.google.devtools.ksp.symbol.KSAnnotated;
import com.google.errorprone.annotations.DoNotMock;
import javax.annotation.Nullable;
import javax.lang.model.element.Element;

/** Wrapper type for an element. */
@DoNotMock("Only use real implementations created by Dagger")
public abstract class DaggerElement {
/**
* Java representation for the element, returns {@code null} not using java annotation processor.
* Returns the Javac representation for the element.
*
* @throws IllegalStateException if the current backend isn't Javac.
*/
@Nullable
public abstract Element java();
public abstract Element javac();

/** KSP declaration for the element, returns {@code null} not using KSP. */
@Nullable
/**
* Returns the KSP representation for the element.
*
* @throws IllegalStateException if the current backend isn't KSP.
*/
public abstract KSAnnotated ksp();

/** Returns the backend used in this compilation. */
Expand Down
15 changes: 9 additions & 6 deletions java/dagger/spi/model/DaggerExecutableElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@

import com.google.devtools.ksp.symbol.KSFunctionDeclaration;
import com.google.errorprone.annotations.DoNotMock;
import javax.annotation.Nullable;
import javax.lang.model.element.ExecutableElement;

/** Wrapper type for an executable element. */
@DoNotMock("Only use real implementations created by Dagger")
public abstract class DaggerExecutableElement {
/**
* Java representation for the element, returns {@code null} not using java annotation processor.
* Returns the Javac representation for the executable element.
*
* @throws IllegalStateException if the current backend isn't Javac.
*/
@Nullable
public abstract ExecutableElement java();
public abstract ExecutableElement javac();

/** KSP declaration for the element, returns {@code null} not using KSP. */
@Nullable
/**
* Returns the KSP representation for the executable element.
*
* @throws IllegalStateException if the current backend isn't KSP.
*/
public abstract KSFunctionDeclaration ksp();

/** Returns the backend used in this compilation. */
Expand Down
23 changes: 14 additions & 9 deletions java/dagger/spi/model/DaggerProcessingEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.devtools.ksp.processing.Resolver;
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment;
import com.google.errorprone.annotations.DoNotMock;
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;

/** Wrapper type for an element. */
Expand All @@ -32,18 +31,24 @@ public enum Backend {
}

/**
* Java representation for the processing environment, returns {@code null} not using java
* annotation processor.
* Returns the Javac representation for the processing environment.
*
* @throws IllegalStateException if the current backend isn't Javac.
*/
@Nullable
public abstract ProcessingEnvironment java();
public abstract ProcessingEnvironment javac();

/** Ksp symbol processing environment hosting symbol processors. */
@Nullable
/**
* Returns the KSP representation for the processing environment.
*
* @throws IllegalStateException if the current backend isn't KSP.
*/
public abstract SymbolProcessorEnvironment ksp();

/** Ksp resolver provides [SymbolProcessor] with access to compiler details such as Symbols. */
@Nullable
/**
* Returns the KSP representation for the resolver.
*
* @throws IllegalStateException if the current backend isn't KSP.
*/
public abstract Resolver resolver();

/** Returns the backend used in this compilation. */
Expand Down
18 changes: 11 additions & 7 deletions java/dagger/spi/model/DaggerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@

import com.google.devtools.ksp.symbol.KSType;
import com.google.errorprone.annotations.DoNotMock;
import javax.annotation.Nullable;
import javax.lang.model.type.TypeMirror;

/** Wrapper type for a type. */
@DoNotMock("Only use real implementations created by Dagger")
public abstract class DaggerType {
/**
* Returns the Javac representation for the type.
*
* @throws IllegalStateException if the current backend isn't Javac.
*/
public abstract TypeMirror javac();

/** Java representation for the type, returns {@code null} not using java annotation processor. */
@Nullable
public abstract TypeMirror java();

/** KSP declaration for the type, returns {@code null} not using KSP. */
@Nullable
/**
* Returns the KSP representation for the type.
*
* @throws IllegalStateException if the current backend isn't KSP.
*/
public abstract KSType ksp();

/** Returns the backend used in this compilation. */
Expand Down
Loading

0 comments on commit 7107431

Please sign in to comment.