使用javassist进行magic-api增强改造

什么是magic-api,推荐查看官网https://www.ssssssss.org/magic-api/
目前遇到需要扩展magic-api的properties,而源码并不支持在左侧列表树接口中返回properties属性

一. 改造思路

实际上,magic-api通过org.ssssssss.magicapi.core.model.MagicEntity来表示返回对象。
通过观察,可发现构造器中未将properties进行set。

protected void simple(MagicEntity entity) {
	entity.setId(this.id);
	entity.setName(this.name);
	entity.setGroupId(this.groupId);
	entity.setCreateBy(this.createBy);
	entity.setCreateTime(this.createTime);
	entity.setUpdateBy(this.updateBy);
	entity.setUpdateTime(this.updateTime);
	entity.setLock(this.lock);
}

所以需要通过一些手段将properties注入,期望结果:

protected void simple(MagicEntity entity) {
	entity.setProperties(this.properties);
	entity.setId(this.id);
	entity.setName(this.name);
	entity.setGroupId(this.groupId);
	entity.setCreateBy(this.createBy);
	entity.setCreateTime(this.createTime);
	entity.setUpdateBy(this.updateBy);
	entity.setUpdateTime(this.updateTime);
	entity.setLock(this.lock);
}

二.源码增强

新建一个MagicEnhance增强类。

@Slf4j
public class MagicEnhance {

    public MagicEnhance(){

    }
}

实现enhance增强方法,完整代码如下:

@Slf4j
public class MagicEnhance {

    public MagicEnhance(){

    }

    public static void enhance() {
        //初始化
        ClassPool pool = ClassPool.getDefault();
        //找到对应的类
        CtClass ctClass;
        try {
            pool.importPackage("org.ssssssss.magicapi.core.model");
            ctClass = pool.get("org.ssssssss.magicapi.core.model.MagicEntity");
            if (ctClass.isFrozen()) {
                ctClass.defrost();
            }
            // 找到对应的方法 通过new CtClass[]{ctClass}进行入参注入
            CtMethod ctMethod = ctClass.getDeclaredMethod("simple", new CtClass[]{ctClass});
            // 此处需注意,setBoday中的入参需通过$符号来指定,规则:$0表示this,$1, $2以此类推表示第1个参数,第二个参数
            ctMethod.setBody("{\n" +
                    "        $1.setProperties(this.properties);\n" +
                    "        $1.setId(this.id);\n" +
                    "        $1.setName(this.name);\n" +
                    "        $1.setGroupId(this.groupId);\n" +
                    "        $1.setCreateBy(this.createBy);\n" +
                    "        $1.setCreateTime(this.createTime);\n" +
                    "        $1.setUpdateBy(this.updateBy);\n" +
                    "        $1.setUpdateTime(this.updateTime);\n" +
                    "        $1.setLock(this.lock);\n" +
                    "    }");
            ctClass.toClass();
            log.info("magic增强成功");
        } catch (NotFoundException | CannotCompileException e) {
            log.error("magic增强失败", e);
            throw new ServiceException("magic增强失败");
        }
    }
}

三. 备注

使用增强之前需检查项目中有没有使用spring-boot-devtools依赖:

<!-- spring-boot-devtools -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<optional>true</optional>
</dependency>

若使用了则会在增强后导致项目启动失败,报错信息如下

LinkageError loader (instance of xxx) previously initiated loading for a different type with name...

需要将该依赖进行去除。

参考资料:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值