flink涉及的基础知识 - 动态代理

用来对原方法进行增强,类结构如下:


java_动态代理

例子

InvocationHandler 实现类

public class MathHandler implements InvocationHandler {
    private MathService service;

    public MathHandler(MathService service) {
        this.service = service;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(service, args);
    }
}

target接口

public interface MathService {
    int add(int num1, int num2);
    int multiply(int num1, int num2);
}

target实现

public class MathServiceImpl implements MathService {
    @Override
    public int add(int num1, int num2) {
        return num1 + num2;
    }

    @Override
    public int multiply(int num1, int num2) {
        return num1 * num2;
    }
}

生成代理

// 生成代理类的class文件
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
final MathService service = (MathService)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class[]{MathService.class},
                new MathHandler(new MathServiceImpl()));

读者可以根据堆栈图自己debug下生成的逻辑


堆栈

代码执行完后,则会在classpath的com/sun/proxy下看到代理文件了,如果在ideal中,刷新下就可以看到


文件所在位置

生成的代理类如下:

package com.sun.proxy;

import hadoop.proxy.MathService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

/* renamed from: com.sun.proxy.$Proxy0  reason: invalid class name */
public final class C$Proxy0 extends Proxy implements MathService {
    private static Method m0;
    private static Method m1;
    private static Method m2;
    private static Method m3;
    private static Method m4;

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m3 = Class.forName("hadoop.proxy.MathService").getMethod("multiply", Integer.TYPE, Integer.TYPE);
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
            m4 = Class.forName("hadoop.proxy.MathService").getMethod("add", Integer.TYPE, Integer.TYPE);
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
        } catch (NoSuchMethodException e) {
            throw new NoSuchMethodError(e.getMessage());
        } catch (ClassNotFoundException e2) {
            throw new NoClassDefFoundError(e2.getMessage());
        }
    }

    public C$Proxy0(InvocationHandler invocationHandler) {
        super(invocationHandler);
    }

    public final int add(int i, int i2) {
        try {
            return ((Integer) ((Proxy) this).h.invoke(this, m4, new Object[]{Integer.valueOf(i), Integer.valueOf(i2)})).intValue();
        } catch (Error | RuntimeException e) {
            throw e;
        } catch (Throwable th) {
            throw new UndeclaredThrowableException(th);
        }
    }

    public final boolean equals(Object obj) {
        try {
            return ((Boolean) ((Proxy) this).h.invoke(this, m1, new Object[]{obj})).booleanValue();
        } catch (Error | RuntimeException e) {
            throw e;
        } catch (Throwable th) {
            throw new UndeclaredThrowableException(th);
        }
    }

    public final int hashCode() {
        try {
            return ((Integer) ((Proxy) this).h.invoke(this, m0, null)).intValue();
        } catch (Error | RuntimeException e) {
            throw e;
        } catch (Throwable th) {
            throw new UndeclaredThrowableException(th);
        }
    }

    public final int multiply(int i, int i2) {
        try {
            return ((Integer) ((Proxy) this).h.invoke(this, m3, new Object[]{Integer.valueOf(i), Integer.valueOf(i2)})).intValue();
        } catch (Error | RuntimeException e) {
            throw e;
        } catch (Throwable th) {
            throw new UndeclaredThrowableException(th);
        }
    }

    public final String toString() {
        try {
            return (String) ((Proxy) this).h.invoke(this, m2, null);
        } catch (Error | RuntimeException e) {
            throw e;
        } catch (Throwable th) {
            throw new UndeclaredThrowableException(th);
        }
    }
}

特点:

  1. 获取所有超类的方法对象
  2. 在实现接口的方法时,会调用InvocationHandler的invoke方法,proxy就是this,method就是反射构造的同方法签名的Method对象,args就是用户调用传入的参数
  3. 通常来说,用户只需要在实现的InvocationHandler的invoke方法中,加一句代码method.invoke(具体目标类,入参)即可完成整个代理
  4. flink的做法是将Method对象及入参打包成一个消息,进行发送,然后调用具体的干活组件去执行

你可能感兴趣的:(flink涉及的基础知识 - 动态代理)