public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = biggestConstructor(field.getType()); changer.enableAccess(constructor); final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); Object newFieldInstance = constructor.newInstance(args); new FieldSetter(testClass, field).set(newFieldInstance); return new FieldInitializationReport(field.get(testClass), false, true); } catch (IllegalArgumentException e) { throw new MockitoException("internal error : argResolver provided incorrect types for constructor " + constructor + " of type " + field.getType().getSimpleName(), e); } catch (InvocationTargetException e) { throw new MockitoException("the constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e); } catch (InstantiationException e) { throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e); } catch (IllegalAccessException e) { throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e); } finally { if(constructor != null) { changer.safelyDisableAccess(constructor); } } }
private Constructor<?> biggestConstructor(Class<?> clazz) { final List<Constructor<?>> constructors = Arrays.asList(clazz.getDeclaredConstructors()); Collections.sort(constructors, byParameterNumber); Constructor<?> constructor = constructors.get(0); checkParameterized(constructor, field); return constructor; } }
public Object instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = biggestConstructor(field.getType()); changer.enableAccess(constructor); final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); Object newFieldInstance = constructor.newInstance(args); new FieldSetter(testClass, field).set(newFieldInstance); return field.get(testClass); } catch (IllegalArgumentException e) { throw new MockitoException("internal error : argResolver provided incorrect types for constructor " + constructor + " of type " + field.getType().getSimpleName(), e); } catch (InvocationTargetException e) { throw new MockitoException("the constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e); } catch (InstantiationException e) { throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e); } catch (IllegalAccessException e) { throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e); } finally { if(constructor != null) { changer.safelyDisableAccess(constructor); } } }
/** * Prepare initializer with the given field on the given instance. * * <p> * This constructor fail fast if the field type cannot be handled. * </p> * * @param fieldOwner Instance of the test. * @param field Field to be initialize. */ public FieldInitializer(Object fieldOwner, Field field, ConstructorArgumentResolver argResolver) { this(fieldOwner, field, new ParameterizedConstructorInstantiator(fieldOwner, field, argResolver)); }
private Constructor<?> biggestConstructor(Class<?> clazz) { final List<Constructor<?>> constructors = Arrays.asList(clazz.getDeclaredConstructors()); Collections.sort(constructors, byParameterNumber); Constructor<?> constructor = constructors.get(0); checkParameterized(constructor, field); return constructor; } }
public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = biggestConstructor(field.getType()); changer.enableAccess(constructor); final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); Object newFieldInstance = constructor.newInstance(args); setField(testClass, field,newFieldInstance); return new FieldInitializationReport(field.get(testClass), false, true); } catch (IllegalArgumentException e) { throw new MockitoException("internal error : argResolver provided incorrect types for constructor " + constructor + " of type " + field.getType().getSimpleName(), e); } catch (InvocationTargetException e) { throw new MockitoException("the constructor of type '" + field.getType().getSimpleName() + "' has raised an exception (see the stack trace for cause): " + e.getTargetException().toString(), e); } catch (InstantiationException e) { throw new MockitoException("InstantiationException (see the stack trace for cause): " + e.toString(), e); } catch (IllegalAccessException e) { throw new MockitoException("IllegalAccessException (see the stack trace for cause): " + e.toString(), e); } finally { if(constructor != null) { changer.safelyDisableAccess(constructor); } } }
/** * Prepare initializer with the given field on the given instance. * * <p> * This constructor fail fast if the field type cannot be handled. * </p> * * @param fieldOwner Instance of the test. * @param field Field to be initialize. * @param argResolver Constructor parameters resolver */ public FieldInitializer(Object fieldOwner, Field field, ConstructorArgumentResolver argResolver) { this(fieldOwner, field, new ParameterizedConstructorInstantiator(fieldOwner, field, argResolver)); }
/** * Prepare initializer with the given field on the given instance. * * <p> * This constructor fail fast if the field type cannot be handled. * </p> * * @param fieldOwner Instance of the test. * @param field Field to be initialize. * @param argResolver Constructor parameters resolver */ public FieldInitializer(Object fieldOwner, Field field, ConstructorArgumentResolver argResolver) { this(fieldOwner, field, new ParameterizedConstructorInstantiator(fieldOwner, field, argResolver)); }
private Constructor<?> biggestConstructor(Class<?> clazz) { final List<? extends Constructor<?>> constructors = Arrays.asList(clazz.getDeclaredConstructors()); Collections.sort(constructors, byParameterNumber); Constructor<?> constructor = constructors.get(0); checkParameterized(constructor, field); return constructor; } }