|
5 | 5 |
|
6 | 6 | public class SnapshotCaptor {
|
7 | 7 |
|
8 |
| - private Class<?> parameterClass; |
| 8 | + private Class<?> parameterClass; |
9 | 9 |
|
10 |
| - private Class<?> argumentClass; |
| 10 | + private Class<?> argumentClass; |
11 | 11 |
|
12 |
| - private String[] ignore; |
| 12 | + private String[] ignore; |
13 | 13 |
|
14 |
| - public SnapshotCaptor(Class<?> parameterClass, String... ignore) { |
15 |
| - this.parameterClass = parameterClass; |
16 |
| - this.argumentClass = parameterClass; |
17 |
| - this.ignore = ignore; |
18 |
| - } |
| 14 | + public SnapshotCaptor(Class<?> parameterClass, String... ignore) { |
| 15 | + this.parameterClass = parameterClass; |
| 16 | + this.argumentClass = parameterClass; |
| 17 | + this.ignore = ignore; |
| 18 | + } |
19 | 19 |
|
20 |
| - public SnapshotCaptor(Class<?> parameterClass, Class<?> argumentClass, String... ignore) { |
21 |
| - this.parameterClass = parameterClass; |
22 |
| - this.argumentClass = argumentClass; |
23 |
| - this.ignore = ignore; |
24 |
| - } |
| 20 | + public SnapshotCaptor(Class<?> parameterClass, Class<?> argumentClass, String... ignore) { |
| 21 | + this.parameterClass = parameterClass; |
| 22 | + this.argumentClass = argumentClass; |
| 23 | + this.ignore = ignore; |
| 24 | + } |
25 | 25 |
|
26 |
| - public Class<?> getParameterClass() { |
27 |
| - return parameterClass; |
28 |
| - } |
| 26 | + public Class<?> getParameterClass() { |
| 27 | + return parameterClass; |
| 28 | + } |
29 | 29 |
|
30 |
| - public Object removeIgnored(Object value) { |
31 |
| - Object newValue = value; |
32 |
| - if (ignore != null && ignore.length > 0) { |
33 |
| - newValue = shallowCopy(value); |
34 |
| - for (String each : ignore) { |
35 |
| - try { |
36 |
| - Field field = this.argumentClass.getDeclaredField(each); |
37 |
| - field.setAccessible(true); |
38 |
| - if (field.getType().isPrimitive()) { |
39 |
| - field.setByte(newValue, Integer.valueOf(0).byteValue()); |
40 |
| - } |
41 |
| - else { |
42 |
| - field.set(newValue, null); |
43 |
| - } |
44 |
| - } catch (IllegalAccessException | NoSuchFieldException e) { |
45 |
| - throw new SnapshotMatchException("Invalid Ignore value " + each, e.getCause()); |
46 |
| - } |
47 |
| - } |
| 30 | + public Object removeIgnored(Object value) { |
| 31 | + Object newValue = value; |
| 32 | + if (ignore != null && ignore.length > 0) { |
| 33 | + newValue = shallowCopy(value); |
| 34 | + for (String each : ignore) { |
| 35 | + try { |
| 36 | + Field field = this.argumentClass.getDeclaredField(each); |
| 37 | + field.setAccessible(true); |
| 38 | + if (field.getType().isPrimitive()) { |
| 39 | + field.setByte(newValue, Integer.valueOf(0).byteValue()); |
| 40 | + } else { |
| 41 | + field.set(newValue, null); |
| 42 | + } |
| 43 | + } catch (IllegalAccessException | NoSuchFieldException e) { |
| 44 | + throw new SnapshotMatchException("Invalid Ignore value " + each, e.getCause()); |
48 | 45 | }
|
49 |
| - return newValue; |
| 46 | + } |
50 | 47 | }
|
| 48 | + return newValue; |
| 49 | + } |
51 | 50 |
|
52 |
| - private Object shallowCopy(Object value) { |
| 51 | + private Object shallowCopy(Object value) { |
| 52 | + try { |
| 53 | + Object newValue = constructCopy(this.argumentClass); |
| 54 | + |
| 55 | + Field[] fields = this.argumentClass.getDeclaredFields(); |
| 56 | + |
| 57 | + for (Field field : fields) { |
| 58 | + field.setAccessible(true); |
53 | 59 | try {
|
54 |
| - Object newValue = constructCopy(this.argumentClass); |
55 |
| - |
56 |
| - Field[] fields = this.argumentClass.getDeclaredFields(); |
57 |
| - |
58 |
| - for (Field field: fields) { |
59 |
| - field.setAccessible(true); |
60 |
| - try { |
61 |
| - field.set(newValue, field.get(value)); |
62 |
| - } |
63 |
| - catch(Exception e) { |
64 |
| - //ignore |
65 |
| - } |
66 |
| - } |
67 |
| - return newValue; |
68 |
| - } |
69 |
| - catch (Exception e) { |
70 |
| - throw new SnapshotMatchException("Class "+ this.argumentClass.getSimpleName() + " must have a default empty constructor!"); |
| 60 | + field.set(newValue, field.get(value)); |
| 61 | + } catch (Exception e) { |
| 62 | + // ignore |
71 | 63 | }
|
| 64 | + } |
| 65 | + return newValue; |
| 66 | + } catch (Exception e) { |
| 67 | + throw new SnapshotMatchException( |
| 68 | + "Class " |
| 69 | + + this.argumentClass.getSimpleName() |
| 70 | + + " must have a default empty constructor!"); |
72 | 71 | }
|
| 72 | + } |
73 | 73 |
|
74 |
| - private Object constructCopy(Class<?> argumentClass) |
75 |
| - throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchMethodException { |
| 74 | + private Object constructCopy(Class<?> argumentClass) |
| 75 | + throws InstantiationException, IllegalAccessException, |
| 76 | + java.lang.reflect.InvocationTargetException, NoSuchMethodException { |
76 | 77 |
|
77 |
| - try { |
78 |
| - return argumentClass.getDeclaredConstructor().newInstance(); |
79 |
| - } |
80 |
| - catch (Exception e) { |
81 |
| - // Ignore - should log |
82 |
| - } |
| 78 | + try { |
| 79 | + return argumentClass.getDeclaredConstructor().newInstance(); |
| 80 | + } catch (Exception e) { |
| 81 | + // Ignore - should log |
| 82 | + } |
83 | 83 |
|
84 |
| - Constructor[] constructors = argumentClass.getDeclaredConstructors(); |
| 84 | + Constructor[] constructors = argumentClass.getDeclaredConstructors(); |
85 | 85 |
|
86 |
| - if (constructors.length == 0) { |
87 |
| - return argumentClass.getDeclaredConstructor().newInstance(); |
88 |
| - } |
| 86 | + if (constructors.length == 0) { |
| 87 | + return argumentClass.getDeclaredConstructor().newInstance(); |
| 88 | + } |
89 | 89 |
|
90 |
| - int i = 0; |
91 |
| - Class[] types = constructors[i].getParameterTypes(); |
92 |
| - Object[] paramValues = new Object[types.length]; |
93 |
| - |
94 |
| - for (int j = 0; j < types.length; j++) { |
95 |
| - if (types[j].isPrimitive()) { |
96 |
| - paramValues[j] = Integer.valueOf(0).byteValue(); |
97 |
| - } |
98 |
| - else { |
99 |
| - paramValues[j] = constructCopy(types[j]); |
100 |
| - } |
101 |
| - } |
102 |
| - return constructors[i].newInstance(paramValues); |
| 90 | + int i = 0; |
| 91 | + Class[] types = constructors[i].getParameterTypes(); |
| 92 | + Object[] paramValues = new Object[types.length]; |
| 93 | + |
| 94 | + for (int j = 0; j < types.length; j++) { |
| 95 | + if (types[j].isPrimitive()) { |
| 96 | + paramValues[j] = Integer.valueOf(0).byteValue(); |
| 97 | + } else { |
| 98 | + paramValues[j] = constructCopy(types[j]); |
| 99 | + } |
103 | 100 | }
|
| 101 | + return constructors[i].newInstance(paramValues); |
| 102 | + } |
104 | 103 | }
|
0 commit comments