Skip to content

Commit a83f0aa

Browse files
author
Andre Bonna
committed
Treat objects without default constructor
1 parent 7ee7f99 commit a83f0aa

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Based on [facebook's Jest framework](https://facebook.github.io/jest/docs/en/sna
1818

1919

2020

21-
#### How to install using [Maven](https://mvnrepository.com/artifact/io.github.json-snapshot/json-snapshot/1.0.9)
21+
#### How to install using [Maven](https://mvnrepository.com/artifact/io.github.json-snapshot/json-snapshot/1.0.11)
2222

2323

2424

@@ -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.9</version>
31+
<version>1.0.11</version>
3232
</dependency>
3333
```
3434

pom.xml

+1-1
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.9</version>
8+
<version>1.0.11</version>
99
<packaging>jar</packaging>
1010

1111
<name>json-snapshot</name>

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

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.jsonSnapshot;
22

3+
import java.lang.reflect.Constructor;
34
import java.lang.reflect.Field;
45

56
public class SnapshotCaptor {
@@ -50,7 +51,8 @@ public Object removeIgnored(Object value) {
5051

5152
private Object shallowCopy(Object value) {
5253
try {
53-
Object newValue = this.argumentClass.newInstance();
54+
Object newValue = constructCopy(this.argumentClass);
55+
5456
Field[] fields = this.argumentClass.getDeclaredFields();
5557

5658
for (Field field: fields) {
@@ -68,4 +70,35 @@ private Object shallowCopy(Object value) {
6870
throw new SnapshotMatchException("Class "+ this.argumentClass.getSimpleName() + " must have a default empty constructor!");
6971
}
7072
}
73+
74+
private Object constructCopy(Class<?> argumentClass)
75+
throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
76+
77+
try {
78+
return argumentClass.newInstance();
79+
}
80+
catch (Exception e) {
81+
// Ignore - should log
82+
}
83+
84+
Constructor[] constructors = argumentClass.getDeclaredConstructors();
85+
86+
if (constructors.length == 0) {
87+
return argumentClass.newInstance();
88+
}
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);
103+
}
71104
}

0 commit comments

Comments
 (0)