xdc1227 2016-12-08 05:46 采纳率: 0%
浏览 3803

使用javassist实现动态修改注解,在tomcat下无法获取class路径

public ExcelBean createExcelBean(String[] propertyArray, String[] typeArray, String[] columnNameArray) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException {

    ExcelBean excelBean = null;
    ClassPool pool = ClassPool.getDefault();
    //在tomcat环境下,向pool插入一个类对象的搜索路径
    pool.insertClassPath("com.camb.common.web.bean.ExcelBean");
    //获取要修改的类
    CtClass ctClass = pool.get("com.camb.common.web.bean.ExcelBean");
    for (int i = 0; i < propertyArray.length; i++) {
        //生成属性以及get和set方法
        String property = propertyArray[i];
        String method = property.substring(0, 1).toUpperCase() + property.substring(1, property.length());
        String type = typeArray[i];
        String column = columnNameArray[i];
        ctClass.addField(CtField.make("private " + type + " " + property + ";", ctClass));
        ctClass.addMethod(CtMethod.make("public void set" + method + "(" + type + " " + property + "){this." + property + " = " + property + ";}", ctClass));
        ctClass.addMethod(CtMethod.make("public " + type + " get" + method + "(){return this." + property + ";}", ctClass));

        //获取类里的属性
        CtField ctField = ctClass.getField(property);
        FieldInfo fieldInfo = ctField.getFieldInfo();
        System.out.println("属性名:" + ctField.getName());
        ConstPool cp = fieldInfo.getConstPool();

        //获取注解信息
        AnnotationsAttribute attribute = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
        Annotation annotation = new Annotation("com.camb.common.web.util.Excel", cp);
        //修改名称为name的注解
        annotation.addMemberValue("name", new StringMemberValue(column, cp));
        attribute.setAnnotation(annotation);
        fieldInfo.addAttribute(attribute);

        //打印修改后注解
        Annotation annotation2 = attribute.getAnnotation("com.camb.common.web.util.Excel");
        String value = ((StringMemberValue) annotation2.getMemberValue("name")).getValue();
        System.out.println("修改后的注解参数===" + value);
    }
    //修改后的class
    Class<?> c = ctClass.toClass();
    excelBean = (ExcelBean) c.newInstance();
    ctClass.detach();
    return excelBean;
}

    pool.insertClassPath("com.camb.common.web.bean.ExcelBean");这个设置并不能找到class的位置,如果使用pool.insertClassPath(new ClassClassPath(ExcelBean.class));虽然能找class,但是会产生异常
    com.camb.common.web.exception.ProcessException: javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of  org/apache/catalina/loader/WebappClassLoader): attempted  duplicate class definition for name: "com/camb/common/web/bean/ExcelBean"
  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2016-12-17 13:19
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算