Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Linker and Scope toString + tests #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
@@ -0,0 +1,42 @@
package com.example;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import dagger.BindsOptionalOf;
import dagger.Component;
import dagger.Module;
import java.lang.annotation.Retention;
import java.util.Optional;
import javax.inject.Inject;
import javax.inject.Qualifier;
import javax.inject.Scope;
import javax.inject.Singleton;

@Singleton
@Component(modules = OptionalBindingWrongScope.Module1.class)
public interface OptionalBindingWrongScope {
@Qualified
Optional<Thing> thing();

@Module
abstract class Module1 {
@BindsOptionalOf
@Qualified
abstract Thing optionalThing();
}

@Qualified // @BindsOptionalOf cannot work with unqualified JIT, so a @Qualifier is needed
@Unrelated // mismatched scope (to Singleton) makes putJitBinding return null
final class Thing {
@Inject
Thing() {}
}

@Scope
@Retention(RUNTIME)
@interface Unrelated {}

@Qualifier
// @Retention(RUNTIME) missing, so that binding is not found
@interface Qualified {}
}
36 changes: 33 additions & 3 deletions integration-tests/src/test/java/com/example/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ public void optionalBindingNullable() {
}
}

@Test
@IgnoreCodegen
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually compiling and working in codegen, but this test would fail at fail() with codegen.
I couldn't find any repro other than OptionalBindingWrongScope where linker was not null, any ideas?

Copy link
Owner

Choose a reason for hiding this comment

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

I merged the first two commits while I think about this more.

I didn't realize qualifier annotations on types actually worked...

public void optionalBindingWrongScope() {
OptionalBindingWrongScope component = backend.create(OptionalBindingWrongScope.class);
try {
component.thing();
fail();
} catch (IllegalStateException e) {
assertThat(e)
.hasMessageThat()
.isEqualTo(
"Unable to find binding for key=com.example.OptionalBindingWrongScope$Thing"
+ " with linker=Linker with Scope[@javax.inject.Singleton()]\n"
+ " * Requested: java.util.Optional<com.example.OptionalBindingWrongScope$Thing>\n"
+ " from @Optional[com.example.OptionalBindingWrongScope$Module1.optionalThing(…)]\n");
}
}

@Test
public void optionalBindingAbsent() {
OptionalBindingAbsent component = backend.create(OptionalBindingAbsent.class);
Expand Down Expand Up @@ -245,7 +263,11 @@ public void justInTimeWrongScope() {
component.thing();
fail();
} catch (IllegalStateException e) {
// TODO assert some message
assertThat(e)
.hasMessageThat()
.isEqualTo(
"Unable to find binding for key=com.example.JustInTimeWrongScope$Thing"
+ " with linker=null");
}
}

Expand All @@ -257,7 +279,11 @@ public void justInTimeScopedIntoUnscoped() {
component.thing();
fail();
} catch (IllegalStateException e) {
// TODO assert some message
assertThat(e)
.hasMessageThat()
.isEqualTo(
"Unable to find binding for key=com.example.JustInTimeScopedIntoUnscoped$Thing"
+ " with linker=null");
}
}

Expand All @@ -270,7 +296,11 @@ public void justInTimeNotScopedInAncestry() {
child.thing();
fail();
} catch (IllegalStateException e) {
// TODO assert some message
assertThat(e)
.hasMessageThat()
.isEqualTo(
"Unable to find binding for key=com.example.JustInTimeNotScopedInAncestry$Thing"
+ " with linker=null");
}
}

Expand Down
16 changes: 14 additions & 2 deletions reflect/src/main/java/dagger/reflect/Linker.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ LinkedBinding<?> link(Key key, UnlinkedBinding unlinkedBinding) {

private RuntimeException failure(Key key, String reason, String cause) {
StringBuilder builder = new StringBuilder(reason).append(" for ").append(key).append('\n');
appendChain(builder);
builder.append(" * Requested: ").append(key).append("\n which ").append(cause).append('.');
throw new IllegalStateException(builder.toString());
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Linker with ").append(scope).append("\n");
appendChain(builder);
return builder.toString();
}

private void appendChain(StringBuilder builder) {
for (Map.Entry<Key, Binding> entry : chain.entrySet()) {
builder
.append(" * Requested: ")
Expand All @@ -48,7 +62,5 @@ private RuntimeException failure(Key key, String reason, String cause) {
.append(entry.getValue())
.append('\n');
}
builder.append(" * Requested: ").append(key).append("\n which ").append(cause).append('.');
throw new IllegalStateException(builder.toString());
}
}
10 changes: 6 additions & 4 deletions reflect/src/main/java/dagger/reflect/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ private Scope(
this.parent = parent;
}

@Override
public String toString() {
return "Scope" + annotations;
}

LinkedBinding<?> getBinding(Key key) {
LinkedBinding<?> binding = findBinding(key, null);
if (binding != null) {
Expand Down Expand Up @@ -76,10 +81,7 @@ LinkedBinding<?> findBinding(Key key, @Nullable Linker linker) {
LinkedBinding<?> jitBinding = putJitBinding(key, linker, jitLookup);
if (jitBinding == null) {
throw new IllegalStateException(
"Unable to find binding for key="
+ key
+ " and linker="
+ linker); // TODO nice error message with scope chain
"Unable to find binding for key=" + key + " with linker=" + linker);
}
return jitBinding;
}
Expand Down