1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.codebox.instance;
16
17 import com.codebox.bean.ValueBuilder;
18 import com.codebox.enums.LoadData;
19 import com.codebox.enums.LoadType;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.Arrays;
24
25 import org.junit.jupiter.api.Assertions;
26
27
28
29
30
31
32
33 public class ClassInstance<T> {
34
35
36
37
38
39
40
41
42
43
44 @SuppressWarnings("unchecked")
45 public final T newInstance(final Class<T> clazz) {
46
47 for (final Constructor<?> constructor : clazz.getConstructors()) {
48
49 if (constructor.isAnnotationPresent(Deprecated.class)) {
50 continue;
51 }
52
53
54 if (constructor.getParameterCount() == 0) {
55 try {
56 return (T) constructor.newInstance((Object[]) null);
57 } catch (final InstantiationException | IllegalAccessException | InvocationTargetException e) {
58 Assertions.fail(String.format(
59 "An exception was thrown while testing the class (new instance) '%s' with '%s': '%s'",
60 constructor.getName(), Arrays.toString((Object[]) null), e.toString()));
61 }
62 }
63 }
64
65
66 for (final Constructor<?> constructor : clazz.getConstructors()) {
67
68
69 if (constructor.isAnnotationPresent(Deprecated.class)) {
70 continue;
71 }
72
73 final Class<?>[] types = constructor.getParameterTypes();
74
75 final Object[] values = new Object[constructor.getParameterTypes().length];
76
77
78 for (int i = 0; i < values.length; i++) {
79 values[i] = this.buildValue(types[i], LoadType.STANDARD_DATA);
80 }
81
82 try {
83 return (T) constructor.newInstance(values);
84 } catch (final InstantiationException | IllegalAccessException | InvocationTargetException e) {
85 Assertions.fail(String.format(
86 "An exception was thrown while testing the class (new instance) '%s' with '%s': '%s'",
87 constructor.getName(), Arrays.toString(values), e.toString()));
88 }
89 }
90 return null;
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public <R> Object buildValue(final Class<R> returnType, final LoadType loadType) {
106 final ValueBuilder valueBuilder = new ValueBuilder();
107 valueBuilder.setLoadData(LoadData.ON);
108 return valueBuilder.buildValue(returnType, loadType);
109 }
110
111 }