martedì 13 luglio 2010

Annotation injection thru Javassist

Today I have found an introspection problem within the Javassist library, I'm not sure is a problem of the library or of the bytecode manipulation. I found the problem while working around WhiteCat, that injects Java annotation into ordinary classes at run-time. Of course the injection works fine, but it is like the annotation is no more visible in an "ordinary" way. In fact, doing introspection against a manipulated class, I found that the annotations are there, but with a class name like $Proxy. Why is this problematic? Simply because it does not allow for a class-to-class comparison when working with annotations, and more important it does not allow introspection against the annotation itself (e.g., to get the annotations of an annotation).

The solution in order to do regular introspection is the following:
  1. get the annotation and its toString() value;
  2. manipulate the string value in order to get the annotation fully qualified name;
  3. load the annotation class from its name thru reflection;
  4. work on the obtained class.
Not really clean, but good enough for WhiteCat to work.

Nessun commento: