Java的几种instrument方法

本文介绍了Java中实现instrument的两种方式:1. 通过Proxy创建装饰者模式,用于在Account类的操作中添加安全检查;2. 利用InvocationHandler,当调用Account接口方法时进行instrument操作,Spring框架广泛应用此机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java的两种instrument方法

1. 通过proxy

假设我们想在Account类中的operation中加入某个安全检查机制check。

public class Account { 
    public void operation() { 
        // todo
    } 
}
public class Check { 
    public static void check() { 
        // todo
    }  
}

这个时候我们需要先定义一个 Account类的接口:


public interface Account { 
    void operation(); 
}

然后把令Account类定义为一个实现类:

public class AccountImpl extends Account{ 
    public void operation() { 
        // todo
    } 
}

然后定义一个 Account类的 Decorator

public class AccountCheck implements Account {     
    private  Account account; 
    public AccountCheck (Account account) { 
        this.account = account; 
    } 
    public void operation() { 
        Checker.check(); 
        account.operation(); 
    } 
}

2. 通过InvocationHandler

在把Account定义为接口之后,在AccountImpl中实现接口,然后写一个InnovationHandler子类中实现invoke方法,在Account的方法调用时候对方法进行instrument

class CheckerInvocationHandler implements InvocationHandler { 
    private Object proxyedObject; 
    public CheckerInvocationHandler(Object o) { 
        proxyedObject = o; 
    } 
        
    public Object invoke(Object object, Method method, Object[] arguments) 
        throws Throwable {             
        if (object instanceof Account && method.getName().equals("opertaion")) { 
            Checker.check(); 
        } 
        return method.invoke(proxyedObject, arguments); 
    } 
}
public static void main(String[] args) { 
    Account account = (Account) Proxy.newProxyInstance( 
        Account.class.getClassLoader(), 
        new Class[] { Account.class }, 
        new CheckerInvocationHandler(new AccountImpl()) 
    ); 
    account.function(); 
}

Spring中用到了很多这个机制。

Java byte array to inputsteam

            Map<String, byte[]> classMap = ClazzReader.readClazz(classPath, resourceName);
            for (Map.Entry<String, byte[]> entry : classMap.entrySet()) {
                String className1 = entry.getKey();
                //System.out.println("className1: " + className1);

                InputStream is = ByteSource.wrap(entry.getValue()).openStream();
                System.out.println("|-- " + className1);

                ClassReader reader = new ClassReader(is);
                ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS);
                ClassVisitor pubMethAdapter = new PublicizeMethodAdapter(ASM4, writer);
                reader.accept(pubMethAdapter, ClassReader.SKIP_DEBUG);

            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值