diff --git a/projects/Mockito/21/org/mockito/internal/creation/instance/ConstructorInstantiator.java b/projects/Mockito/21/org/mockito/internal/creation/instance/ConstructorInstantiator.java index 0cc8226..9f0201a 100644 --- a/projects/Mockito/21/org/mockito/internal/creation/instance/ConstructorInstantiator.java +++ b/projects/Mockito/21/org/mockito/internal/creation/instance/ConstructorInstantiator.java @@ -14,18 +14,23 @@ public T newInstance(Class cls) { if (outerClassInstance == null) { return noArgConstructor(cls); } - return withOuterClass(cls); + return withParams(cls, outerClassInstance); } - private T withOuterClass(Class cls) { + private static T withParams(Class cls, Object... params) { try { //this is kind of overengineered because we don't need to support more params //however, I know we will be needing it :) - Constructor c = cls.getDeclaredConstructor(outerClassInstance.getClass()); - return c.newInstance(outerClassInstance); + for (Constructor constructor : cls.getDeclaredConstructors()) { + Class[] types = constructor.getParameterTypes(); + if (paramsMatch(types, params)) { + return (T) constructor.newInstance(params); + } + } } catch (Exception e) { throw paramsException(cls, e); } + throw paramsException(cls, null); } private static InstantationException paramsException(Class cls, Exception e) { @@ -33,6 +38,17 @@ private static InstantationException paramsException(Class cls, Exception + cls.getSimpleName() + "'.\nPlease ensure that the outer instance has correct type and that the target class has parameter-less constructor.", e); } + private static boolean paramsMatch(Class[] types, Object[] params) { + if (params.length != types.length) { + return false; + } + for (int i = 0; i < params.length; i++) { + if (!types[i].isInstance(params[i])) { + return false; + } + } + return true; + } private static T noArgConstructor(Class cls) { try {