support abstract method annotation. issue #744 #911

This commit is contained in:
wenshao
2016-11-20 14:57:29 +08:00
parent 8c305900fa
commit c2b2c7e8d2
3 changed files with 98 additions and 0 deletions
@@ -1162,6 +1162,41 @@ public class TypeUtils {
}
}
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;
}
@@ -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);
}
}
@@ -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;
}
}
}