Java Annotations. Is there a helper library for reading / processing annotations?

I am starting to make heavy use of Java annotations. One example is a method with annotations and converting them to a telnet-based command line command. I do this by parsing annotations and pasting into a jpt parser.

However, I do a lot of them manually. For example, processing the annotation of a method parameter.

Method method = ... //; Class[] parameters = method.getParamterTypes(); Annotation[][] annotations = method.getparamterAnnotations(); for( int i = 0; i < parameters.length; i++ ) { // iterate through the annotation , see if each param has specific annotation ,etc. } 

It is very redundant and tiring.

Is there any open source project that helps handle annotations?

+7
java annotations
source share
3 answers

We use this to search for a specific annotation:

 for (Field field : clazz.getDeclaredFields()) { if (field.isAnnotationPresent(MyAnnotation.class)) { field.setAccessible(true); String fieldName = field.getName(); Object fieldValue = field.get(myObj); field.setAccessible(false); } } 
+6
source share

Any project will contain the same boilerplate code. Why don't you make your own utility?

I am sure you can use ASM:

+1
source share

Not quite sure what help you need for annotations, but I suggest you take a look at the Annox project. Here is the API .

With Annox, you can, for example, read annotations in

  • Xannotation
  • XAnnotationField
  • XPackage
  • Xclass
  • XConstructor
  • Xfield
  • Xmethod
  • XParameter

which can better suit your needs. You can read arbitrary annotations from XML, run visitors on them, etc.

0
source share

All Articles