Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have to @Override an abstract function and offer an array of strings (defining searchable fields for my datatable):

@Override
public String[] getQueryFields() {
    return new String[] { "email", "firstname", "lastname", "lastLogin"};
}

When I look on my code I could imagine that I just reference a class and annotate the JPA column fields, e.g. by @Searchable to signal this capability and build my array:

@Column
@Getter
@Setter
...
@Searchable
private String email;

Is there a way to handle this?

share|improve this question
    
You can certainly do it although it's a pain to write by hand; if you can afford an external library, have a look at Google reflections. –  fge Mar 11 at 8:50
add comment

1 Answer

You can certainly do anything that you want with Reflection. but you should try and explore for other options too. Here is a a crude example i just wrote. it will give you the fields that are annotated by @AnAnnotation.

@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface AnAnnotation {

}


public class AnnotExample {

   @AnAnnotation
   private String b;

   @AnAnnotation
   private String c;

   public static void getAnnotatedFields(final Class clazz) {
      clazz.getAnnotation(AnAnnotation.class);

      final Field[] declaredFields = clazz.getDeclaredFields();
      for (final Field field : declaredFields) {
         final AnAnnotation annotation2 = field.getAnnotation(AnAnnotation.class);
         if (annotation2 != null) {
            System.out.println(field.getName());
         }

      }
   }

   public static void main(final String[] args) {
      AnnotExample.getAnnotatedFields(AnnotExample.class);
   }

}
share|improve this answer
    
Hi, thanks a lot. But what do you mean with "...you should try and explore for other options too"? –  John Rumpel Mar 11 at 17:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.