From 079669fae472be80aa1b83b01ef0696f365d07be Mon Sep 17 00:00:00 2001 From: wenshao Date: Thu, 15 Mar 2018 14:18:47 +0800 Subject: [PATCH] bug fixed for kotlin support,for issue #1750 --- .../alibaba/fastjson/util/JavaBeanInfo.java | 62 +++++++++--------- .../alibaba/json/bvt/kotlin/Issue1750.java | 42 ++++++++++++ .../kotlin/Issue1750_ProcessBO.clazz | Bin 0 -> 2686 bytes 3 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/kotlin/Issue1750.java create mode 100644 src/test/resources/kotlin/Issue1750_ProcessBO.clazz diff --git a/src/main/java/com/alibaba/fastjson/util/JavaBeanInfo.java b/src/main/java/com/alibaba/fastjson/util/JavaBeanInfo.java index ce1d6e23d6..584d1b7eff 100644 --- a/src/main/java/com/alibaba/fastjson/util/JavaBeanInfo.java +++ b/src/main/java/com/alibaba/fastjson/util/JavaBeanInfo.java @@ -121,42 +121,44 @@ public JavaBeanInfo(Class clazz, // if (creatorConstructor != null) { this.creatorConstructorParameterTypes = creatorConstructor.getParameterTypes(); - boolean match; - if (creatorConstructorParameterTypes.length != fields.length) { - match = false; - } else { - match = true; - for (int i = 0; i < creatorConstructorParameterTypes.length; i++) { - if (creatorConstructorParameterTypes[i] != fields[i].fieldClass) { - match = false; - break; - } - } - } - if (!match) { - boolean kotlin = TypeUtils.isKotlin(clazz); - if (kotlin) { - this.creatorConstructorParameters = TypeUtils.getKoltinConstructorParameters(clazz); - Annotation[][] paramAnnotationArrays = creatorConstructor.getParameterAnnotations(); - for (int i = 0; i < creatorConstructorParameters.length && i < paramAnnotationArrays.length; ++i) { - Annotation[] paramAnnotations = paramAnnotationArrays[i]; - JSONField fieldAnnotation = null; - for (Annotation paramAnnotation : paramAnnotations) { - if (paramAnnotation instanceof JSONField) { - fieldAnnotation = (JSONField) paramAnnotation; - break; - } + boolean kotlin = TypeUtils.isKotlin(clazz); + if (kotlin) { + this.creatorConstructorParameters = TypeUtils.getKoltinConstructorParameters(clazz); + + Annotation[][] paramAnnotationArrays = creatorConstructor.getParameterAnnotations(); + for (int i = 0; i < creatorConstructorParameters.length && i < paramAnnotationArrays.length; ++i) { + Annotation[] paramAnnotations = paramAnnotationArrays[i]; + JSONField fieldAnnotation = null; + for (Annotation paramAnnotation : paramAnnotations) { + if (paramAnnotation instanceof JSONField) { + fieldAnnotation = (JSONField) paramAnnotation; + break; } - if (fieldAnnotation != null) { - String fieldAnnotationName = fieldAnnotation.name(); - if (fieldAnnotationName.length() > 0) { - creatorConstructorParameters[i] = fieldAnnotationName; - } + } + if (fieldAnnotation != null) { + String fieldAnnotationName = fieldAnnotation.name(); + if (fieldAnnotationName.length() > 0) { + creatorConstructorParameters[i] = fieldAnnotationName; } } + } + } else { + boolean match; + if (creatorConstructorParameterTypes.length != fields.length) { + match = false; } else { + match = true; + for (int i = 0; i < creatorConstructorParameterTypes.length; i++) { + if (creatorConstructorParameterTypes[i] != fields[i].fieldClass) { + match = false; + break; + } + } + } + + if (!match) { this.creatorConstructorParameters = ASMUtils.lookupParameterNames(creatorConstructor); } } diff --git a/src/test/java/com/alibaba/json/bvt/kotlin/Issue1750.java b/src/test/java/com/alibaba/json/bvt/kotlin/Issue1750.java new file mode 100644 index 0000000000..4db82b3f59 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/kotlin/Issue1750.java @@ -0,0 +1,42 @@ +package com.alibaba.json.bvt.kotlin; + +import com.alibaba.fastjson.JSON; +import junit.framework.TestCase; +import org.apache.commons.io.IOUtils; + +import java.io.IOException; +import java.io.InputStream; + +public class Issue1750 extends TestCase { + public void test_user() throws Exception { + ExtClassLoader classLoader = new ExtClassLoader(); + Class clazz = classLoader.loadClass("Issue1750_ProcessBO"); + + String json = "{\n" + + "\t\"masterId\": \"1111111111111\",\n" + + "\t\"processId\": \"222222222222222\",\n" + + "\t\"taskId\": \"33333333333333\",\n" + + "\t\"taskName\": \"44444444444444\"\n" + + "}"; + Object obj = JSON.parseObject(json, clazz); + String result = JSON.toJSONString(obj); + System.out.println(result); + assertEquals("{\"masterId\":\"1111111111111\",\"processId\":\"222222222222222\",\"taskId\":\"33333333333333\",\"taskName\":\"44444444444444\"}", result); + } + + private static class ExtClassLoader extends ClassLoader { + + public ExtClassLoader() throws IOException { + super(Thread.currentThread().getContextClassLoader()); + + { + byte[] bytes; + InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/Issue1750_ProcessBO.clazz"); + bytes = IOUtils.toByteArray(is); + is.close(); + + super.defineClass("Issue1750_ProcessBO", bytes, 0, bytes.length); + } + } + } +} diff --git a/src/test/resources/kotlin/Issue1750_ProcessBO.clazz b/src/test/resources/kotlin/Issue1750_ProcessBO.clazz new file mode 100644 index 0000000000000000000000000000000000000000..473516dc98eae612f98d835f3acbd4d32e312269 GIT binary patch literal 2686 zcmb7GSyS6q6#lO8B5VP&SxstEmc-aBVhK&#kOdObn3yF%++=v@$hHasTVi}=n)HdM z{)7I3KJ~GkDN`m79fp_A^e6Pkbb79248|S?W{i%`Jch0%upZ`An1z-m67&_CO zSB&Y0b5q}~m+id4`Qlm-j6vPg4|T1mTRYlXZqLX&L9{T)C7nA)IbC3gW-dH7ow8}| zJYpEm*ySB<&v0^O-L$x-TbAwUj%m~Ts_m>+ibe7Y?-0n4)#^Q1Ps5!cR+!5$LV`x3_iX{r65S!K7r3(rMJl;N+ zF+es#P_r=v9G&lrm{y9}a^DqJOdS5c&`#(uVP|ZXbMtZ#JG%8OO zm(z@jq9tWKWQeYOE{x%H#5DB4u85%oB!|QPBePcpQQcq`$rl zsoLU=g}KQ}G8>{+rj(IPwUJj0M=$7(POT)B4qIp(nRv94nsZeMRbpWKv{WFz{yja4 zhNM0zB&nxIA^J&bdq|QaeWyn=(iB@v$UV`3D)mcKEhK$a>95LwkY~RHBsI9`ccr{6 ztDR!?RnJDh7x(V$r%AH4Q}%wJ+^U9#d~#S-$n2)8H@CUwcGv9gQg7~X&8lm5H?KEG zTys=a&VCJai4)E+q5z6V&j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