Skip to content

Commit

Permalink
Internal changes
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 591015561
  • Loading branch information
Chang-Eric authored and Dagger Team committed Dec 14, 2023
1 parent 5f8b76c commit 13d64c6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
7 changes: 5 additions & 2 deletions java/dagger/internal/codegen/binding/Nullability.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ public static Nullability of(XElement element) {
}

private static ImmutableSet<ClassName> getNullableAnnotations(XElement element) {
return getNullableAnnotations(element.getAllAnnotations().stream());
return getNullableAnnotations(element.getAllAnnotations().stream(), ImmutableSet.of());
}

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

Expand Down
8 changes: 6 additions & 2 deletions java/dagger/internal/codegen/writing/FactoryGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ private MethodSpec getMethod(ProvisionBinding binding) {
MethodSpec.Builder getMethod =
methodBuilder("get")
.addModifiers(PUBLIC)
.returns(providedTypeName)
.addParameters(assistedParameters.values());

if (factoryTypeName(binding).isPresent()) {
Expand All @@ -270,10 +269,12 @@ private MethodSpec getMethod(ProvisionBinding binding) {
.nullability()
.nullableAnnotations()
.forEach(getMethod::addAnnotation);
getMethod.returns(providedTypeName);
getMethod.addStatement("return $L", invokeNewInstance);
} else if (!binding.injectionSites().isEmpty()) {
CodeBlock instance = CodeBlock.of("instance");
getMethod
.returns(providedTypeName)
.addStatement("$T $L = $L", providedTypeName, instance, invokeNewInstance)
.addCode(
InjectionSiteMethod.invokeAll(
Expand All @@ -283,8 +284,11 @@ private MethodSpec getMethod(ProvisionBinding binding) {
binding.key().type().xprocessing(),
sourceFiles.frameworkFieldUsages(binding.dependencies(), frameworkFields)::get))
.addStatement("return $L", instance);

} else {
getMethod.addStatement("return $L", invokeNewInstance);
getMethod
.returns(providedTypeName)
.addStatement("return $L", invokeNewInstance);
}
return getMethod.build();
}
Expand Down
3 changes: 2 additions & 1 deletion java/dagger/internal/codegen/writing/InjectionMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ private static MethodSpec methodProxy(
if (isVoid(method.getReturnType())) {
return builder.addStatement("$L", invocation).build();
} else {
Nullability.of(method)
Nullability nullability = Nullability.of(method);
nullability
.nullableAnnotations()
.forEach(builder::addAnnotation);
return builder
Expand Down
71 changes: 62 additions & 9 deletions javatests/dagger/functional/nullables/JspecifyNullableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,100 @@
import dagger.Component;
import dagger.Module;
import dagger.Provides;
import javax.inject.Provider;
import org.jspecify.annotations.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class JspecifyNullableTest {
@Component(modules = MyModule.class)
@Component(modules = MyModule.class, dependencies = ComponentDependency.class)
interface MyComponent {
Integer getInt();
InnerType getInnerType();
Provider<Dependency> getDependencyProvider();
}

interface Dependency {}

interface InnerType {}

@Module
static class MyModule {
private final Integer value;
private final Integer integer;
private final InnerType innerType;

MyModule(Integer value) {
this.value = value;
MyModule(Integer integer, InnerType innerType) {
this.integer = integer;
this.innerType = innerType;
}

@Provides
@Nullable Integer provideInt() {
return value;
return integer;
}

@Provides
@Nullable InnerType provideInnerType() {
return innerType;
}
}

@Component(modules = DependencyModule.class)
interface ComponentDependency {
@Nullable Dependency dependency();
}

@Module
static class DependencyModule {
private final Dependency dependency;

DependencyModule(Dependency dependency) {
this.dependency = dependency;
}

@Provides
@Nullable Dependency provideDependency() {
return dependency;
}
}

@Test
public void testWithValue() {
MyComponent component =
DaggerJspecifyNullableTest_MyComponent.builder().myModule(new MyModule(15)).build();
MyComponent component = DaggerJspecifyNullableTest_MyComponent.builder()
.myModule(new MyModule(15, new InnerType() {}))
.componentDependency(
DaggerJspecifyNullableTest_ComponentDependency.builder()
.dependencyModule(new DependencyModule(new Dependency() {})).build())
.build();
assertThat(component.getInt()).isEqualTo(15);
assertThat(component.getInnerType()).isNotNull();
assertThat(component.getDependencyProvider().get()).isNotNull();
}

@Test
public void testWithNull() {
MyComponent component =
DaggerJspecifyNullableTest_MyComponent.builder().myModule(new MyModule(null)).build();
MyComponent component = DaggerJspecifyNullableTest_MyComponent.builder()
.myModule(new MyModule(null, null))
.componentDependency(
DaggerJspecifyNullableTest_ComponentDependency.builder()
.dependencyModule(new DependencyModule(null)).build())
.build();
NullPointerException expectedException =
assertThrows(NullPointerException.class, component::getInt);
assertThat(expectedException)
.hasMessageThat()
.contains("Cannot return null from a non-@Nullable @Provides method");
NullPointerException expectedException2 =
assertThrows(NullPointerException.class, component::getInnerType);
assertThat(expectedException2)
.hasMessageThat()
.contains("Cannot return null from a non-@Nullable @Provides method");
NullPointerException expectedException3 =
assertThrows(NullPointerException.class, () -> component.getDependencyProvider().get());
assertThat(expectedException3)
.hasMessageThat()
.contains("Cannot return null from a non-@Nullable @Provides method");
}
}

0 comments on commit 13d64c6

Please sign in to comment.