From d3687b7deafcbfccc22bf92fe280e8bf54ba1bd2 Mon Sep 17 00:00:00 2001 From: wenshao Date: Wed, 10 May 2017 03:44:50 +0800 Subject: [PATCH] 1.2.33-SNAPSHOT --- .../json/bvt/annotation/AnnotationTest.java | 97 +++++++++++++++++++ .../java/demo/annotations/AnnotationTest.java | 55 ----------- src/test/java/demo/annotations/Bob.java | 49 ---------- src/test/java/demo/annotations/Person.java | 8 -- .../java/demo/annotations/PersonInfo.java | 17 ---- 5 files changed, 97 insertions(+), 129 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/annotation/AnnotationTest.java delete mode 100644 src/test/java/demo/annotations/AnnotationTest.java delete mode 100644 src/test/java/demo/annotations/Bob.java delete mode 100644 src/test/java/demo/annotations/Person.java delete mode 100644 src/test/java/demo/annotations/PersonInfo.java diff --git a/src/test/java/com/alibaba/json/bvt/annotation/AnnotationTest.java b/src/test/java/com/alibaba/json/bvt/annotation/AnnotationTest.java new file mode 100644 index 0000000000..3f70173f95 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/annotation/AnnotationTest.java @@ -0,0 +1,97 @@ +package com.alibaba.json.bvt.annotations; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import junit.framework.TestCase; +import sun.reflect.annotation.AnnotationType; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Iterator; +import java.util.Map; + +/** + * Created by Helly on 2017/04/10. + */ +public class AnnotationTest extends TestCase { + + public void test_annoation() throws Exception { + Bob bob = new Bob("Bob", 30, true); + JSONObject obj = (JSONObject) JSON.toJSON(bob); + assertEquals(3, obj.size()); + assertEquals(Boolean.TRUE, obj.get("sex")); + assertEquals("Bob", obj.get("name")); + assertEquals(new Integer(30), obj.get("age")); + + PersonInfo info = Bob.class.getAnnotation(PersonInfo.class); + obj = (JSONObject) JSON.toJSON(info); + + assertEquals(3, obj.size()); + assertEquals(Boolean.TRUE, obj.get("sex")); + assertEquals("Bob", obj.get("name")); + assertEquals(new Integer(30), obj.get("age")); + } + + @PersonInfo(name = "Bob", age = 30, sex = true) + public static class Bob implements Person { + private String name; + private int age; + private boolean sex; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public boolean isSex() { + return sex; + } + + public void setSex(boolean sex) { + this.sex = sex; + } + + public Bob() { + } + + public Bob(String name, int age, boolean sex) { + this(); + this.name = name; + this.age = age; + this.sex = sex; + } + + public void hello() { + System.out.println("world"); + } + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public static @interface PersonInfo { + String name(); + int age(); + boolean sex(); + } + + + public static interface Person { + void hello(); + } + +} diff --git a/src/test/java/demo/annotations/AnnotationTest.java b/src/test/java/demo/annotations/AnnotationTest.java deleted file mode 100644 index 4444eb5429..0000000000 --- a/src/test/java/demo/annotations/AnnotationTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package demo.annotations; - -import com.alibaba.fastjson.JSON; -import sun.reflect.annotation.AnnotationType; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Iterator; -import java.util.Map; - -/** - * Created by Helly on 2017/04/10. - */ -public class AnnotationTest { - - public static void main(String[] args) { - print(); - - print2(); - - } - - private static void print2() { - PersonInfo info = Bob.class.getAnnotation(PersonInfo.class); - Class clazz = info.getClass(); - Class[] interfaces = clazz.getInterfaces(); - if (interfaces.length == 1 && interfaces[0].isAnnotation()) { - AnnotationType type = AnnotationType.getInstance(interfaces[0]); - Map members = type.members(); - Iterator> iterator = members.entrySet().iterator(); - Map.Entry entry; - Object val; - while (iterator.hasNext()) { - entry = iterator.next(); - try { - val = entry.getValue().invoke(info); - System.out.println(entry.getKey() + "=" + val); - } catch (IllegalAccessException e) { - //skip - } catch (InvocationTargetException e) { - //skip - } - } - } - } - - private static void print() { - Bob bob = new Bob("Bob", 30, true); - Object obj = JSON.toJSON(bob); - System.out.println(obj.toString()); - PersonInfo info = Bob.class.getAnnotation(PersonInfo.class); - obj = JSON.toJSON(info); - System.out.println(obj.toString()); - } -} diff --git a/src/test/java/demo/annotations/Bob.java b/src/test/java/demo/annotations/Bob.java deleted file mode 100644 index e08cbbc51f..0000000000 --- a/src/test/java/demo/annotations/Bob.java +++ /dev/null @@ -1,49 +0,0 @@ -package demo.annotations; - -/** - * Created by Helly on 2017/04/10. - */ -@PersonInfo(name = "Bob", age = 30, sex = true) -public class Bob implements Person { - private String name; - private int age; - private boolean sex; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public boolean isSex() { - return sex; - } - - public void setSex(boolean sex) { - this.sex = sex; - } - - public Bob() { - } - - public Bob(String name, int age, boolean sex) { - this(); - this.name = name; - this.age = age; - this.sex = sex; - } - - public void hello() { - System.out.println("world"); - } -} diff --git a/src/test/java/demo/annotations/Person.java b/src/test/java/demo/annotations/Person.java deleted file mode 100644 index ac747595e7..0000000000 --- a/src/test/java/demo/annotations/Person.java +++ /dev/null @@ -1,8 +0,0 @@ -package demo.annotations; - -/** - * Created by Helly on 2017/04/10. - */ -public interface Person { - void hello(); -} diff --git a/src/test/java/demo/annotations/PersonInfo.java b/src/test/java/demo/annotations/PersonInfo.java deleted file mode 100644 index 2cdcdf43d5..0000000000 --- a/src/test/java/demo/annotations/PersonInfo.java +++ /dev/null @@ -1,17 +0,0 @@ -package demo.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by Helly on 2017/04/10. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface PersonInfo { - String name(); - int age(); - boolean sex(); -}