使用javassist辅助开发JMX

实际这个功能Spring本身就有且做的更好,这里就又造了一下轮子

使用Java自带的Compiler+字符串拼接/模板引擎,也可做,不过这里为了方便直接使用的javassist

使用效果

在这里插入图片描述
待代理类
在这里插入图片描述
使用
在这里插入图片描述
效果

主要部分

在这里插入图片描述
注解,非常简单

@Data
public class MBeanInfo {

    private CtClass targetClass;
    private String group;
    private String name;
    private String packageName;
    private String interfaceName;
    private CtClass targetInterface;

    @SneakyThrows
    public void addMethod(CtMethod method){
        targetInterface.addMethod(method);
    }

    @SneakyThrows
    public void addSetterGetter(CtMethod method){
        targetClass.addMethod(method);
    }

    public void init(ClassPool pool) {
        targetInterface = pool.makeInterface(packageName + ".$" + interfaceName);
        if(group.isEmpty())group=targetClass.getPackageName();//默认使用包名与类名
        if(name.isEmpty())name= lowerFirst(targetClass.getSimpleName());
        targetClass.setName(targetClass.getPackageName()+".$"+targetClass.getSimpleName());
    }

    private String lowerFirst(String simpleName) {
        char[] array = simpleName.toCharArray();
        array[0]|=' ';//类型转小写的小技巧
        return new String(array);
    }

}

信息包装类型

public class MBeanCreator {

    private ClassPool pool;
    private MBeanServer mBeanServer;

    public MBeanCreator() {
        pool = ClassPool.getDefault();
        mBeanServer = ManagementFactory.getPlatformMBeanServer();
    }

    @SneakyThrows
    public void create(Class<?> clazz) {
        if(!clazz.isAnnotationPresent(MBean.class))return;
        MBeanInfo mBeanInfo= getMBeanInfo(clazz);
        createAndBind(mBeanInfo);
    }

    @SneakyThrows
    private void createAndBind(MBeanInfo mBeanInfo) {
        CtClass targetClass = mBeanInfo.getTargetClass();
        CtClass targetInterface = mBeanInfo.getTargetInterface();
        targetClass.addInterface(targetInterface);
        targetClass.writeFile("C:\\Users\\邵康\\Desktop\\MyTest3\\src\\main\\resources");
        targetInterface.toClass();// 防止  下面 加载时 找不到上面的类  必须先加载依赖类
        Object instance = targetClass.toClass().newInstance();
        ObjectName name = new ObjectName(mBeanInfo.getGroup() + ":name=" + mBeanInfo.getName());
        mBeanServer.registerMBean(instance,name);
    }

    @SneakyThrows
    private MBeanInfo getMBeanInfo(Class<?> clazz) {
        MBeanInfo beanInfo=new MBeanInfo();
        CtClass target = pool.get(clazz.getName());
        beanInfo.setTargetClass(target);
        MBean mBean = (MBean) target.getAnnotation(MBean.class);
        beanInfo.setGroup(mBean.group());
        beanInfo.setName(mBean.name());
        beanInfo.setPackageName(target.getPackageName());
        beanInfo.setInterfaceName(target.getSimpleName()+"MBean");
        beanInfo.init(pool);
        CtMethod[] methods = target.getDeclaredMethods();
        for (CtMethod method : methods) {
            handleMethod(beanInfo,method);
        }
        CtField[] fields = target.getDeclaredFields();
        for (CtField field : fields) {
            handleField(beanInfo,field);
        }
        return beanInfo;
    }

    @SneakyThrows
    private void handleField(MBeanInfo beanInfo, CtField field) {
        String upperFirstName = upperFirst(field.getName());
        if(field.hasAnnotation(Getter.class)){
            CtMethod getter = CtNewMethod.getter("get" + upperFirstName, field);
            doHandleField(beanInfo, getter);
        }
        if(field.hasAnnotation(Setter.class)){
            CtMethod setter = CtNewMethod.setter("set" + upperFirstName, field);
            doHandleField(beanInfo,setter);
        }
    }

    private void doHandleField(MBeanInfo beanInfo, CtMethod getter) throws NotFoundException {
        beanInfo.addSetterGetter(getter);
        doHandleMethod(beanInfo,getter);
    }

    private String upperFirst(String name) {
        char[] array = name.toCharArray();
        array[0]&='_';//首字母大写的小技巧
        return new String(array);
    }

    @SneakyThrows
    private void handleMethod(MBeanInfo beanInfo, CtMethod method) {
        if(!method.hasAnnotation(Operation.class))return;
        doHandleMethod(beanInfo, method);
    }

    private void doHandleMethod(MBeanInfo beanInfo, CtMethod method) throws NotFoundException {
        CtMethod ctMethod = new CtMethod(method.getReturnType(), method.getName(), method.getParameterTypes()
                , beanInfo.getTargetInterface());
        beanInfo.addMethod(ctMethod);
    }

}

主要核心类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值