@Test public void testCloneAnnotationsAttribute() { final CtClass clz = ClassPool.getDefault().makeClass("testCloneAnnotationsAttribute"); final CtMethod ctmethod = addNewMethod(clz, "origMethod"); final AnnotationsAttribute attr = annotateMethod(ctmethod, "origAnno", 135); final AnnotationsAttribute clonedAttr = JavassistUtils.cloneAnnotationsAttribute( ctmethod.getMethodInfo().getConstPool(), attr, java.lang.annotation.ElementType.METHOD); Assert.assertEquals( 135, ((IntegerMemberValue) clonedAttr.getAnnotation("java.lang.Integer").getMemberValue( "origAnno")).getValue()); }
public static AnnotationsAttribute cloneAnnotationsAttribute( final ConstPool constPool, final AnnotationsAttribute attr, final ElementType validElementType) { final ClassLoader cl = ClassLoader.getSystemClassLoader(); final AnnotationsAttribute attrNew = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); if (attr != null) { for (final Annotation annotation : attr.getAnnotations()) { final Annotation newAnnotation = new Annotation(annotation.getTypeName(), constPool); Class<?> annoClass; try { annoClass = cl.loadClass(annotation.getTypeName()); final Target target = annoClass.getAnnotation(Target.class); if ((target != null) && !Arrays.asList(target.value()).contains(validElementType)) { continue; } } catch (final ClassNotFoundException e) { LOGGER.error("Cannot apply this annotation because it's type cannot be found", e); continue; } if (annotation.getMemberNames() != null) { for (final Object memberName : annotation.getMemberNames()) { final MemberValue memberValue = annotation.getMemberValue((String) memberName); if (memberValue != null) { newAnnotation.addMemberValue((String) memberName, memberValue); } } } attrNew.addAnnotation(newAnnotation); } } return attrNew; }
JavassistUtils { public static AnnotationsAttribute cloneAnnotationsAttribute( final ConstPool constPool, final AnnotationsAttribute attr, final ElementType validElementType) { final ClassLoader cl = ClassLoader.getSystemClassLoader(); final AnnotationsAttribute attrNew = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); if (attr != null) { for (final Annotation annotation : attr.getAnnotations()) { final Annotation newAnnotation = new Annotation(annotation.getTypeName(), constPool); Class<?> annoClass; try { annoClass = cl.loadClass(annotation.getTypeName()); final Target target = annoClass.getAnnotation(Target.class); if ((target != null) && !Arrays.asList(target.value()).contains(validElementType)) { continue; } } catch (final ClassNotFoundException e) { LOGGER.error("Cannot apply this annotation because it's type cannot be found", e); continue; } if (annotation.getMemberNames() != null) { for (final Object memberName : annotation.getMemberNames()) { final MemberValue memberValue = annotation.getMemberValue((String) memberName); if (memberValue != null) { newAnnotation.addMemberValue((String) memberName, memberValue); } } } attrNew.addAnnotation(newAnnotation); } } return attrNew; } }
JavassistUtils { public static AnnotationsAttribute cloneAnnotationsAttribute( final ConstPool constPool, final AnnotationsAttribute attr, final ElementType validElementType) { final ClassLoader cl = ClassLoader.getSystemClassLoader(); final AnnotationsAttribute attrNew = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); if (attr != null) { for (final Annotation annotation : attr.getAnnotations()) { final Annotation newAnnotation = new Annotation(annotation.getTypeName(), constPool); Class<?> annoClass; try { annoClass = cl.loadClass(annotation.getTypeName()); final Target target = annoClass.getAnnotation(Target.class); if ((target != null) && !Arrays.asList(target.value()).contains(validElementType)) { continue; } } catch (final ClassNotFoundException e) { LOGGER.error("Cannot apply this annotation because it's type cannot be found", e); continue; } if (annotation.getMemberNames() != null) { for (final Object memberName : annotation.getMemberNames()) { final MemberValue memberValue = annotation.getMemberValue((String) memberName); if (memberValue != null) { newAnnotation.addMemberValue((String) memberName, memberValue); } } } attrNew.addAnnotation(newAnnotation); } } return attrNew; } private  JavassistUtils(); }
JavassistUtils { public static AnnotationsAttribute cloneAnnotationsAttribute( final ConstPool constPool, final AnnotationsAttribute attr, final ElementType validElementType) { final ClassLoader cl = ClassLoader.getSystemClassLoader(); final AnnotationsAttribute attrNew = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); if (attr != null) { for (final Annotation annotation : attr.getAnnotations()) { final Annotation newAnnotation = new Annotation(annotation.getTypeName(), constPool); Class<?> annoClass; try { annoClass = cl.loadClass(annotation.getTypeName()); final Target target = annoClass.getAnnotation(Target.class); if ((target != null) && !Arrays.asList(target.value()).contains(validElementType)) { continue; } } catch (final ClassNotFoundException e) { LOGGER.error("Cannot apply this annotation because it's type cannot be found", e); continue; } if (annotation.getMemberNames() != null) { for (final Object memberName : annotation.getMemberNames()) { final MemberValue memberValue = annotation.getMemberValue((String) memberName); if (memberValue != null) { newAnnotation.addMemberValue((String) memberName, memberValue); } } } attrNew.addAnnotation(newAnnotation); } } return attrNew; } 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(); }
JavassistUtils { public static AnnotationsAttribute cloneAnnotationsAttribute( final ConstPool constPool, final AnnotationsAttribute attr, final ElementType validElementType) { final ClassLoader cl = ClassLoader.getSystemClassLoader(); final AnnotationsAttribute attrNew = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); if (attr != null) { for (final Annotation annotation : attr.getAnnotations()) { final Annotation newAnnotation = new Annotation(annotation.getTypeName(), constPool); Class<?> annoClass; try { annoClass = cl.loadClass(annotation.getTypeName()); final Target target = annoClass.getAnnotation(Target.class); if ((target != null) && !Arrays.asList(target.value()).contains(validElementType)) { continue; } } catch (final ClassNotFoundException e) { LOGGER.error("Cannot apply this annotation because it's type cannot be found", e); continue; } if (annotation.getMemberNames() != null) { for (final Object memberName : annotation.getMemberNames()) { final MemberValue memberValue = annotation.getMemberValue((String) memberName); if (memberValue != null) { newAnnotation.addMemberValue((String) memberName, memberValue); } } } attrNew.addAnnotation(newAnnotation); } } return attrNew; } 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; }