forked from yigit/greenDAO
-
Notifications
You must be signed in to change notification settings - Fork 8
How to Add Annotations
yigit edited this page Aug 21, 2012
·
2 revisions
In many places in java, annotations are necessary. One common use case for Entity objects is using annotations for JSON serialization.
In its simplest form, it looks like this:
note.addStringProperty("text").notNull().addSetterAnnotation(new Annotation("test5", "key1", "value1"));
You can also add annotations to entities
note.addAnnotation(new Annotation("Test"));
note.addAnnotation(new Annotation("Test2", "singleValueeee"));
note.addAnnotation(new Annotation("Test3", "key1", "5", "key2","\"value2\""));
note.addAnnotation(new Annotation("Test4", "key1", null, "key2","\"value2\""));
Generated code will look like this: (complete sample: NoteBase )
@Test( key1 = value1 )
public void setDate(java.util.Date date) {
this.date = date;
}
If you have annotations that you use a lot (e.g. JSONProperty) it is best to subclass Annotation to make code generation cleaner.
private static class JSonProperty extends Annotation {
public JSonProperty(String param) {
super("JsonProperty", "\"" + param + "\"");
}
@Override
public String getPackage() {
return "org.codehaus.jackson.annotate.*";
}
}
Than you can use it as follows:
foo.addStringProperty("fooBar").addSetterGetterAnnotation(new JSonProperty("foo_bar"));