[jboss-user] [Javassist] - Adding a Method Annotation not working as expected...

Amit P do-not-reply at jboss.com
Mon Oct 3 19:14:26 EDT 2011


Amit P [http://community.jboss.org/people/apinke] created the discussion

"Adding a Method Annotation not working as expected..."

To view the discussion, visit: http://community.jboss.org/message/629947#629947

--------------------------------------------------------------
Hi All, 

I have a few classes ( in a supplied jar) in which I am trying to mark certain specifc methods as "Deprecated" .
I am able to use javassist and add the annotation to the method (code attached to the end of this mail) but Eclipse refuses to identify that as a "Deprecated" method at compile time.
I decompiled and verified that there is a "@Deprecated" annotation on the method. I also used ASM compare between a class in which I added a @Deprecated and this class in which javassist  added the annotation and notice one strange thing : the access flags are different

"normal" method :
// DEPRECATED
  // access flags 0x20001
  public getName()Ljava/lang/String;
  @Ljava/lang/Deprecated;()
   L0
    LINENUMBER 7 L0
  

javassist annotated method :
   // access flags 0x1
  public getName()Ljava/lang/String;
  @Ljava/lang/Deprecated;()
   L0
    LINENUMBER 7 L0
  

Questions :
1) is there a problem in the way the Annotation is being added ?
2) Cant I add a java.lang.Deprecated annotation to a method ? is there some special modifier or instruction to be used to add the inbuilt java Annotations ?

Any help or pointer is appreciated !! 
I tried to make it a "invisibleTag" ,  specify it as "Deprecated" , "@Deprecated" but the IDE refuses to treat that as a Deprecated API.

thanks
Pat
----
code snippet :


 CtMethod[] methods = cc.getMethods();
  for (int k = 0; k < methods.length; k++) {
   CtMethod met = methods[k];
    System.out.println("Found Method --> " + met.getName());
    try{
     ClassFile ccFile = cc.getClassFile(); 
     ConstPool constpool = ccFile.getConstPool(); 
     
     AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); 
     Annotation annot = new Annotation("java.lang.Deprecated", constpool);
     attr.addAnnotation(annot); 
     met.getMethodInfo().addAttribute(attr);  
     
     
    }catch(Exception e){
     System.out.println("Skipping method:-->" + met.getName() +"Reason:" + e.getMessage() );
    }
  }
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/629947#629947]

Start a new discussion in Javassist at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20111003/1092fc51/attachment-0001.html 


More information about the jboss-user mailing list