public DbSchemaChanger addAnnotation(String methodName,String annotationName,Map<String,Object> values) throws Exception {
  CtMethod methodDescriptor=cc.getDeclaredMethod(methodName);
  ClassFile ccFile=cc.getClassFile();
  ccFile.setVersionToJava5();
  ConstPool constpool=ccFile.getConstPool();
  MethodInfo minfo=methodDescriptor.getMethodInfo();
  AnnotationsAttribute attr=(AnnotationsAttribute)minfo.getAttribute(AnnotationsAttribute.visibleTag);
  if (attr == null) {
    attr=new AnnotationsAttribute(constpool,AnnotationsAttribute.visibleTag);
  }
  Annotation annot=new Annotation(annotationName,constpool);
  Set<Map.Entry<String,Object>> entries=values.entrySet();
  for (  Map.Entry<String,Object> entry : entries) {
    String attrName=entry.getKey();
    Object attrValue=entry.getValue();
    if (attrValue instanceof String) {
      annot.addMemberValue(attrName,new StringMemberValue((String)attrValue,ccFile.getConstPool()));
    }
 else {
      throw new RuntimeException(String.format("Unsupported attribute type %s of %s",attrName,attrValue));
    }
  }
  attr.addAnnotation(annot);
  minfo.addAttribute(attr);
  log.info("add {} to method {}",attr,methodDescriptor);
  return this;
}

public DbSchemaChanger addAnnotation ( String methodName , String annotationName , Map < String , Object > values ) throws Exception { CtMethod methodDescriptor = cc . getDeclaredMethod ( methodName ) ; ClassFile ccFile = cc . getClassFile ( ) ; ccFile . setVersionToJava5 ( ) ; ConstPool constpool = ccFile . getConstPool ( ) ; MethodInfo minfo = methodDescriptor . getMethodInfo ( ) ; AnnotationsAttribute attr = ( AnnotationsAttribute ) minfo . getAttribute ( AnnotationsAttribute . visibleTag ) ; if ( attr == null ) { attr = new AnnotationsAttribute ( constpool , AnnotationsAttribute . visibleTag ) ; } Annotation annot = new Annotation ( annotationName , constpool ) ; Set < Map . Entry < String , Object > > entries = values . entrySet ( ) ; for ( Map . Entry < String , Object > entry : entries ) { String attrName = entry . getKey ( ) ; Object attrValue = entry . getValue ( ) ; if ( attrValue instanceof String ) { annot . addMemberValue ( attrName , new StringMemberValue ( ( String ) attrValue , ccFile . getConstPool ( ) ) ) ; } else { throw new RuntimeException ( String . format ( "Unsupported attribute type %s of %s" , attrName , attrValue ) ) ; } } attr . addAnnotation ( annot ) ; minfo . addAttribute ( attr ) ; log . info ( "add {} to method {}" , attr , methodDescriptor ) ; return this ; }
add an annotation to a method
