gpt4 book ai didi

java - CGLib是否修改类的所有实例

转载 作者:行者123 更新时间:2023-11-29 08:57:25 25 4
gpt4 key购买 nike

如果我如下实现一些 MethodInterceptor:

public class HashCodeAlwaysZeroMethodInterceptor implements MethodInterceptor {

public Object intercept(Object object, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {

if ("hashCode".equals(method.getName())) {

return 0;
}

return methodProxy.invokeSuper(object, args);
}
}

Object proxy = Enhancer.create(
Object.class,
new HashCodeAlwaysZeroMethodInterceptor());

现在是否增强了 Object 的每个实例?即:如果我做类似的事情:

class foo { foo(){} }
foo myfoo = new foo();
int hash = myfoo.hashCode();
System.io.println(hash); //Prints 0

它真的会打印 0 吗?

最佳答案

来自 API 文档

创建

public static Factory create(java.lang.Class type,
Callback callback)

Helper method to create an intercepted object. For finer control over the generated instance, use a new instance of Enhancer instead of this static method.

Parameters:
type - class to extend or interface to implement
callback - the callback to use for all methods

从文档看来你的问题的答案是肯定的

关于java - CGLib是否修改类的所有实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19511621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com