Skip to content

Commit

Permalink
bug fixed for kotlin support,for issue #1750
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 15, 2018
1 parent c254769 commit 079669f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
62 changes: 32 additions & 30 deletions src/main/java/com/alibaba/fastjson/util/JavaBeanInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/alibaba/json/bvt/kotlin/Issue1750.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Binary file not shown.

0 comments on commit 079669f

Please sign in to comment.