25
25
import java .lang .reflect .Modifier ;
26
26
import java .lang .reflect .ParameterizedType ;
27
27
import java .lang .reflect .Type ;
28
- import java .util .ArrayList ;
29
28
import java .util .Arrays ;
30
29
import java .util .LinkedList ;
31
30
import java .util .List ;
@@ -45,8 +44,12 @@ class InstanceCoercer extends TypeCoercer<Object> {
45
44
46
45
@ Override
47
46
public boolean test (Class aClass ) {
48
- // If the class doesn't have a no-arg constructor, abandon hope
49
- return getConstructor (aClass ) != null ;
47
+ try {
48
+ getConstructor (aClass );
49
+ return true ;
50
+ } catch (JsonException ex ) {
51
+ return false ;
52
+ }
50
53
}
51
54
52
55
@ Override
@@ -97,7 +100,7 @@ public BiFunction<JsonInput, PropertySetting, Object> apply(Type type) {
97
100
98
101
private Map <String , TypeAndWriter > getFieldWriters (Constructor <?> constructor ) {
99
102
List <Field > fields = new LinkedList <>();
100
- for (Class current = constructor .getDeclaringClass (); current != Object .class ; current = current .getSuperclass ()) {
103
+ for (Class <?> current = constructor .getDeclaringClass (); current != Object .class ; current = current .getSuperclass ()) {
101
104
fields .addAll (Arrays .asList (current .getDeclaredFields ()));
102
105
}
103
106
@@ -109,17 +112,16 @@ private Map<String, TypeAndWriter> getFieldWriters(Constructor<?> constructor) {
109
112
Collectors .toMap (
110
113
Field ::getName ,
111
114
field -> {
112
- TypeAndWriter tw = new TypeAndWriter ();
113
- tw .type = field .getGenericType ();
114
- tw .writer =
115
+ Type type = field .getGenericType ();
116
+ BiConsumer <Object , Object > writer =
115
117
(instance , value ) -> {
116
118
try {
117
119
field .set (instance , value );
118
120
} catch (IllegalAccessException e ) {
119
121
throw new JsonException (e );
120
122
}
121
123
};
122
- return tw ;
124
+ return new TypeAndWriter ( type , writer ) ;
123
125
}));
124
126
}
125
127
@@ -131,9 +133,8 @@ private Map<String, TypeAndWriter> getBeanWriters(Constructor<?> constructor) {
131
133
Collectors .toMap (
132
134
SimplePropertyDescriptor ::getName ,
133
135
desc -> {
134
- TypeAndWriter tw = new TypeAndWriter ();
135
- tw .type = desc .getWriteMethod ().getGenericParameterTypes ()[0 ];
136
- tw .writer = (instance , value ) -> {
136
+ Type type = desc .getWriteMethod ().getGenericParameterTypes ()[0 ];
137
+ BiConsumer <Object , Object >writer = (instance , value ) -> {
137
138
Method method = desc .getWriteMethod ();
138
139
method .setAccessible (true );
139
140
try {
@@ -142,31 +143,31 @@ private Map<String, TypeAndWriter> getBeanWriters(Constructor<?> constructor) {
142
143
throw new JsonException (e );
143
144
}
144
145
};
145
- return tw ;
146
+ return new TypeAndWriter ( type , writer ) ;
146
147
}));
147
148
}
148
149
149
150
private Constructor <?> getConstructor (Type type ) {
150
- Class target = getClss (type );
151
+ Class <?> target = getClss (type );
151
152
152
153
try {
153
- @ SuppressWarnings ( "unchecked" ) Constructor <?> constructor = target .getDeclaredConstructor ();
154
+ Constructor <?> constructor = target .getDeclaredConstructor ();
154
155
constructor .setAccessible (true );
155
156
return constructor ;
156
157
} catch (ReflectiveOperationException e ) {
157
158
throw new JsonException (e );
158
159
}
159
160
}
160
161
161
- private static Class getClss (Type type ) {
162
- Class target = null ;
162
+ private static Class <?> getClss (Type type ) {
163
+ Class <?> target = null ;
163
164
164
165
if (type instanceof Class ) {
165
- target = (Class ) type ;
166
+ target = (Class <?> ) type ;
166
167
} else if (type instanceof ParameterizedType ) {
167
168
Type rawType = ((ParameterizedType ) type ).getRawType ();
168
169
if (rawType instanceof Class ) {
169
- target = (Class ) rawType ;
170
+ target = (Class <?> ) rawType ;
170
171
}
171
172
}
172
173
@@ -176,9 +177,14 @@ private static Class getClss(Type type) {
176
177
return target ;
177
178
}
178
179
179
- private class TypeAndWriter {
180
- public Type type ;
181
- public BiConsumer <Object , Object > writer ;
180
+ private static class TypeAndWriter {
181
+ private final Type type ;
182
+ private final BiConsumer <Object , Object > writer ;
183
+
184
+ public TypeAndWriter (Type type , BiConsumer <Object , Object > writer ) {
185
+ this .type = type ;
186
+ this .writer = writer ;
187
+ }
182
188
}
183
189
184
190
}
0 commit comments