DEMO
写个简单的JDK动态代理Demo,来跟踪分析源码,Demo代码如下
public interface MyInterface {
void print();
}
public class MyClass implements MyInterface {
@Override
public void print() {
System.out.println("主方法啦啦啦啦啦");
}
}
public class MyInvocationHandler implements InvocationHandler {
private Object target ;
public MyInvocationHandler(Object obj) {
this.target = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
startLog();
Object returnObj = method.invoke(target, args);
endLog();
return null;
}
private void endLog() {
System.out.println("后置操作");
}
private void startLog() {
System.out.println("前置操作");
}
}
public class Test {
public static void main(String[] args) {
MyClass myclass = new MyClass();
// 创建代理对象
MyInterface o = (MyInterface)Proxy.newProxyInstance(myclass.getClass().getClassLoader(),
myclass.getClass().getInterfaces(),
new MyInvocationHandler(myclass));
// 调用代理方法
o.print();
}
}
IDEA中run运行Test类,执行正常
Debug重复打印问题
打断点,并DEBUG一行一行走,发现多打印的好多次“前置后置操作”
百思不得姐,打断点到前置后置操作方法,打印的时候并不停。。。
加了一行打印方法名,终于有眉目了
百度说是idea在debug的时候会调用对象的toString方法,而代理对象除了会代理自定义接口的方法print(),还会代理Object类的toString(),equals(),hashCode()方法,所以会重复调用
Proxy源码
public class Proxy implements java.io.Serializable {
private static final long serialVersionUID = -2222568056686623797L;
private static final Class<?>[] constructorParams =
{ InvocationHandler.class };
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
Objects.requireNonNull(h);
final Class<?>[] intfs = interfaces.clone();
// 根据传入的classLoader和接口 动态构造代理类的class
Class<?> cl = getProxyClass0(loader, intfs);
try {
// 获取代理类的那个以InvocationHandler为入参的构造函数
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
// 修饰符,构造函数是否是public的,如果不是 得设置可访问
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
// 实例化代理对象
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
}
}
}
关键在于这个产生的class类了,如何产生的?产生的是什么样的代理类?
private static final WeakCache<ClassLoader, Class<?>[], Class<?>>
proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());
private static Class<?> getProxyClass0(ClassLoader loader,
Class<?>... interfaces) {
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
// 如果给定的loader和接口生成的代理类存在,则从缓存中获取,否则使用ProxyClassFactory创建个新的
return proxyClassCache.get(loader, interfaces);
}
来看这个缓存类WeakCache.get(可以不关注此缓存类,直接看ProxyClassFactory.get方法生成class这块)
final class WeakCache<K, P, V> {
private final ReferenceQueue<K> refQueue = new ReferenceQueue<>();
// 两层map,对于本文来说 classLoader为key,value是一个以接口数组为key、对应的代理类引用或工厂类为value的子map
private final ConcurrentMap<Object, ConcurrentMap<Object, Supplier<V>>> map = new ConcurrentHashMap<>();
//
private final ConcurrentMap<Supplier<V>, Boolean> reverseMap = new ConcurrentHashMap<>();
// 缓存类key的构造工厂
private final BiFunction<K, P, ?> subKeyFactory;
// 缓存类value的构造工厂
private final BiFunction<K, P, V> valueFactory;
public V get(K key, P parameter) {
Objects.requireNonNull(parameter);
// classLoader的key对象是弱引用,关联的refQueue队列, 在被gc回收时会把此key放入队列中
// 此方法循环队列,如果key对象被回收,删除缓存类中对应的value
expungeStaleEntries();
// 封装classLoader 构造一个弱引用key对象
Object cacheKey = CacheKey.valueOf(key, refQueue);
// 先获取,看缓存中是否存在此classLoader对应的代理缓存map
ConcurrentMap<Object, Supplier<V>> valuesMap = map.get(cacheKey);
if (valuesMap == null) {
// 如果没有,设置个空
ConcurrentMap<Object, Supplier<V>> oldValuesMap
= map.putIfAbsent(cacheKey,
valuesMap = new ConcurrentHashMap<>());
if (oldValuesMap != null) {
// 类似单例的DCL
valuesMap = oldValuesMap;
}
}
// 根据接口数组 构造一个子key
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
// 从代理缓存map中查找是否存在此接口数组对应的代理类构造工厂
Supplier<V> supplier = valuesMap.get(subKey);
Factory factory = null;
while (true) {
// 不为空,get获取代理类class
if (supplier != null) {
// supplier might be a Factory or a CacheValue<V> instance
V value = supplier.get();
if (value != null) {
return value;
}
}
// 没有缓存或者 缓存的代理class已经被卸载了,重新new一个工厂类来创建代理class
if (factory == null) {
factory = new Factory(key, parameter, subKey, valuesMap);
}
// 更新缓存
if (supplier == null) {
// 同样DCL设置新的
supplier = valuesMap.putIfAbsent(subKey, factory);
if (supplier == null) {
// successfully installed Factory
supplier = factory;
}
// else retry with winning supplier
} else {
// 替换
if (valuesMap.replace(subKey, supplier, factory)) {
// successfully replaced
// cleared CacheEntry / unsuccessful Factory
// with our Factory
supplier = factory;
} else {
// retry with current supplier
supplier = valuesMap.get(subKey);
}
}
}
}
}
java.lang.reflect.WeakCache.Factory
private final class Factory implements Supplier<V> {
private final K key;
private final P parameter;
private final Object subKey;
private final ConcurrentMap<Object, Supplier<V>> valuesMap;
Factory(K key, P parameter, Object subKey,
ConcurrentMap<Object, Supplier<V>> valuesMap) {
this.key = key;
this.parameter = parameter;
this.subKey = subKey;
this.valuesMap = valuesMap;
}
@Override
public synchronized V get() { // serialize access
// re-check
Supplier<V> supplier = valuesMap.get(subKey);
if (supplier != this) {
// 第一次放入value的是Factory工厂,工厂创建出代理class后,valuesMap中放的就是CacheValue,即代理class
// 这里如果别的线程已经创建出来了,返回null,上层重新循环获取
return null;
}
// 这里就是proxyClassFactory创建class的地方
V value = null;
try {
value = Objects.requireNonNull(valueFactory.apply(key, parameter));
} finally {
if (value == null) { // remove us on failure
valuesMap.remove(subKey, this);
}
}
// the only path to reach here is with non-null value
assert value != null;
// 弱引用 代理class
CacheValue<V> cacheValue = new CacheValue<>(value);
// put into reverseMap
reverseMap.put(cacheValue, Boolean.TRUE);
// 把代理class的弱引用替换到valuesMap, Factory --> CacheValue
if (!valuesMap.replace(subKey, this, cacheValue)) {
throw new AssertionError("Should not reach here");
}
// successfully replaced us with new CacheValue -> return the value
// wrapped by it
return value;
}
}
java.lang.reflect.Proxy.ProxyClassFactory#apply
private static final class ProxyClassFactory
implements BiFunction<ClassLoader, Class<?>[], Class<?>>
{
// JDK动态代理类的统一命名就是 $Proxy1\ $Proxy2\ $Proxy4
private static final String proxyClassNamePrefix = "$Proxy";
private static final AtomicLong nextUniqueNumber = new AtomicLong();
@Override
public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
for (Class<?> intf : interfaces) {
// 用传入的classLoader加载各接口
Class<?> interfaceClass = null;
try {
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
// 确定一下,确实是接口
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
// 接口数组传重复了
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
String proxyPkg = null; // package to define proxy class in
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
// 非公共的接口方法必须在同一包下
for (Class<?> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
// 默认的代理class包路径
if (proxyPkg == null) {
// if no non-public proxy interfaces, use com.sun.proxy package
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
// 代理类名 xxx.xxx.$Proxy1\ $Proxy2
long num = nextUniqueNumber.getAndIncrement();
String proxyName = proxyPkg + proxyClassNamePrefix + num;
// 操作字节码开始搞了
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
// classLoader加载字节码转为class对象返回
return defineClass0(loader, proxyName,
proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e) {
throw new IllegalArgumentException(e.toString());
}
}
}
这一句最终根据类名和接口类来产生对应的代理类
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
那么我们可以先把这行来提出来搞到main方法中,自己生成一个文件先看看是个什么东西
public static void main(String[] args) {
MyClass myclass = new MyClass();
// 创建代理对象
// MyInterface o = (MyInterface) //Proxy.newProxyInstance(myclass.getClass().getClassLoader(),
// myclass.getClass().getInterfaces(),
// new MyInvocationHandler(myclass));
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy111", myclass.getClass().getInterfaces());
FileOutputStream out = null;
try {
out = new FileOutputStream("D:\\proxy.class");
out.write(classFile);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// o.print();
}
反编译之后
public final class $Proxy111 extends Proxy implements MyInterface {
private static Method m1;
private static Method m3;
private static Method m2;
private static Method m0;
public $Proxy111(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final void print() throws {
try {
super.h.invoke(this, m3, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final int hashCode() throws {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m3 = Class.forName("com.example.demo.proxy.MyInterface").getMethod("print");
m2 = Class.forName("java.lang.Object").getMethod("toString");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
可以看到代码方法几乎相同,继承Proxy类,实现自定义接口,Proxy类成员变量有一个InvocationHandler h,调用h.invoke来实现代码。
h哪来的?开头Proxy代码中最后有一段 获取代理类的那个以InvocationHandler为入参的构造函数,创建代理实例对象。也就是我们传入的MyInvocationHandler来进行真正的操作。
接着跟代码,看内部如何构造代理类字节码
sun.misc.ProxyGenerator#generateClassFile
// 任何代理类都会实现这三个Object的方法
private static Method hashCodeMethod;
private static Method equalsMethod;
private static Method toStringMethod;
static {
try {
hashCodeMethod = Object.class.getMethod("hashCode");
equalsMethod = Object.class.getMethod("equals", new Class<?>[] { Object.class });
toStringMethod = Object.class.getMethod("toString");
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
private byte[] generateClassFile() {
// 列出要代理的所有方法,三个Object的和传入接口数组的所有方法
addProxyMethod(hashCodeMethod, Object.class);
addProxyMethod(equalsMethod, Object.class);
addProxyMethod(toStringMethod, Object.class);
for (Class<?> intf : interfaces) {
for (Method m : intf.getMethods()) {
addProxyMethod(m, intf);
}
}
/*
对于有相同方法签名的方法,校验返回类型是否兼容
*/
for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
checkReturnTypes(sigmethods);
}
// 产生各个方法的字节码
try {
// 产生构造方法的字节码
methods.add(generateConstructor());
for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
for (ProxyMethod pm : sigmethods) {
// 根据上边add的方法数组构造 private static Method m1; 等成员变量
fields.add(new FieldInfo(pm.methodFieldName,
"Ljava/lang/reflect/Method;",
ACC_PRIVATE | ACC_STATIC));
// 产生本方法开头add的那些方法的字节码
methods.add(pm.generateMethod());
}
}
// 静态块字节码
methods.add(generateStaticInitializer());
} catch (IOException e) {
throw new InternalError("unexpected I/O Exception", e);
}
if (methods.size() > 65535) {
throw new IllegalArgumentException("method limit exceeded");
}
if (fields.size() > 65535) {
throw new IllegalArgumentException("field limit exceeded");
}
/* ============================================================
* Step 3: Write the final class file.
*/
/*
* Make sure that constant pool indexes are reserved for the
* following items before starting to write the final class file.
*/
cp.getClass(dotToSlash(className));
cp.getClass(superclassName);
for (Class<?> intf: interfaces) {
cp.getClass(dotToSlash(intf.getName()));
}
/*
* Disallow new constant pool additions beyond this point, since
* we are about to write the final constant pool table.
*/
cp.setReadOnly();
// 输出字节码
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
try {
/*
* Write all the items of the "ClassFile" structure.
* See JVMS section 4.1.
*/
// u4 magic;
dout.writeInt(0xCAFEBABE);
// u2 minor_version;
dout.writeShort(CLASSFILE_MINOR_VERSION);
// u2 major_version;
dout.writeShort(CLASSFILE_MAJOR_VERSION);
cp.write(dout); // (write constant pool)
// public final class 类名 父类名 接口名 // u2 access_flags;
dout.writeShort(accessFlags);
// u2 this_class;
dout.writeShort(cp.getClass(dotToSlash(className)));
// u2 super_class;
dout.writeShort(cp.getClass(superclassName));
// u2 interfaces_count;
dout.writeShort(interfaces.length);
// u2 interfaces[interfaces_count];
for (Class<?> intf : interfaces) {
dout.writeShort(cp.getClass(
dotToSlash(intf.getName())));
}
// 成员变量 m1\m2\m3
dout.writeShort(fields.size());
// field_info fields[fields_count];
for (FieldInfo f : fields) {
f.write(dout);
}
// 各个代理方法
dout.writeShort(methods.size());
// method_info methods[methods_count];
for (MethodInfo m : methods) {
m.write(dout);
}
// u2 attributes_count;
dout.writeShort(0); // (no ClassFile attributes for proxy classes)
} catch (IOException e) {
throw new InternalError("unexpected I/O Exception", e);
}
return bout.toByteArray();
}
sun.misc.ProxyGenerator.ProxyMethod#generateConstructor
产生构造方法的字节码
/**
* Generate the constructor method for the proxy class.
*/
private MethodInfo generateConstructor() throws IOException {
MethodInfo minfo = new MethodInfo(
"<init>", "(Ljava/lang/reflect/InvocationHandler;)V",
ACC_PUBLIC);
DataOutputStream out = new DataOutputStream(minfo.code);
code_aload(0, out);
code_aload(1, out);
out.writeByte(opc_invokespecial);
out.writeShort(cp.getMethodRef(
superclassName,
"<init>", "(Ljava/lang/reflect/InvocationHandler;)V"));
out.writeByte(opc_return);
minfo.maxStack = 10;
minfo.maxLocals = 2;
minfo.declaredExceptions = new short[0];
return minfo;
}
代码中直接操作的字节,我们可以先javap -c 把class转成能看的字节码指令文件,截图了一小段,接着跟代码看的会容易些,可以看到是一一对应的
20210710补充
print()方法中调用另一个方法print2,print2前后会打印日志嘛?
在看了产生的代理类源码后,这个问题应该很容易想到,并不会。因为代理对象调用print()时,对象是代理对象,走的是h.invoke,打印前后日志,调用真正对象的print(),在真正对象中调用print2(),怎么会打印前后日志呢是吧,只有从代理对象.print2()调用,才会打印前后日志。
public class MyClass implements MyInterface {
@Override
public void print() {
System.out.println("主方法啦啦啦啦啦");
print2();
}
@Override
public void print2() {
System.out.println("222222222222");
}
}
public class MyInvocationHandler implements InvocationHandler {
private Object target ;
public MyInvocationHandler(Object obj) {
this.target = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("后置操作" + method.getName());
Object returnObj = method.invoke(target, args);
System.out.println("前置操作" + method.getName());
return null;
}
}