Pegar atributo de Annotation

1 resposta
wagnerlegiao

Pessoal, estou criando uma annotation básica aqui. da seguinte forma:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

public @ interface ValidateRange {
public int max();
public int min();

}

e o método pra validar é :

public void validate(Object o) throws Exception{

Class classe = o.getClass();

Field[] fields = classe.getDeclaredFields();

for(Field f : fields){			

if(f.isAnnotationPresent(ValidateRange.class)){

f.setAccessible(true);

if(f.get(o).toString().length() > CAMPO_MAX_DA_ANNOTATION)

throw new Exception("Campo "+f.getName()+bla bla bla);

}

}

}

Eis o problema, nao consigo pegar o CAMPO_MAX_DA_ANNOTATION, ou seja, os atributos da annotation, pra poder manipulá-los dentro do validate, alguém poderia me ajudar?
abraço.

1 Resposta

wagnerlegiao

acabei resolvendo aki. ficou assim:

public void validate(Object o) throws Exception{

Class classe = o.getClass();

Field[] fields = classe.getDeclaredFields();

for(Field f : fields){			

if(f.isAnnotationPresent(ValidateRange.class)){

f.setAccessible(true);

ValidateRange ann =  f.getAnnotation(ValidateRange.class);				

if(f.get(o).toString().length() > ann.max())

throw new Exception("Campo "+f.getName()+mensagem);

if(f.get(o).toString().length() < ann.min())

throw new Exception(Campo +f.getName()+ …”);

}

}

}
Criado 6 de agosto de 2008
Ultima resposta 6 de ago. de 2008
Respostas 1
Participantes 1