2727import java .lang .reflect .Field ;
2828import java .lang .reflect .Method ;
2929import java .lang .reflect .Type ;
30- import java .util .HashMap ;
3130import java .util .LinkedHashMap ;
3231import java .util .Map ;
3332
@@ -65,7 +64,7 @@ protected <T> T doRead(Object o, Class<T> clazz) {
6564 try {
6665 instance = clazz .newInstance ();
6766 } catch (Throwable throwable ) {
68- throw new YmlParseException (String .format ("clazz - %s instance error" , clazz .getName ()), throwable );
67+ throw new YmlParseException (String .format ("clazz '%s' create instance error " , clazz .getName ()), throwable );
6968 }
7069
7170 Class <?> raw = clazz ;
@@ -80,12 +79,12 @@ protected <T> T doRead(Object o, Class<T> clazz) {
8079 YmlSerializer ymlSerializer = field .getAnnotation (YmlSerializer .class );
8180
8281 //filter no annotations field
83- if (noAnnotationField ( field )) {
82+ if (isNullOrEmpty ( ymlSerializer )) {
8483 continue ;
8584 }
8685
8786 //filter ignore field
88- if (ignoreField ( field )) {
87+ if (isIgnore ( ymlSerializer )) {
8988 continue ;
9089 }
9190 setValueToField (o , field , instance , ymlSerializer , clazz );
@@ -96,12 +95,12 @@ protected <T> T doRead(Object o, Class<T> clazz) {
9695 YmlSerializer ymlSerializer = method .getAnnotation (YmlSerializer .class );
9796
9897 //filter no annotations method
99- if (noAnnotationMethod ( method )) {
98+ if (isNullOrEmpty ( ymlSerializer )) {
10099 continue ;
101100 }
102101
103102 //filter ignore method
104- if (ignoreMethod ( method )) {
103+ if (isIgnore ( ymlSerializer )) {
105104 continue ;
106105 }
107106
@@ -120,9 +119,9 @@ private <T> void setValueToField(Object o, Field field, T instance, YmlSerialize
120119 Object obj = ((Map ) o ).get (getAnnotationMappingName (field .getName (), ymlSerializer ));
121120
122121 // required field
123- if (requiredField (field )) {
122+ if (requiredField (ymlSerializer )) {
124123 if (obj == null ) {
125- throw new YmlParseException (String .format ("required field - %s " , field .getName ()));
124+ throw new YmlParseException (String .format ("field '%s' is missing " , field .getName ()));
126125 }
127126 }
128127
@@ -163,18 +162,18 @@ protected <T> Object doWrite(Field field, T t, YmlSerializer ymlSerializer) {
163162 try {
164163 return TypeAdaptorFactory .getAdaptor (fieldType ).write (field .get (t ));
165164 } catch (Throwable throwable ) {
166- throw new YmlParseException (String .format ("field - %s get error" , field .getName ()), throwable );
165+ throw new YmlParseException (String .format ("field '%s' is get error" , field .getName ()), throwable );
167166 }
168167 }
169168
170169 //annotation provider adaptor
171170 if (ymlSerializer .adaptor () != EmptyAdapter .class ) {
172171 try {
173- YmlAdaptor instance = ( YmlAdaptor ) ymlSerializer .adaptor ().newInstance ();
172+ YmlAdaptor instance = ymlSerializer .adaptor ().newInstance ();
174173 return instance .write (field .get (t ));
175174 } catch (Throwable throwable ) {
176175 throw new YmlParseException (
177- String .format ("create instance adaptor - %s error" , ymlSerializer .adaptor ().getName ()), throwable );
176+ String .format ("create instance '%s' adaptor error" , ymlSerializer .adaptor ().getName ()), throwable );
178177 }
179178 }
180179
@@ -193,11 +192,12 @@ public static <T> void validate(Field field, T instance, YmlSerializer ymlSerial
193192 value = field .get (instance );
194193 } catch (Throwable throwable ) {
195194 throw new YmlFormatException (String
196- .format ("validate - %s instance error - %s" , ymlSerializer .validator ().getName (), throwable ));
195+ .format ("field '%s' is validate error %s" , ymlSerializer .validator ().getName (),
196+ throwable .getMessage ()));
197197 }
198198
199199 if (validator .validate (value ) == false ) {
200- throw new YmlFormatException (String .format ("field - %s , validate - error" , field .getName ()));
200+ throw new YmlFormatException (String .format ("field '%s' is validate error" , field .getName ()));
201201 }
202202
203203 }
@@ -254,7 +254,7 @@ public <T> Object doWrite(T clazz) {
254254 // 获取 field 对应的值
255255 YmlSerializer ymlSerializer = field .getAnnotation (YmlSerializer .class );
256256
257- if (noAnnotationField ( field )) {
257+ if (isNullOrEmpty ( ymlSerializer )) {
258258 continue ;
259259 }
260260
@@ -267,7 +267,7 @@ public <T> Object doWrite(T clazz) {
267267 // 获取 field 对应的值
268268 YmlSerializer ymlSerializer = method .getAnnotation (YmlSerializer .class );
269269
270- if (noAnnotationMethod ( method )) {
270+ if (isNullOrEmpty ( ymlSerializer )) {
271271 continue ;
272272 }
273273
@@ -300,12 +300,10 @@ private String getAnnotationMappingName(String s, YmlSerializer ymlSerializer) {
300300
301301 /**
302302 * detect required field from annotation
303- *
304- * @param field field
305- * @return True or false
303+ * @param annotation
304+ * @return
306305 */
307- private Boolean requiredField (Field field ) {
308- YmlSerializer annotation = field .getAnnotation (YmlSerializer .class );
306+ private Boolean requiredField (YmlSerializer annotation ) {
309307 if (annotation == null ) {
310308 return false ;
311309 }
@@ -318,10 +316,11 @@ private Boolean requiredField(Field field) {
318316 }
319317
320318 /**
321- * filter no annotation field
319+ * detect annotation has or not
320+ * @param annotation
321+ * @return
322322 */
323- private Boolean noAnnotationField (Field field ) {
324- YmlSerializer annotation = field .getAnnotation (YmlSerializer .class );
323+ private Boolean isNullOrEmpty (YmlSerializer annotation ) {
325324 if (annotation == null ) {
326325 return true ;
327326 }
@@ -330,10 +329,11 @@ private Boolean noAnnotationField(Field field) {
330329 }
331330
332331 /**
333- * filter ignore field
332+ * filter ignore action
333+ * @param annotation
334+ * @return
334335 */
335- private Boolean ignoreField (Field field ) {
336- YmlSerializer annotation = field .getAnnotation (YmlSerializer .class );
336+ private Boolean isIgnore (YmlSerializer annotation ) {
337337 if (annotation .ignore ()) {
338338 return true ;
339339 }
@@ -342,55 +342,6 @@ private Boolean ignoreField(Field field) {
342342 }
343343
344344
345- /**
346- * filter no annotation method
347- */
348- private Boolean noAnnotationMethod (Method method ) {
349- YmlSerializer annotation = method .getAnnotation (YmlSerializer .class );
350- if (annotation == null ) {
351- return true ;
352- }
353-
354- return false ;
355- }
356-
357- /**
358- * filter ignore method
359- */
360- private Boolean ignoreMethod (Method method ) {
361- YmlSerializer annotation = method .getAnnotation (YmlSerializer .class );
362- if (annotation .ignore ()) {
363- return true ;
364- }
365-
366- return false ;
367- }
368-
369-
370- /**
371- * get all fields
372- *
373- * @return Map<name, field>
374- */
375- private Map <String , Field > allFields (Class <?> clazz ) {
376- Map <String , Field > map = new HashMap <>();
377-
378- while (true ) {
379- if (clazz == null ) {
380- break ;
381- }
382-
383- for (Field field : clazz .getDeclaredFields ()) {
384- map .put (field .getName (), field );
385- }
386-
387- clazz = clazz .getSuperclass ();
388- }
389-
390- return map ;
391- }
392-
393-
394345 private String fieldNameForSetterGetter (String fieldName ) {
395346 return Character .toUpperCase (fieldName .charAt (0 )) + fieldName .substring (1 );
396347 }
@@ -411,10 +362,6 @@ private Method getGetterMethod(Field field, Class<?> clazz) {
411362 return getMethod (field , clazz , "get" );
412363 }
413364
414- private Method getSetterMethod (Field field , Class <?> clazz ) {
415- return getMethod (field , clazz , "set" );
416- }
417-
418365 /**
419366 * get method
420367 */
0 commit comments