JavassistUtils { public static void copyClassAnnotations(final CtClass oldClass, final CtClass newClass) { final AnnotationsAttribute classAnnotations = (AnnotationsAttribute) oldClass.getClassFile().getAttribute( AnnotationsAttribute.visibleTag); final AnnotationsAttribute copyClassAttribute = JavassistUtils.cloneAnnotationsAttribute( newClass.getClassFile2().getConstPool(), classAnnotations, ElementType.TYPE); newClass.getClassFile().addAttribute(copyClassAttribute); } 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 testCopyClassAnnontations() { final CtClass fromClass = ClassPool.getDefault().makeClass("fromClass"); final CtClass toClass = ClassPool.getDefault().makeClass("toClass"); final ConstPool fromPool = fromClass.getClassFile().getConstPool(); final AnnotationsAttribute attr = new AnnotationsAttribute(fromPool, AnnotationsAttribute.visibleTag); final Annotation anno = new Annotation("java.lang.Integer", fromPool); anno.addMemberValue("copyClassName", new IntegerMemberValue(fromPool, 246)); attr.addAnnotation(anno); fromClass.getClassFile().addAttribute(attr); JavassistUtils.copyClassAnnotations(fromClass, toClass); final Annotation toAnno = ((AnnotationsAttribute) toClass.getClassFile().getAttribute( AnnotationsAttribute.visibleTag)).getAnnotation("java.lang.Integer"); Assert.assertEquals( 246, ((IntegerMemberValue) toAnno.getMemberValue("copyClassName")).getValue()); }