Skip to content

Commit

Permalink
fix issue where Lazy and Provider were not working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
sigpwned committed Jan 14, 2025
1 parent 2efdf4e commit d5bec8f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package rapier.assumptions.dagger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
Expand Down Expand Up @@ -65,7 +66,7 @@ public Integer provideInteger() {
final String errors = compileSourceCode(componentSourceCode, moduleSourceCode);

assertTrue(errors.isBlank(), "Expected no errors, but errors were found.");

// TODO After we refactor DaggerTestBase, verify no warnings here
}

Expand Down Expand Up @@ -189,4 +190,51 @@ public static void main(String[] args) {
assertThrowsExactly(NullPointerException.class,
() -> compileAndRunSourceCode(componentSourceCode, moduleSourceCode, appSourceCode));
}


/**
* Verify that Dagger will satisfy a primitve dependency with a binding of its boxed equivalent
*/
@Test
public void givenIntDependencyAndIntegerBinding_whenCompileAndRun_thenNullError()
throws IOException {
final String componentSourceCode = """
import dagger.Component;
import javax.inject.Provider;
import javax.annotation.Nullable;
@Component(modules={ExampleModule.class})
public interface ExampleComponent {
public int provisionInt();
}
""";

final String moduleSourceCode = """
import dagger.Module;
import dagger.Provides;
import javax.annotation.Nullable;
@Module
public class ExampleModule {
@Provides
public Integer provideInteger() {
return 123;
}
}
""";

final String appSourceCode = """
public class ExampleApp {
public static void main(String[] args) {
ExampleComponent c = DaggerExampleComponent.builder().build();
System.out.println(c.provisionInt());
}
}
""";

final String output =
compileAndRunSourceCode(componentSourceCode, moduleSourceCode, appSourceCode).trim();

assertEquals(output, "123");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,11 @@ private Optional<DaggerInjectionSite> newInjectionSite(DaggerInjectionSiteType s

final TypeMirror provisionedErasure = getTypes().erasure(provisionedType);
final TypeMirror literalJavaxInjectProviderType =
getElements().getTypeElement("javax.inject.Provider").asType();
getTypes().erasure(getElements().getTypeElement("javax.inject.Provider").asType());
final TypeMirror literalJakaInjectProviderType =
getElements().getTypeElement("jakarta.inject.Provider").asType();
final TypeMirror literalDaggerLazyType = getElements().getTypeElement("dagger.Lazy").asType();
getTypes().erasure(getElements().getTypeElement("jakarta.inject.Provider").asType());
final TypeMirror literalDaggerLazyType =
getTypes().erasure(getElements().getTypeElement("dagger.Lazy").asType());
final TypeMirror literalJavaUtilOptionalType =
getElements().getTypeElement("java.util.Optional").asType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public interface ExampleComponent {
@rapier.envvar.EnvironmentVariable("BOOLEAN")
public boolean provisionBooleanAsBoolean();
@rapier.envvar.EnvironmentVariable("STRING")
public dagger.Lazy<String> provisionLazyOfString();
@rapier.envvar.EnvironmentVariable("STRING")
public javax.inject.Provider<String> provisionProviderOfString();
}
""");

Expand Down Expand Up @@ -198,6 +204,8 @@ public static void main(String[] args) {
System.out.println(component.provisionStringAsSingleArgumentConstructorExample());
System.out.println(component.provisionBooleanAsBoxedBoolean());
System.out.println(component.provisionBooleanAsBoolean());
System.out.println(component.provisionLazyOfString().get());
System.out.println(component.provisionProviderOfString().get());
}
}
""");
Expand Down Expand Up @@ -231,7 +239,9 @@ public static void main(String[] args) {
ValueOfExample [s=xyz]
SingleArgumentConstructorExample [s=xyz]
true
true""", output);
true
xyz
xyz""", output);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SystemPropertyProcessorConversionTest extends RapierTestBase {
public void test() throws IOException {
final JavaFileObject componentSource = prepareSourceFile("""
package com.example;
@dagger.Component(modules = {RapierExampleComponentSystemPropertyModule.class})
public interface ExampleComponent {
@rapier.sysprop.SystemProperty("INT")
Expand Down Expand Up @@ -103,6 +103,12 @@ public interface ExampleComponent {
@rapier.sysprop.SystemProperty("BOOLEAN")
public boolean provisionBooleanAsBoolean();
@rapier.sysprop.SystemProperty("STRING")
public dagger.Lazy<String> provisionLazyOfString();
@rapier.sysprop.SystemProperty("STRING")
public javax.inject.Provider<String> provisionProviderOfString();
}
""");

Expand Down Expand Up @@ -198,6 +204,8 @@ public static void main(String[] args) {
System.out.println(component.provisionStringAsSingleArgumentConstructorExample());
System.out.println(component.provisionBooleanAsBoxedBoolean());
System.out.println(component.provisionBooleanAsBoolean());
System.out.println(component.provisionLazyOfString().get());
System.out.println(component.provisionProviderOfString().get());
}
}
""");
Expand Down Expand Up @@ -231,7 +239,9 @@ public static void main(String[] args) {
ValueOfExample [s=xyz]
SingleArgumentConstructorExample [s=xyz]
true
true""", output);
true
xyz
xyz""", output);
}

/**
Expand Down

0 comments on commit d5bec8f

Please sign in to comment.