vandench

SelfDeprecation

Apr 21st, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. =import java.lang.reflect.Method;
  2.  
  3. import javassist.ClassPool;
  4. import javassist.CtClass;
  5. import javassist.CtMethod;
  6. import javassist.bytecode.AnnotationsAttribute;
  7. import javassist.bytecode.ClassFile;
  8. import javassist.bytecode.ConstPool;
  9. import javassist.bytecode.annotation.Annotation;
  10. import javassist.bytecode.annotation.StringMemberValue;
  11.  
  12. public class SelfDeprecation
  13. {
  14.     static boolean dep = false;
  15.  
  16.     public static void deprecateMethod(String className, String methodName) throws Exception
  17.     {
  18.         ClassPool pool = ClassPool.getDefault();
  19.         CtClass cc = pool.getCtClass(className);
  20.         CtMethod descriptor = cc.getDeclaredMethod(methodName);
  21.         ClassFile ccFile = cc.getClassFile();
  22.         ConstPool constpool = ccFile.getConstPool();
  23.         AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
  24.         Annotation annot = new Annotation("java.lang.Deprecated", constpool);
  25.         attr.addAnnotation(annot);
  26.         descriptor.getMethodInfo().addAttribute(attr);
  27.         if(!dep)
  28.         {
  29.             deprecateMethod("SelfDeperecation", "deprecateMethod");
  30.             dep = true;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment