1
1
package org .gitlab4j .api ;
2
2
3
+ import static org .junit .jupiter .api .Assertions .fail ;
4
+
3
5
import java .io .BufferedReader ;
4
6
import java .io .IOException ;
5
7
import java .io .InputStreamReader ;
6
8
import java .io .Reader ;
9
+ import java .io .Serializable ;
10
+ import java .lang .reflect .Field ;
11
+ import java .lang .reflect .Modifier ;
12
+ import java .lang .reflect .ParameterizedType ;
13
+ import java .lang .reflect .Type ;
14
+ import java .util .Collection ;
15
+ import java .util .HashSet ;
7
16
import java .util .List ;
8
17
import java .util .Map ;
18
+ import java .util .Set ;
9
19
10
20
import org .gitlab4j .api .utils .JacksonJson ;
21
+ import org .junit .jupiter .api .Assertions ;
11
22
12
23
import com .fasterxml .jackson .core .JsonParseException ;
13
24
import com .fasterxml .jackson .core .JsonProcessingException ;
@@ -26,7 +37,7 @@ public class JsonUtils {
26
37
}
27
38
28
39
static JsonNode readTreeFromMap (Map <String , Object > map ) throws JsonParseException , JsonMappingException , IOException {
29
- String jsonString = jacksonJson .getObjectMapper ().writeValueAsString (map );
40
+ String jsonString = jacksonJson .getObjectMapper ().writeValueAsString (map );
30
41
return (jacksonJson .readTree (jsonString ));
31
42
}
32
43
@@ -40,48 +51,120 @@ static JsonNode readTreeFromResource(String filename) throws JsonParseException,
40
51
}
41
52
42
53
static <T > T unmarshalResource (Class <T > returnType , String filename ) throws JsonParseException , JsonMappingException , IOException {
54
+ checkSerializable (returnType );
43
55
InputStreamReader reader = new InputStreamReader (TestGitLabApiBeans .class .getResourceAsStream (filename ));
44
56
return (jacksonJson .unmarshal (returnType , reader ));
45
57
}
46
58
47
- static <T > List <T > unmarshalResourceList (Class <T > returnType , String filename ) throws JsonParseException , JsonMappingException , IOException {
59
+ static <T > List <T > unmarshalResourceList (Class <T > returnType , String filename ) throws JsonParseException , JsonMappingException , IOException {
60
+ checkSerializable (returnType );
48
61
InputStreamReader reader = new InputStreamReader (TestGitLabApiBeans .class .getResourceAsStream (filename ));
49
62
return (JsonUtils .unmarshalList (returnType , reader ));
50
63
}
51
64
52
65
static <T > Map <String , T > unmarshalResourceMap (Class <T > returnType , String filename ) throws JsonParseException , JsonMappingException , IOException {
66
+ checkSerializable (returnType );
53
67
InputStreamReader reader = new InputStreamReader (TestGitLabApiBeans .class .getResourceAsStream (filename ));
54
68
return (jacksonJson .unmarshalMap (returnType , reader ));
55
69
}
56
70
57
71
static <T > T unmarshal (Class <T > returnType , Reader reader ) throws JsonParseException , JsonMappingException , IOException {
72
+ checkSerializable (returnType );
58
73
return (jacksonJson .unmarshal (returnType , reader ));
59
74
}
60
75
61
76
static <T > T unmarshal (Class <T > returnType , JsonNode tree ) throws JsonParseException , JsonMappingException , IOException {
77
+ checkSerializable (returnType );
62
78
return (jacksonJson .unmarshal (returnType , tree ));
63
79
}
64
80
65
81
static <T > T unmarshal (Class <T > returnType , String json ) throws JsonParseException , JsonMappingException , IOException {
82
+ checkSerializable (returnType );
66
83
return (jacksonJson .unmarshal (returnType , json ));
67
84
}
68
85
69
86
static <T > List <T > unmarshalList (Class <T > returnType , Reader reader ) throws JsonParseException , JsonMappingException , IOException {
87
+ checkSerializable (returnType );
70
88
return (jacksonJson .unmarshalList (returnType , reader ));
71
89
}
72
90
73
91
static <T > List <T > unmarshalList (Class <T > returnType , String json ) throws JsonParseException , JsonMappingException , IOException {
92
+ checkSerializable (returnType );
74
93
return (jacksonJson .unmarshalList (returnType , json ));
75
94
}
76
95
77
96
static <T > Map <String , T > unmarshalMap (Class <T > returnType , Reader reader ) throws JsonParseException , JsonMappingException , IOException {
97
+ checkSerializable (returnType );
78
98
return (jacksonJson .unmarshalMap (returnType , reader ));
79
99
}
80
100
81
101
static <T > Map <String , T > unmarshalMap (Class <T > returnType , String json ) throws JsonParseException , JsonMappingException , IOException {
102
+ checkSerializable (returnType );
82
103
return (jacksonJson .unmarshalMap (returnType , json ));
83
104
}
84
105
106
+ static <T > void checkSerializable (Class <T > cls ) {
107
+ if (!isSerializable (cls , new HashSet <>())) {
108
+ fail ("Class " + cls .getCanonicalName () + " or one of its member does not implement Serializable" );
109
+ }
110
+ }
111
+ static <T > boolean isSerializable (Class <T > cls , Set <Class <?>> checkedTypes ) {
112
+ if (checkedTypes .contains (cls )) {
113
+ return true ;
114
+ }
115
+ checkedTypes .add (cls );
116
+
117
+ if (!Serializable .class .isAssignableFrom (cls )) {
118
+ return false ;
119
+ }
120
+
121
+ Field [] fields = cls .getDeclaredFields ();
122
+ for (Field field : fields ) {
123
+ if (!Modifier .isStatic (field .getModifiers ()) && !Modifier .isTransient (field .getModifiers ())) {
124
+ Class <?> fieldClass = field .getType ();
125
+ if (!isSimpleType (fieldClass ) && !isSerializable (fieldClass , checkedTypes ) && !isCollectionSerializable (field , checkedTypes )) {
126
+ return false ;
127
+ }
128
+ }
129
+ }
130
+ return true ;
131
+ }
132
+
133
+ private static boolean isSimpleType (Class <?> type ) {
134
+ return type .isPrimitive () ||
135
+ type .equals (String .class ) ||
136
+ type .equals (Integer .class ) ||
137
+ type .equals (Long .class ) ||
138
+ type .equals (Double .class ) ||
139
+ type .equals (Float .class ) ||
140
+ type .equals (Boolean .class ) ||
141
+ type .equals (Character .class ) ||
142
+ type .equals (Byte .class ) ||
143
+ type .equals (Short .class );
144
+ }
145
+
146
+ private static boolean isCollectionSerializable (Field field , Set <Class <?>> checkedTypes ) {
147
+ Class <?> fieldType = field .getType ();
148
+ if (Collection .class .isAssignableFrom (fieldType ) || Map .class .isAssignableFrom (fieldType )) {
149
+ Type genericType = field .getGenericType ();
150
+
151
+ if (genericType instanceof ParameterizedType ) {
152
+ ParameterizedType parameterizedType = (ParameterizedType ) genericType ;
153
+ Type [] typeArguments = parameterizedType .getActualTypeArguments ();
154
+
155
+ for (Type typeArg : typeArguments ) {
156
+ if (typeArg instanceof Class ) {
157
+ Class <?> typeClass = (Class <?>) typeArg ;
158
+ if (!isSimpleType (typeClass ) && !isSerializable (typeClass , checkedTypes )) {
159
+ return false ;
160
+ }
161
+ }
162
+ }
163
+ }
164
+ }
165
+ return true ;
166
+ }
167
+
85
168
static <T > boolean compareJson (T apiObject , String filename ) throws IOException {
86
169
InputStreamReader reader = new InputStreamReader (TestGitLabApiBeans .class .getResourceAsStream (filename ));
87
170
return (compareJson (apiObject , reader ));
0 commit comments