Skip to content

Commit

Permalink
Make Nullability#nullableAnnotations return a set of ClassNames i…
Browse files Browse the repository at this point in the history
…nstead of `XAnnotation`s

All uses of this API immediately convert to `ClassName`, and this makes it
easier to deduplicate the returned set.

RELNOTES=N/A
PiperOrigin-RevId: 580220708
  • Loading branch information
cushon authored and Dagger Team committed Nov 7, 2023
1 parent 0323371 commit 6278d31
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ private static ImmutableList<String> formatedAnnotations(XExecutableElement exec
Nullability nullability = Nullability.of(executableElement);
if (nullability.isKotlinTypeNullable()
&& nullability.nullableAnnotations().stream()
.map(XAnnotations::getClassName)
.noneMatch(JET_BRAINS_NULLABLE::equals)
&& getProcessingEnv(executableElement).findTypeElement(JET_BRAINS_NULLABLE) != null) {
formattedAnnotations =
Expand Down
17 changes: 12 additions & 5 deletions java/dagger/internal/codegen/binding/Nullability.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static androidx.room.compiler.processing.XElementKt.isMethod;
import static androidx.room.compiler.processing.XElementKt.isVariableElement;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName;
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
import static dagger.internal.codegen.xprocessing.XElements.asVariable;

Expand All @@ -29,6 +28,9 @@
import androidx.room.compiler.processing.XType;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.internal.codegen.xprocessing.XAnnotations;
import java.util.stream.Stream;

/**
* Contains information about the nullability of an element.
Expand All @@ -49,9 +51,14 @@ public abstract class Nullability {
public static Nullability of(XElement element) {
return new AutoValue_Nullability(
/* isKotlinTypeNullable= */ isKotlinTypeNullable(element),
/* nullableAnnotations= */ element.getAllAnnotations().stream()
.filter(annotation -> getClassName(annotation).simpleName().contentEquals("Nullable"))
.collect(toImmutableSet()));
/* nullableAnnotations= */ getNullableAnnotations(element.getAllAnnotations().stream()));
}

private static ImmutableSet<ClassName> getNullableAnnotations(Stream<XAnnotation> annotations) {
return annotations
.map(XAnnotations::getClassName)
.filter(annotation -> annotation.simpleName().contentEquals("Nullable"))
.collect(toImmutableSet());
}

/**
Expand Down Expand Up @@ -82,7 +89,7 @@ private static boolean isKotlinTypeNullable(XType type) {

public abstract boolean isKotlinTypeNullable();

public abstract ImmutableSet<XAnnotation> nullableAnnotations();
public abstract ImmutableSet<ClassName> nullableAnnotations();

public final boolean isNullable() {
return isKotlinTypeNullable() || !nullableAnnotations().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import dagger.internal.codegen.compileroption.CompilerOptions;
import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation;
import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression;
import dagger.internal.codegen.xprocessing.XAnnotations;

/**
* A {@link javax.inject.Provider} creation expression for a provision method on a component's
Expand Down Expand Up @@ -107,8 +106,6 @@ public CodeBlock creationExpression() {
binding
.nullability()
.nullableAnnotations()
.stream()
.map(XAnnotations::getClassName)
.forEach(getMethod::addAnnotation);

// We need to use the componentShard here since the generated type is static and shards are
Expand Down
3 changes: 0 additions & 3 deletions java/dagger/internal/codegen/writing/FactoryGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import dagger.internal.codegen.model.Scope;
import dagger.internal.codegen.writing.InjectionMethods.InjectionSiteMethod;
import dagger.internal.codegen.writing.InjectionMethods.ProvisionMethod;
import dagger.internal.codegen.xprocessing.XAnnotations;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -270,8 +269,6 @@ private MethodSpec getMethod(ProvisionBinding binding) {
binding
.nullability()
.nullableAnnotations()
.stream()
.map(XAnnotations::getClassName)
.forEach(getMethod::addAnnotation);
getMethod.addStatement("return $L", invokeNewInstance);
} else if (!binding.injectionSites().isEmpty()) {
Expand Down
2 changes: 0 additions & 2 deletions java/dagger/internal/codegen/writing/InjectionMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ private static MethodSpec methodProxy(
} else {
Nullability.of(method)
.nullableAnnotations()
.stream()
.map(XAnnotations::getClassName)
.forEach(builder::addAnnotation);
return builder
.returns(method.getReturnType().getTypeName())
Expand Down

0 comments on commit 6278d31

Please sign in to comment.