Skip to content

Commit

Permalink
support abstract method annotation. issue #744 #911
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 20, 2016
1 parent fa72505 commit a250461
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Expand Up @@ -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;
}

Expand Down
26 changes: 26 additions & 0 deletions 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);
}
}
37 changes: 37 additions & 0 deletions 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;
}
}
}

0 comments on commit a250461

Please sign in to comment.