2012-09-04 1 views
5

Come funziona l'annotazione con Java? E come posso creare annotazioni personalizzate in questo modo:Creazione di annotazioni personalizzate

@Entity(keyspace=':') 
class Student 
{ 
    @Id 
    @Attribute(value="uid") 
    Long Id; 
    @Attribute(value="fname") 
    String firstname; 
    @Attribute(value="sname") 
    String surname; 

    // Getters and setters 
} 

In sostanza, quello che ho bisogno di avere è questa POJO essere serializzato come questo quando persisteva:

dao.persist(new Student(0, "john", "smith")); 
dao.persist(new Student(1, "katy", "perry")); 

Tale che, l'attuale generata/oggetto persistente è un Map<String,String> come questo:

uid:0:fname -> john 
uid:0:sname -> smith 
uid:1:fname -> katy 
uid:1:sname -> perry 

Tutte le idee come implementare questa?

risposta

3

Se si creano annotazioni personalizzate, è necessario utilizzare Reflection API Example Here per elaborarle. È possibile refere How to declare annotation. Ecco come si presenta la dichiarazione di annotazione di esempio in java.

import java.lang.annotation.*; 

/** 
* Indicates that the annotated method is a test method. 
* This annotation should be used only on parameterless static methods. 
*/ 
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface Test { } 

Retention e Target sono conosciuti come meta-annotations.

RetentionPolicy.RUNTIME indica che si desidera conservare l'annotazione in fase di esecuzione ed è possibile accedervi in ​​fase di esecuzione.

ElementType.METHOD indica che è possibile dichiarare annotazioni solo su metodi Allo stesso modo è possibile configurare l'annotazione per livello di classe, livello variabile membro ecc

Ogni classe Reflection ha metodi per ottenere le annotazioni che vengono dichiarati.

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) getAnnotation(Class<T> annotationClass) 
Returns this element's annotation for the specified type if such an annotation is present, else null. 

public Annotation[] getDeclaredAnnotations() 
Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. (Returns an array of length zero if no annotations are directly present on this element.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers. 

Troverete questi metodi attuali per Field, Method, Class classi.

e.g.To recuperare le annotazioni presenti sulla classe specificata in fase di esecuzione

Annotation[] annos = ob.getClass().getAnnotations(); 
+0

posso ottenere l'annotazione con getAnnotations() tuttavia come posso ottenere quale campo o il metodo relativi alla annotazione? – xybrek

+0

Si sta chiamando 'getAnnotations()' su 'Field',' Method' o 'Class' solo in modo che questo sia il campo relativo a queste annotazioni. Another betther [esempio] (http://tutorials.jenkov.com/java-reflection/annotations.html) –

+0

Giusto, ho finito il mio codice per questa funzione – xybrek