Skip to content

Commit

Permalink
Cast provider fields values to the right Provider class in BoundField…
Browse files Browse the repository at this point in the history
…Module.

Before this patch a ClassCastException would be thrown if a
javax.inject.Provider was used with @Bind, because the code would try to cast it
to com.google.inject.Provider.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=105098115
  • Loading branch information
timonvo authored and sameb committed Oct 28, 2015
1 parent 43d61da commit fcbc3e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ private void bindField(final BoundFieldInfo fieldInfo) {
}
// This is safe because we checked that the field's type is Provider above.
@SuppressWarnings("unchecked")
Provider<?> fieldValueUnsafe = (Provider<?>) getFieldValue(fieldInfo);
javax.inject.Provider<?> fieldValueUnsafe =
(javax.inject.Provider<?>) getFieldValue(fieldInfo);
binderUnsafe.toProvider(fieldValueUnsafe);
} else if (fieldInfo.bindAnnotation.lazy()) {
binderUnsafe.toProvider(new Provider<Object>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,22 @@ public void testBindingProvider() {
assertEquals(testValue, injector.getInstance(Integer.class));
}

public void testBindingJavaxProvider() {
final Integer testValue = 1024;
Object instance = new Object() {
@Bind private javax.inject.Provider<Integer> anInt = new javax.inject.Provider<Integer>() {
@Override public Integer get() {
return testValue;
}
};
};

BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);

assertEquals(testValue, injector.getInstance(Integer.class));
}

public void testBindingNullField() {
Object instance = new Object() {
@Bind private Integer anInt = null;
Expand Down

0 comments on commit fcbc3e0

Please sign in to comment.