java 使用javassist 动态修改注解的值

来源:互联网 发布:js页面获取当前时间 编辑:程序博客网 时间:2024/05/17 09:03

如下实例:

@Testpublic void aetTestCaseAtrributes() throws NotFoundException {ClassPool pool = ClassPool.getDefault();// 获取需要修改的类CtClass ct = pool.get("com.XXX.XXX.XXX.XXXX");// 获取类里的所有方法CtMethod[] cms = ct.getDeclaredMethods();for (CtMethod cm : cms) {System.out.println("方法名称====" + cm.getName());MethodInfo methodInfo = cm.getMethodInfo();AnnotationsAttribute attribute = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag);System.out.println(attribute);ConstPool cPool = methodInfo.getConstPool();AnnotationsAttribute attribute2 = new AnnotationsAttribute(cPool, AnnotationsAttribute.visibleTag);Annotation[] anns= attribute2.getAnnotations();for(Annotation ann:anns){System.out.println(ann.getTypeName());}Annotation annotation = new Annotation("org.testng.annotations.Test", cPool);annotation.addMemberValue("invocationCount", new LongMemberValue(10L, cPool));attribute2.setAnnotation(annotation);methodInfo.addAttribute(attribute2);Annotation annotation2 = attribute2.getAnnotation("org.testng.annotations.Test");long text = ((LongMemberValue) annotation2.getMemberValue("invocationCount")).getValue();attribute = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag);System.out.println(attribute);System.out.println(text);}


0 0
原创粉丝点击