-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
projects/Mockito/21/org/mockito/internal/creation/instance/ConstructorInstantiator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |