1818
1919import java .io .IOException ;
2020import java .util .Iterator ;
21+ import java .util .function .Predicate ;
2122
2223import feign .codec .EncodeException ;
2324import feign .form .multipart .AbstractWriter ;
2425import feign .form .multipart .Output ;
26+ import feign .form .util .PojoUtil ;
2527
2628import org .springframework .http .MediaType ;
2729import org .springframework .web .multipart .MultipartFile ;
@@ -36,12 +38,8 @@ public abstract class PojoSerializationWriter extends AbstractWriter {
3638
3739 @ Override
3840 public boolean isApplicable (Object object ) {
39- boolean isMultipartFileOrCollection = (object instanceof MultipartFile )
40- || (object instanceof MultipartFile []);
41- boolean isUserPojoOrCollection = isUserPojoCollection (object )
42- || isUserPojo (object );
43-
44- return !isMultipartFileOrCollection && isUserPojoOrCollection ;
41+ return !isTypeOrCollection (object , o -> o instanceof MultipartFile )
42+ && isTypeOrCollection (object , PojoUtil ::isUserPojo );
4543 }
4644
4745 @ Override
@@ -65,28 +63,20 @@ public void write(Output output, String key, Object object) throws EncodeExcepti
6563
6664 protected abstract String serialize (Object object ) throws IOException ;
6765
68- private boolean isUserPojoCollection (Object object ) {
69- // TODO Refactor!
66+ private boolean isTypeOrCollection (Object object , Predicate <Object > isType ) {
7067 if (object .getClass ().isArray ()) {
7168 Object [] array = (Object []) object ;
7269
73- return array .length > 1 && isUserPojo (array [0 ]);
74- }
75-
76- if (!(object instanceof Iterable )) {
77- return false ;
70+ return array .length > 1 && isType .test (array [0 ]);
7871 }
72+ else if (object instanceof Iterable ) {
73+ Iterable <?> iterable = (Iterable <?>) object ;
74+ Iterator <?> iterator = iterable .iterator ();
7975
80- Iterable <?> iterable = (Iterable <?>) object ;
81- Iterator <?> iterator = iterable .iterator ();
82-
83- if (iterator .hasNext ()) {
84- Object next = iterator .next ();
85-
86- return !(next instanceof MultipartFile ) && isUserPojo (next );
76+ return iterator .hasNext () && isType .test (iterator .next ());
8777 }
8878 else {
89- return false ;
79+ return isType . test ( object ) ;
9080 }
9181 }
9282
0 commit comments