Skip to content

Commit

Permalink
buggy files form Mockito #21
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Mar 7, 2017
1 parent 62b1a62 commit e5fd500
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.mockito.internal.creation.instance;

import java.lang.reflect.Constructor;

public class ConstructorInstantiator implements Instantiator {

private final Object outerClassInstance;

public ConstructorInstantiator(Object outerClassInstance) {
this.outerClassInstance = outerClassInstance;
}

public <T> T newInstance(Class<T> cls) {
if (outerClassInstance == null) {
return noArgConstructor(cls);
}
return withOuterClass(cls);
}

private <T> T withOuterClass(Class<T> cls) {
try {
//this is kind of overengineered because we don't need to support more params
//however, I know we will be needing it :)
Constructor<T> c = cls.getDeclaredConstructor(outerClassInstance.getClass());
return c.newInstance(outerClassInstance);
} catch (Exception e) {
throw paramsException(cls, e);
}
}

private static <T> InstantationException paramsException(Class<T> cls, Exception e) {
return new InstantationException("Unable to create mock instance of '"
+ cls.getSimpleName() + "'.\nPlease ensure that the outer instance has correct type and that the target class has parameter-less constructor.", e);
}


private static <T> T noArgConstructor(Class<T> cls) {
try {
return cls.newInstance();
} catch (Exception e) {
throw new InstantationException("Unable to create mock instance of '"
+ cls.getSimpleName() + "'.\nPlease ensure it has parameter-less constructor.", e);
}
}
}

0 comments on commit e5fd500

Please sign in to comment.