From c2b2c7e8d2c5290a65ebd8ecfe5ded233ccbeca3 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 20 Nov 2016 14:57:29 +0800 Subject: [PATCH] support abstract method annotation. issue #744 #911 --- .../com/alibaba/fastjson/util/TypeUtils.java | 35 ++++++++++++++++++ .../com/alibaba/json/bvt/bug/Issue744.java | 26 +++++++++++++ .../com/alibaba/json/bvt/bug/Issue744_1.java | 37 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/test/java/com/alibaba/json/bvt/bug/Issue744.java create mode 100644 src/test/java/com/alibaba/json/bvt/bug/Issue744_1.java diff --git a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java index 9a2d9b465d..cc63d04c17 100755 --- a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java +++ b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java @@ -1162,6 +1162,41 @@ public static JSONField getSupperMethodAnnotation(Class clazz, Method method) } } + Class superClass = clazz.getSuperclass(); + if (superClass == null) { + return null; + } + + if (Modifier.isAbstract(superClass.getModifiers())) { + Class[] types = method.getParameterTypes(); + + for (Method interfaceMethod : superClass.getMethods()) { + Class[] interfaceTypes = interfaceMethod.getParameterTypes(); + if (interfaceTypes.length != types.length) { + continue; + } + if (!interfaceMethod.getName().equals(method.getName())) { + continue; + } + boolean match = true; + for (int i = 0; i < types.length; ++i) { + if (!interfaceTypes[i].equals(types[i])) { + match = false; + break; + } + } + + if (!match) { + continue; + } + + JSONField annotation = interfaceMethod.getAnnotation(JSONField.class); + if (annotation != null) { + return annotation; + } + } + } + return null; } diff --git a/src/test/java/com/alibaba/json/bvt/bug/Issue744.java b/src/test/java/com/alibaba/json/bvt/bug/Issue744.java new file mode 100644 index 0000000000..586e023d52 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Issue744.java @@ -0,0 +1,26 @@ +package com.alibaba.json.bvt.bug; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.annotation.JSONField; +import junit.framework.TestCase; + +import java.util.Date; + +/** + * Created by wenshao on 2016/11/13. + */ +public class Issue744 extends TestCase { + + public static class Model { + @JSONField(format="yyyy-MM-dd'T'HH:mm:ss") + public Date date; + } + + public void test() { + String text = "{\"date\":\"9999-09-08T00:00:00\"}"; + Model model =JSON.parseObject(text, Model.class); + + String text2 = JSON.toJSONString(model); + System.out.println(text2); + } +} \ No newline at end of file diff --git a/src/test/java/com/alibaba/json/bvt/bug/Issue744_1.java b/src/test/java/com/alibaba/json/bvt/bug/Issue744_1.java new file mode 100644 index 0000000000..f064326806 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Issue744_1.java @@ -0,0 +1,37 @@ +package com.alibaba.json.bvt.bug; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.annotation.JSONField; +import junit.framework.TestCase; + +/** + * Created by wenshao on 2016/11/13. + */ +public class Issue744_1 extends TestCase { + + public void test_for_issue() throws Exception { + C c = new C(); + c.setName("name"); + + String json = JSON.toJSONString(c); + assertEquals("{}", json); + } + + public static abstract class B { + @JSONField(serialize = false, deserialize = false) + public abstract String getName(); + + } + + public static class C extends B { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} \ No newline at end of file