Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
129 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
src/test/java/com/alibaba/json/bvt/annotation/AnnotationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.