0

Read this answer on the jboss community regarding adding an annotation to a parameter in a method. Stuck at getting the parameter or in Javassist terms the attribute.

Given class XYZ:

public class XYZ {
   public void changes(String whatever, Integer flag) {
   }
}

With the following code: ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.get(XYZ.class.getName());

CtMethod m = clazz.getDeclaredMethod("changes");
CtClass[] parameters = m.getParameterTypes();  // shows 2 parameter (one String, other Integer)

AttributeInfo paramAtrributeInfo = m.getMethodInfo().getAttribute("whatever");

paramAttributeInfo is null. I see 2 attributes when debugging but these have concrete calls LocalVariableAttribute and LineNumberAttribute.

Can't get a handle (attribute) to add my parameter annotation to. Ideas?

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Matthew Campbell
  • 1,864
  • 3
  • 24
  • 51
  • 1
    Of course, it is `null`. The fact that you want to add an annotation implies that the annotation does not exist yet. So even if you replace the string `"whatever"` with the correct attribute name for parameter annotations it would return a non-`null` value only if another parameter annotation exists. That’s far away from a safe bet. – Holger Nov 21 '13 at 10:44
  • @Holger, totally right. Found code (random project) that is doing something similar. Creating a ParameterAnnotationsAttribute, adding the annotation then adding that attribute to the method. Will report back if that works. – Matthew Campbell Nov 21 '13 at 11:09
  • @Holger, thanks again. Hadn't gotten my head around the way Javassist works. Creating a ParameterAnnotationsAttribute (PAA) then an Annotation and adding it to the PAA works great. – Matthew Campbell Nov 25 '13 at 11:47

0 Answers0