Fixed some di problems.
This commit is contained in:
parent
f4e44fe050
commit
ab4d006955
@ -166,6 +166,18 @@ public abstract class AbstractInjectingObjectFactory implements ObjectFactory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean areArgsAssignable(Class<?>[] paramTypes, Object[] givenArgs) {
|
||||||
|
if (paramTypes.length != givenArgs.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < paramTypes.length; i++) {
|
||||||
|
if (!paramTypes[i].isInstance(givenArgs[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @implNote If overridden, please cache any found non-inject constructors using
|
* @implNote If overridden, please cache any found non-inject constructors using
|
||||||
* {@link #putCachedNonInjectConstructor}.
|
* {@link #putCachedNonInjectConstructor}.
|
||||||
@ -187,7 +199,7 @@ public abstract class AbstractInjectingObjectFactory implements ObjectFactory {
|
|||||||
|
|
||||||
final Constructor<?>[] constructors = this.cachedAllConstructors.computeIfAbsent(clazz, Class::getConstructors);
|
final Constructor<?>[] constructors = this.cachedAllConstructors.computeIfAbsent(clazz, Class::getConstructors);
|
||||||
for (Constructor<?> constructor : constructors) {
|
for (Constructor<?> constructor : constructors) {
|
||||||
if (Arrays.equals(constructor.getParameterTypes(), types)) {
|
if (areArgsAssignable(constructor.getParameterTypes(), constructorArgs)) {
|
||||||
final Constructor<T> found = (Constructor<T>) constructor;
|
final Constructor<T> found = (Constructor<T>) constructor;
|
||||||
this.putCachedNonInjectConstructor(new CachedNonInjectConstructor<>(clazz, found, types));
|
this.putCachedNonInjectConstructor(new CachedNonInjectConstructor<>(clazz, found, types));
|
||||||
return found;
|
return found;
|
||||||
|
@ -213,4 +213,41 @@ public class DefaultRegistryObjectFactoryTests {
|
|||||||
assertEquals("HELLO, WORLD!", g.greet());
|
assertEquals("HELLO, WORLD!", g.greet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class NoInjectGreeter implements Greeter {
|
||||||
|
|
||||||
|
private final String greeting;
|
||||||
|
|
||||||
|
public NoInjectGreeter(String greeting) {
|
||||||
|
this.greeting = greeting;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String greet() {
|
||||||
|
return this.greeting;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void noInjectFoundViaGet() {
|
||||||
|
final var b = DefaultRegistryObjectFactory.Builder.withDefaults();
|
||||||
|
b.configureRegistry(r -> {
|
||||||
|
r.bind(NoInjectGreeter.class, toSelf());
|
||||||
|
});
|
||||||
|
final var f = b.build();
|
||||||
|
final var g = f.get(NoInjectGreeter.class, "Given Greeting");
|
||||||
|
assertEquals("Given Greeting", g.greet());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void noInjectFindViaCreate() {
|
||||||
|
final var b = DefaultRegistryObjectFactory.Builder.withDefaults();
|
||||||
|
b.configureRegistry(r -> {
|
||||||
|
r.bind(NoInjectGreeter.class, toSelf());
|
||||||
|
});
|
||||||
|
final var f = b.build();
|
||||||
|
final var g = f.createInstance(NoInjectGreeter.class, "Given Greeting");
|
||||||
|
assertEquals("Given Greeting", g.greet());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user