From 6b171063a469bea6b21de68049419d20167fca8f Mon Sep 17 00:00:00 2001 From: bingor Date: Mon, 17 Oct 2022 14:05:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98=EF=BC=88?= =?UTF-8?q?=E4=B8=80=E6=9C=9F=EF=BC=89-=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-JDK=E5=8A=A8=E6=80=81=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/msb/proxy/v8/LogHandler.java | 33 ++++++++++++++++ src/main/java/com/msb/proxy/v8/Movable.java | 11 ++++++ src/main/java/com/msb/proxy/v8/Tank.java | 38 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/main/java/com/msb/proxy/v8/LogHandler.java create mode 100644 src/main/java/com/msb/proxy/v8/Movable.java create mode 100644 src/main/java/com/msb/proxy/v8/Tank.java diff --git a/src/main/java/com/msb/proxy/v8/LogHandler.java b/src/main/java/com/msb/proxy/v8/LogHandler.java new file mode 100644 index 0000000..c6adf9a --- /dev/null +++ b/src/main/java/com/msb/proxy/v8/LogHandler.java @@ -0,0 +1,33 @@ +package com.msb.proxy.v8;/** + * @Author bingor + * @Date 2022/10/17 10:30 + * @Description: com.msb.proxy.v8 + * @Version: 1.0 + */ + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + *@ClassName LogHandler + *@Description TODO + *@Author bingor + *@Date 2022/10/17 10:30 + *@Version 3.0 + */ +public class LogHandler implements InvocationHandler { + + private Tank tank; + + public LogHandler(Tank tank) { + this.tank = tank; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + System.out.println("method " + method.getName() + "start..."); + Object o = method.invoke(tank, args); + System.out.println("method " + method.getName() + " end..."); + return o; + } +} diff --git a/src/main/java/com/msb/proxy/v8/Movable.java b/src/main/java/com/msb/proxy/v8/Movable.java new file mode 100644 index 0000000..970f3ef --- /dev/null +++ b/src/main/java/com/msb/proxy/v8/Movable.java @@ -0,0 +1,11 @@ +package com.msb.proxy.v8; + +/** + * @Author bingor + * @Date 2022/10/17 9:40 + * @Description: com.msb.proxy.v5 + * @Version: 1.0 + */ +public interface Movable { + public void move(); +} diff --git a/src/main/java/com/msb/proxy/v8/Tank.java b/src/main/java/com/msb/proxy/v8/Tank.java new file mode 100644 index 0000000..0123e86 --- /dev/null +++ b/src/main/java/com/msb/proxy/v8/Tank.java @@ -0,0 +1,38 @@ +package com.msb.proxy.v8;/** + * @Author bingor + * @Date 2022/10/17 9:41 + * @Description: com.msb.proxy.v5 + * @Version: 1.0 + */ + +import java.lang.reflect.Proxy; +import java.util.Random; + +/** + *@ClassName Tank + *@Description TODO + *@Author bingor + *@Date 2022/10/17 9:41 + *@Version 3.0 + */ +public class Tank implements Movable { + @Override + public void move() { + System.out.println("tank move……"); + try { + Thread.sleep(new Random().nextInt(10000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + Tank tank = new Tank(); + +// System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true"); //输出生成的代理类 + System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true"); //输出生成的代理类 + + Movable movable = (Movable) Proxy.newProxyInstance(Tank.class.getClassLoader(), new Class[] {Movable.class}, new LogHandler(tank)); + movable.move();; + } +}