JavassistUtils { public static void copyMethodAnnotationsToField(final CtMethod method, final CtField field) { final AnnotationsAttribute methodAnnotations = (AnnotationsAttribute) method.getMethodInfo().getAttribute(AnnotationsAttribute.visibleTag); final AnnotationsAttribute copyMethodAttribute = JavassistUtils.cloneAnnotationsAttribute( field.getFieldInfo2().getConstPool(), methodAnnotations, ElementType.FIELD); field.getFieldInfo().addAttribute(copyMethodAttribute); } private  JavassistUtils(); static AnnotationsAttribute cloneAnnotationsAttribute(
      final ConstPool constPool,
      final AnnotationsAttribute attr,
      final ElementType validElementType); static CtMethod findMethod(final CtClass clz, final Method m); static void copyClassAnnotations(final CtClass oldClass, final CtClass newClass); static void copyMethodAnnotationsToField(final CtMethod method, final CtField field); static String getNextUniqueClassName(); static String getNextUniqueFieldName(); static CtClass generateEmptyClass(); static final String PREFIX_PACKAGE; }
@Test public void testCopyMethodAnnotationsToField() { final CtClass ctclass = ClassPool.getDefault().makeClass("test"); final CtMethod createdMethod = addNewMethod(ctclass, "doNothing"); annotateMethod(createdMethod, "value", 123); final CtField createdField = addNewField(ctclass, "toField"); JavassistUtils.copyMethodAnnotationsToField(createdMethod, createdField); IntegerMemberValue i = null; for (final Annotation annot : ((AnnotationsAttribute) createdField.getFieldInfo().getAttribute( AnnotationsAttribute.visibleTag)).getAnnotations()) { i = (IntegerMemberValue) annot.getMemberValue("value"); if (i != null) { break; } } if ((i == null) || (i.getValue() != 123)) { fail("Expected annotation value 123 but found " + i); } }