@SuppressWarnings("unchecked") private static <T> T invokeConstructor(Constructor<?> constructor, Object... params) throws java.lang.InstantiationException, IllegalAccessException, InvocationTargetException { AccessibilityChanger accessibility = new AccessibilityChanger(); accessibility.enableAccess(constructor); return (T) constructor.newInstance(params); }
public static void setField(Object target, Field field,Object value) { AccessibilityChanger changer = new AccessibilityChanger(); changer.enableAccess(field); try { field.set(target, value); } catch (IllegalAccessException e) { throw new RuntimeException("Access not authorized on field '" + field + "' of object '" + target + "' with value: '" + value + "'", e); } catch (IllegalArgumentException e) { throw new RuntimeException("Wrong argument on field '" + field + "' of object '" + target + "' with value: '" + value + "', \n" + "reason : " + e.getMessage(), e); } changer.safelyDisableAccess(field); } }
public void set(Object value) { AccessibilityChanger changer = new AccessibilityChanger(); changer.enableAccess(field); try { field.set(target, value); } catch (IllegalAccessException e) { throw new RuntimeException("Access not authorized on field '" + field + "' of object '" + target + "' with value: '" + value + "'", e); } catch (IllegalArgumentException e) { throw new RuntimeException("Wrong argument on field '" + field + "' of object '" + target + "' with value: '" + value + "', \n" + "reason : " + e.getMessage(), e); } changer.safelyDisableAccess(field); } }
private <T> void copyValues(T from, T mock, Class<?> classFrom) { Field[] fields = classFrom.getDeclaredFields(); for (Field field : fields) { // ignore static fields if (Modifier.isStatic(field.getModifiers())) { continue; } AccessibilityChanger accessibilityChanger = new AccessibilityChanger(); try { accessibilityChanger.enableAccess(field); fieldCopier.copyValue(from, mock, field); } catch (Throwable t) { //Ignore - be lenient - if some field cannot be copied then let's be it } finally { accessibilityChanger.safelyDisableAccess(field); } } } }
/** * Initialize field if not initialized and return the actual instance. * * @return Actual field instance. */ public FieldInitializationReport initialize() { final AccessibilityChanger changer = new AccessibilityChanger(); changer.enableAccess(field); try { return acquireFieldInstance(); } catch(IllegalAccessException e) { throw new MockitoException("Problems initializing field '" + field.getName() + "' of type '" + field.getType().getSimpleName() + "'", e); } finally { changer.safelyDisableAccess(field); } }
private <T> void copyValues(T from, T mock, Class classFrom) { Field[] fields = classFrom.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { // ignore static fields Field field = fields[i]; if (Modifier.isStatic(field.getModifiers())) { continue; } AccessibilityChanger accessibilityChanger = new AccessibilityChanger(); try { accessibilityChanger.enableAccess(field); fieldCopier.copyValue(from, mock, field); } catch (Throwable t) { //Ignore - be lenient - if some field cannot be copied then let's be it } finally { accessibilityChanger.safelyDisableAccess(field); } } } }
/** * Set the value to the property represented by this {@link BeanPropertySetter} * @param value the new value to pass to the property setter * @return <code>true</code> if the value has been injected, <code>false</code> otherwise * @throws RuntimeException Can be thrown if the setter threw an exception, if the setter is not accessible * or, if <code>reportNoSetterFound</code> and setter could not be found. */ public boolean set(final Object value) { AccessibilityChanger changer = new AccessibilityChanger(); Method writeMethod = null; try { writeMethod = target.getClass().getMethod(setterName(field.getName()), field.getType()); changer.enableAccess(writeMethod); writeMethod.invoke(target, value); return true; } catch (InvocationTargetException e) { throw new RuntimeException("Setter '" + writeMethod + "' of '" + target + "' with value '" + value + "' threw exception : '" + e.getTargetException() + "'", e); } catch (IllegalAccessException e) { throw new RuntimeException("Access not authorized on field '" + field + "' of object '" + target + "' with value: '" + value + "'", e); } catch (NoSuchMethodException e) { reportNoSetterFound(); } finally { if(writeMethod != null) { changer.safelyDisableAccess(writeMethod); } } reportNoSetterFound(); return false; }
/** * Initialize field if not initialized and return the actual instance. * * @return Actual field instance. */ public FieldInitializationReport initialize() { final AccessibilityChanger changer = new AccessibilityChanger(); changer.enableAccess(field); try { return acquireFieldInstance(); } catch(IllegalAccessException e) { throw new MockitoException("Problems initializing field '" + field.getName() + "' of type '" + field.getType().getSimpleName() + "'", e); } finally { changer.safelyDisableAccess(field); } }
public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = field.getType().getDeclaredConstructor(); changer.enableAccess(constructor); final Object[] noArg = new Object[0]; Object newFieldInstance = constructor.newInstance(noArg); setField(testClass, field,newFieldInstance); return new FieldInitializationReport(field.get(testClass), true, false); } catch (NoSuchMethodException e) { throw new MockitoException("the type '" + field.getType().getSimpleName() + "' has no default constructor", e); } catch (InvocationTargetException e) { throw new MockitoException("the default 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); } } } }
/** * Set the value to the property represented by this {@link BeanPropertySetter} * @param value the new value to pass to the property setter * @return <code>true</code> if the value has been injected, <code>false</code> otherwise * @throws RuntimeException Can be thrown if the setter threw an exception, if the setter is not accessible * or, if <code>reportNoSetterFound</code> and setter could not be found. */ public boolean set(final Object value) { AccessibilityChanger changer = new AccessibilityChanger(); Method writeMethod = null; try { writeMethod = target.getClass().getMethod(setterName(field.getName()), field.getType()); changer.enableAccess(writeMethod); writeMethod.invoke(target, value); return true; } catch (InvocationTargetException e) { throw new RuntimeException("Setter '" + writeMethod + "' of '" + target + "' with value '" + value + "' threw exception : '" + e.getTargetException() + "'", e); } catch (IllegalAccessException e) { throw new RuntimeException("Access not authorized on field '" + field + "' of object '" + target + "' with value: '" + value + "'", e); } catch (NoSuchMethodException e) { reportNoSetterFound(); } finally { if(writeMethod != null) { changer.safelyDisableAccess(writeMethod); } } reportNoSetterFound(); return false; }
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); } } }
public FieldInitializationReport instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = field.getType().getDeclaredConstructor(); changer.enableAccess(constructor); final Object[] noArg = new Object[0]; Object newFieldInstance = constructor.newInstance(noArg); new FieldSetter(testClass, field).set(newFieldInstance); return new FieldInitializationReport(field.get(testClass), true, false); } catch (NoSuchMethodException e) { throw new MockitoException("the type '" + field.getType().getSimpleName() + "' has no default constructor", e); } catch (InvocationTargetException e) { throw new MockitoException("the default 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); } } } }
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); } } }
public void set(Object value) { AccessibilityChanger changer = new AccessibilityChanger(); changer.enableAccess(field); try { field.set(target, value); } catch (IllegalAccessException e) { throw new RuntimeException("Access not authorized on field '" + field + "' of object '" + target + "' with value: '" + value + "'", e); } catch (IllegalArgumentException e) { throw new RuntimeException("Wrong argument on field '" + field + "' of object '" + target + "' with value: '" + value + "', \n" + "reason : " + e.getMessage(), e); } changer.safelyDisableAccess(field); } }
/** * Initialize field if no initialized and return the actual instance. * * @return Actual field instance. */ public FieldInitializationReport initialize() { final AccessibilityChanger changer = new AccessibilityChanger(); changer.enableAccess(field); try { return acquireFieldInstance(); } catch(IllegalAccessException e) { throw new MockitoException("Problems initializing field '" + field.getName() + "' of type '" + field.getType().getSimpleName() + "'", e); } finally { changer.safelyDisableAccess(field); } }
private <T> void copyValues(T from, T mock, Class classFrom) { Field[] fields = classFrom.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { // ignore static fields Field field = fields[i]; if (Modifier.isStatic(field.getModifiers())) { continue; } AccessibilityChanger accessibilityChanger = new AccessibilityChanger(); try { accessibilityChanger.enableAccess(field); fieldCopier.copyValue(from, mock, field); } catch (Throwable t) { //Ignore - be lenient - if some field cannot be copied then let's be it } finally { accessibilityChanger.safelyDisableAccess(field); } } } }
AccessibilityChanger changer = new AccessibilityChanger(); Method writeMethod = null; try {
public Object instantiate() { final AccessibilityChanger changer = new AccessibilityChanger(); Constructor<?> constructor = null; try { constructor = field.getType().getDeclaredConstructor(); changer.enableAccess(constructor); final Object[] noArg = new Object[0]; Object newFieldInstance = constructor.newInstance(noArg); new FieldSetter(testClass, field).set(newFieldInstance); return field.get(testClass); } catch (NoSuchMethodException e) { throw new MockitoException("the type '" + field.getType().getSimpleName() + "' has no default constructor", e); } catch (InvocationTargetException e) { throw new MockitoException("the default 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); } } } }
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); } } }