Skip to content

Commit 24aa4eb

Browse files
author
Andre Bonna
committed
Improvements to extractArgs to include ignoring fields logic
1 parent fbcfda3 commit 24aa4eb

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Add to your pom.xml dependencies section:
2828
<dependency>
2929
<groupId>io.github.json-snapshot</groupId>
3030
<artifactId>json-snapshot</artifactId>
31-
<version>1.0.4</version>
31+
<version>1.0.6</version>
3232
</dependency>
3333
```
3434

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>io.github.json-snapshot</groupId>
77
<artifactId>json-snapshot</artifactId>
8-
<version>1.0.5</version>
8+
<version>1.0.6</version>
99
<packaging>jar</packaging>
1010

1111
<name>json-snapshot</name>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.github.jsonSnapshot;
2+
3+
import java.lang.reflect.Field;
4+
5+
public class SnapshotCaptor {
6+
7+
private Class<?> parameterClass;
8+
9+
private Class<?> argumentClass;
10+
11+
private String[] ignore;
12+
13+
public SnapshotCaptor(Class<?> parameterClass, String... ignore) {
14+
this.parameterClass = parameterClass;
15+
this.argumentClass = parameterClass;
16+
this.ignore = ignore;
17+
}
18+
19+
public SnapshotCaptor(Class<?> parameterClass, Class<?> argumentClass, String... ignore) {
20+
this.parameterClass = parameterClass;
21+
this.argumentClass = argumentClass;
22+
this.ignore = ignore;
23+
}
24+
25+
public Class<?> getParameterClass() {
26+
return parameterClass;
27+
}
28+
29+
public Object removeIgnored(Object value) {
30+
Object newValue = value;
31+
if (ignore != null && ignore.length > 0) {
32+
newValue = shallowCopy(value);
33+
for (String each : ignore) {
34+
try {
35+
Field field = this.argumentClass.getDeclaredField(each);
36+
field.setAccessible(true);
37+
field.set(newValue, null);
38+
} catch (IllegalAccessException | NoSuchFieldException e) {
39+
throw new SnapshotMatchException("Invalid Ignore value " + each, e.getCause());
40+
}
41+
}
42+
}
43+
return newValue;
44+
}
45+
46+
private Object shallowCopy(Object value) {
47+
try {
48+
Object newValue = this.argumentClass.newInstance();
49+
Field[] fields = this.argumentClass.getDeclaredFields();
50+
51+
for (Field field: fields) {
52+
field.setAccessible(true);
53+
try {
54+
field.set(newValue, field.get(value));
55+
}
56+
catch(Exception e) {
57+
//ignore
58+
}
59+
}
60+
return newValue;
61+
}
62+
catch (Exception e) {
63+
throw new SnapshotMatchException("Class "+ this.argumentClass.getSimpleName() + " must have a default empty constructor!");
64+
}
65+
}
66+
}

src/main/java/io/github/jsonSnapshot/SnapshotMatchException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ public class SnapshotMatchException extends RuntimeException {
55
SnapshotMatchException(String message) {
66
super(message);
77
}
8+
9+
SnapshotMatchException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
812
}

src/main/java/io/github/jsonSnapshot/SnapshotUtils.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,36 @@
1414
public class SnapshotUtils {
1515

1616
public static <T> HashMap<String, List<LinkedHashMap<String, Object>>> extractArgs(T object, String methodName,
17-
Class... classes) {
17+
SnapshotCaptor... snapshotCaptors) {
18+
List<ArgumentCaptor> captors = new ArrayList<>();
19+
Class[] classes = new Class[snapshotCaptors.length];
20+
21+
int i = 0;
22+
for (SnapshotCaptor snapshotCaptor : snapshotCaptors) {
23+
classes[i] = snapshotCaptor.getParameterClass();
24+
captors.add(ArgumentCaptor.forClass(snapshotCaptor.getParameterClass()));
25+
i++;
26+
}
27+
28+
return process(object, methodName, captors, classes, snapshotCaptors);
29+
}
30+
31+
public static <T> HashMap<String, List<LinkedHashMap<String, Object>>> extractArgs(T object, String methodName,
32+
Class<?>... classes) {
1833
List<ArgumentCaptor> captors = new ArrayList<>();
19-
HashMap<String, List<LinkedHashMap<String, Object>>> result = new HashMap<>();
2034

2135
for (Class clazz : classes) {
2236
captors.add(ArgumentCaptor.forClass(clazz));
2337
}
2438

39+
return process(object, methodName, captors, classes, null);
40+
}
41+
42+
private static <T> HashMap<String, List<LinkedHashMap<String, Object>>> process(T object, String methodName,
43+
List<ArgumentCaptor> captors,
44+
Class[] classes,
45+
SnapshotCaptor[] snapshotCaptors) {
46+
HashMap<String, List<LinkedHashMap<String, Object>>> result = new HashMap<>();
2547
try {
2648
Parameter[] parameters = object.getClass().getMethod(methodName, classes).getParameters();
2749

@@ -42,7 +64,11 @@ public static <T> HashMap<String, List<LinkedHashMap<String, Object>>> extractAr
4264

4365
int j = 0;
4466
for (ArgumentCaptor captor : captors) {
45-
objectMap.put(parameters[j].getName(), captor.getAllValues().get(i));
67+
Object value = captor.getAllValues().get(i);
68+
if (snapshotCaptors != null) {
69+
value = snapshotCaptors[j].removeIgnored(value);
70+
}
71+
objectMap.put(parameters[j].getName(), value);
4672
j++;
4773
}
4874
extractedObjects.add(objectMap);
@@ -51,7 +77,7 @@ public static <T> HashMap<String, List<LinkedHashMap<String, Object>>> extractAr
5177

5278
result.put(object.getClass().getSuperclass().getSimpleName() + "." + methodName, extractedObjects);
5379
} catch (Exception e) {
54-
throw new SnapshotMatchException(e.getMessage());
80+
throw new SnapshotMatchException(e.getMessage(), e.getCause());
5581
}
5682

5783
return result;

0 commit comments

Comments
 (0)