diff --git a/src/net/azib/ipscan/di/Injector.java b/src/net/azib/ipscan/di/Injector.java index ffbd945f..3bbefc65 100644 --- a/src/net/azib/ipscan/di/Injector.java +++ b/src/net/azib/ipscan/di/Injector.java @@ -42,11 +42,12 @@ public class Injector { } private T createInstance(Class type) { - Constructor constructor = (Constructor) stream(type.getConstructors()) - .filter(c -> c.isAnnotationPresent(Inject.class)).findAny() - .orElseThrow(() -> new InjectException(type.getName() + " has no constructors annotated with @Inject")); + Constructor[] constructors = (Constructor[]) type.getConstructors(); + Constructor constructor = stream(constructors).filter(c -> c.isAnnotationPresent(Inject.class)).findAny().orElse(null); + if (constructor == null && constructors.length == 1) constructor = constructors[0]; try { - return constructor.newInstance(resolveDeps(constructor)); + if (constructor != null) return constructor.newInstance(resolveDeps(constructor)); + else throw new InjectException(type.getName() + " has no default constructor or a constructor annotated with @Inject"); } catch (Exception e) { throw new InjectException("Cannot create " + type.getName() + ", deps: " + Arrays.toString(constructor.getGenericParameterTypes()), e); diff --git a/test/net/azib/ipscan/di/InjectorTest.java b/test/net/azib/ipscan/di/InjectorTest.java index 5890ae0d..6727286d 100644 --- a/test/net/azib/ipscan/di/InjectorTest.java +++ b/test/net/azib/ipscan/di/InjectorTest.java @@ -33,7 +33,7 @@ public class InjectorTest { } static class Dummy { - @Inject public Dummy() {} + public Dummy() {} } static class WithDeps {