Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public void bindsProvider() {

@Test
@IgnoreCodegen
@ReflectBug("check not implemented")
public void bindsProviderNullabilityMismatch() {
BindsProviderNullabilityMismatch component =
backend.create(BindsProviderNullabilityMismatch.class);
Expand Down Expand Up @@ -153,7 +152,7 @@ public void optionalBindingNullable() {
assertThat(e)
.hasMessageThat()
.isEqualTo(
"@Provides[com.example.OptionalBindingNullable$Module1.foo(…)] "
"@Nullable @Provides[com.example.OptionalBindingNullable$Module1.foo(…)] "
+ "returned null which is not allowed for optional bindings");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dagger.reflect;

import static dagger.reflect.Reflection.findQualifier;
import static dagger.reflect.Reflection.hasNullable;
import static dagger.reflect.Reflection.newProxy;

import dagger.MembersInjector;
Expand Down Expand Up @@ -89,7 +90,8 @@ private static ComponentInvocationHandler.MethodInvocationHandler createMethodIn
if (parameterTypes.length == 0) {
Key key = Key.of(findQualifier(method.getDeclaredAnnotations()), returnType);
LinkedBinding<?> binding = scope.getBinding(key);
return new ProvisionMethodInvocationHandler(binding);
return new ProvisionMethodInvocationHandler(
binding, hasNullable(method.getDeclaredAnnotations()));
}

if (parameterTypes.length == 1) {
Expand Down Expand Up @@ -123,13 +125,22 @@ private interface MethodInvocationHandler {

private static final class ProvisionMethodInvocationHandler implements MethodInvocationHandler {
private final LinkedBinding<?> binding;
private final boolean nullable;

ProvisionMethodInvocationHandler(LinkedBinding<?> binding) {
ProvisionMethodInvocationHandler(LinkedBinding<?> binding, boolean nullable) {
this.binding = binding;
this.nullable = nullable;
}

@Override
public @Nullable Object invoke(Object[] args) {
if (binding instanceof LinkedProvidesBinding) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BindsInstance could also have @Nullable can't it?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think there needs to be something like a isNullable on Binding until type annotations save us from this nonsense. And then anyone requesting a Binding will have to validate that the consumer can handle null.

LinkedProvidesBinding binding = (LinkedProvidesBinding) this.binding;
if (!binding.nullableMatch(nullable)) {
// TODO add details about requestor.
throw new IllegalStateException(binding + " nullability did not match");
}
}
return binding.get();
}
}
Expand Down
26 changes: 24 additions & 2 deletions reflect/src/main/java/dagger/reflect/LinkedProvidesBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ public final class LinkedProvidesBinding<T> extends LinkedBinding<T> {
private final @Nullable Object instance;
private final Method method;
private final LinkedBinding<?>[] dependencies;
private final boolean nullable;

LinkedProvidesBinding(@Nullable Object instance, Method method, LinkedBinding<?>[] dependencies) {
LinkedProvidesBinding(
@Nullable Object instance, Method method, LinkedBinding<?>[] dependencies, boolean nullable) {
this.instance = instance;
this.method = method;
this.dependencies = dependencies;
this.nullable = nullable;
}

@Override
Expand All @@ -26,11 +29,30 @@ public final class LinkedProvidesBinding<T> extends LinkedBinding<T> {
// The binding is associated with the return type of method as key.
@SuppressWarnings("unchecked")
T value = (T) tryInvoke(instance, method, arguments);
// We could add a check to ensure that value is null only if this Binding is annotated with
// nullable. However, we cannot because not all Nullable annotations have runtime retention so
// it would return false positives.
return value;
}

boolean nullableMatch(boolean nullable) {
if (this.nullable && !nullable) {
return false;
}
if (!this.nullable && nullable) {
return false;
}
return true;
}

@Override
public String toString() {
return "@Provides[" + method.getDeclaringClass().getName() + '.' + method.getName() + "(…)]";
String nullableString = nullable ? "@Nullable " : "";
return nullableString
+ "@Provides["
+ method.getDeclaringClass().getName()
+ '.'
+ method.getName()
+ "(…)]";
}
}
13 changes: 13 additions & 0 deletions reflect/src/main/java/dagger/reflect/Reflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ static boolean hasAnnotation(Annotation[] annotations, Class<? extends Annotatio
return findAnnotation(annotations, annotation) != null;
}

/**
* Check for any annotation which is named "Nullable". Warning: many Nullable annotations do not
* have runtime retention.
*/
static boolean hasNullable(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().getSimpleName().equals("Nullable")) {
return true;
}
}
return false;
}

@SuppressWarnings("StringConcatenationInLoop") // Only occurs when about to throw an exception.
static <T extends Annotation> T requireAnnotation(Class<?> cls, Class<T> annotationClass) {
T annotation = cls.getAnnotation(annotationClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dagger.reflect;

import static dagger.reflect.Reflection.findQualifier;
import static dagger.reflect.Reflection.hasNullable;

import dagger.reflect.Binding.LinkedBinding;
import java.lang.annotation.Annotation;
Expand All @@ -21,7 +22,9 @@ static void parse(Class<?> cls, Object instance, Scope.Builder scopeBuilder) {
Type type = method.getGenericReturnType();
Key key = Key.of(qualifier, type);

Binding binding = new LinkedProvidesBinding<>(instance, method, NO_BINDINGS);
Binding binding =
new LinkedProvidesBinding<>(
instance, method, NO_BINDINGS, hasNullable(method.getDeclaredAnnotations()));

scopeBuilder.addBinding(key, binding);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dagger.reflect;

import static dagger.reflect.Reflection.findQualifier;
import static dagger.reflect.Reflection.hasNullable;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand All @@ -25,7 +26,8 @@ public LinkedBinding<?> link(Linker linker, Scope scope) {
Key key = Key.of(findQualifier(parameterAnnotations[i]), parameterTypes[i]);
dependencies[i] = linker.get(key);
}
return new LinkedProvidesBinding<>(instance, method, dependencies);
return new LinkedProvidesBinding<>(
instance, method, dependencies, hasNullable(method.getDeclaredAnnotations()));
}

@Override
Expand Down