Skip to content

Commit b85c4ea

Browse files
committed
java/se: refine proxy sample
1 parent d402018 commit b85c4ea

File tree

38 files changed

+430
-98
lines changed

38 files changed

+430
-98
lines changed

java/javaee/annotation/annotation-spring-aop/src/main/java/cn/edu/ntu/javaee/annotation/usage/before/proxy/ArithmeticCalculatorProxy.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,41 @@ public Object getProxy() {
4646
}
4747

4848
// /** mock up Proxy work */
49-
// class $Proxy0 extends Proxy implements ArithmeticCalculator {
50-
//
51-
// /**
52-
// * Constructs a new {@code Proxy} instance from a subclass (typically, a dynamic proxy
53-
// class)
54-
// * with the specified value for its invocation handler.
55-
// *
56-
// * @param h the invocation handler for this proxy instance
57-
// * @throws NullPointerException if the given invocation handler, {@code h}, is {@code
58-
// null}.
59-
// */
60-
// protected $Proxy0(InvocationHandler h) {
61-
// super(h);
62-
// }
63-
//
64-
// private Method METHOD_NAME;
65-
//
66-
// @Override
67-
// public int add(int a, int b) throws Throwable {
68-
// return (int) h.invoke(this, METHOD_NAME, new Object[] {a, b});
69-
// }
70-
//
71-
// @Override
72-
// public int sub(int a, int b) {
73-
// return 0;
74-
// }
75-
//
76-
// @Override
77-
// public int mul(int a, int b) {
78-
// return 0;
79-
// }
80-
//
81-
// @Override
82-
// public int div(int a, int b) {
83-
// return 0;
84-
// }
85-
// }
49+
class $Proxy0 extends Proxy implements ArithmeticCalculator {
50+
51+
/**
52+
* Constructs a new {@code Proxy} instance from a subclass (typically, a dynamic proxy
53+
class)
54+
* with the specified value for its invocation handler.
55+
*
56+
* @param h the invocation handler for this proxy instance
57+
* @throws NullPointerException if the given invocation handler, {@code h}, is {@code
58+
null}.
59+
*/
60+
protected $Proxy0(InvocationHandler h) {
61+
super(h);
62+
}
63+
64+
private Method METHOD_NAME;
65+
66+
@Override
67+
public int add(int a, int b) throws Throwable {
68+
return (int) h.invoke(this, METHOD_NAME, new Object[] {a, b});
69+
}
70+
71+
@Override
72+
public int sub(int a, int b) {
73+
return 0;
74+
}
75+
76+
@Override
77+
public int mul(int a, int b) {
78+
return 0;
79+
}
80+
81+
@Override
82+
public int div(int a, int b) {
83+
return 0;
84+
}
85+
}
8686
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.edu.ntu.java.javase.reflect.classloader;
1+
package cn.edu.ntu.java.javase.jvm.classloader;
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;

java/javase/javase-reflect/src/main/java/cn/edu/ntu/java/javase/reflect/model/Animal.java renamed to java/javase/javase-jvm/src/main/java/cn/edu/ntu/java/javase/jvm/spi/Animal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.edu.ntu.java.javase.reflect.model;
1+
package cn.edu.ntu.java.javase.jvm.spi;
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package cn.edu.ntu.java.javase.reflect.model.impl;
1+
package cn.edu.ntu.java.javase.jvm.spi.impl;
22

3-
import cn.edu.ntu.java.javase.reflect.model.Animal;
3+
import cn.edu.ntu.java.javase.jvm.spi.Animal;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package cn.edu.ntu.java.javase.reflect.model.impl;
1+
package cn.edu.ntu.java.javase.jvm.spi.impl;
22

3-
import cn.edu.ntu.java.javase.reflect.model.Animal;
3+
import cn.edu.ntu.java.javase.jvm.spi.Animal;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package cn.edu.ntu.java.javase.reflect.model.impl;
1+
package cn.edu.ntu.java.javase.jvm.spi.impl;
22

3-
import cn.edu.ntu.java.javase.reflect.model.Animal;
3+
import cn.edu.ntu.java.javase.jvm.spi.Animal;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package cn.edu.ntu.java.javase.reflect.model.type;
1+
package cn.edu.ntu.java.javase.jvm.spi.type;
22

3-
import cn.edu.ntu.java.javase.reflect.model.Animal;
4-
import cn.edu.ntu.java.javase.reflect.model.impl.Cat;
5-
import cn.edu.ntu.java.javase.reflect.model.impl.Dog;
6-
import cn.edu.ntu.java.javase.reflect.model.impl.Elephant;
3+
import cn.edu.ntu.java.javase.jvm.spi.impl.Elephant;
4+
import cn.edu.ntu.java.javase.jvm.spi.Animal;
5+
import cn.edu.ntu.java.javase.jvm.spi.impl.Cat;
6+
import cn.edu.ntu.java.javase.jvm.spi.impl.Dog;
77
import lombok.extern.slf4j.Slf4j;
88
import org.junit.Test;
99
import org.slf4j.Logger;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cn.edu.ntu.java.javase.jvm.spi.impl.Cat
2+
cn.edu.ntu.java.javase.jvm.spi.impl.Dog
3+
cn.edu.ntu.java.javase.jvm.spi.impl.Elephant
4+

java/javase/javase-proxy/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>javase</artifactId>
7+
<groupId>cn.edu.ntu.java</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>javase-proxy</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>cn.edu.ntu.java</groupId>
17+
<artifactId>javase-common</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>cglib</groupId>
23+
<artifactId>cglib</artifactId>
24+
<version>3.1</version>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.edu.ntu.proxy;
2+
3+
/**
4+
* @author zack <br>
5+
* @create 2022-12-22 17:16 <br>
6+
* @project javas-jhm <br>
7+
*/
8+
public interface Calculator {
9+
10+
int add(int i, int j);
11+
12+
int sub(int i, int j);
13+
14+
int mult(int i, int j);
15+
16+
int div(int i, int j);
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.edu.ntu.proxy;
2+
3+
/**
4+
* @author zack <br>
5+
* @create 2022-12-22 17:16 <br>
6+
* @project javas-jhm <br>
7+
*/
8+
public class CalculatorImpl implements Calculator {
9+
10+
@Override
11+
public int add(int i, int j) {
12+
13+
int result = i + j;
14+
return result;
15+
}
16+
17+
@Override
18+
public int sub(int i, int j) {
19+
20+
int result = i - j;
21+
return result;
22+
}
23+
24+
@Override
25+
public int mult(int i, int j) {
26+
27+
int result = i * j;
28+
return result;
29+
}
30+
31+
@Override
32+
public int div(int i, int j) {
33+
34+
int result = i / j;
35+
return result;
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.edu.ntu.proxy.dynamic.cglib;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.sf.cglib.proxy.MethodInterceptor;
5+
import net.sf.cglib.proxy.MethodProxy;
6+
7+
import java.lang.reflect.Method;
8+
9+
/**
10+
* @author zack <br>
11+
* @create 2022-12-22 20:12 <br>
12+
* @project javas-jhm <br>
13+
*/
14+
@Slf4j
15+
public class CglibCalculatorProxy implements MethodInterceptor {
16+
17+
@Override
18+
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
19+
throws Throwable {
20+
21+
log.info("The method {} begins with: {}", method.getName(), objects);
22+
Object result = methodProxy.invokeSuper(o, objects);
23+
log.info("The method {} ends with: {}", method.getName(), result);
24+
25+
return result;
26+
}
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.edu.ntu.proxy.dynamic.cglib;
2+
3+
import cn.edu.ntu.proxy.CalculatorImpl;
4+
import lombok.extern.slf4j.Slf4j;
5+
import net.sf.cglib.core.DebuggingClassWriter;
6+
import net.sf.cglib.proxy.Enhancer;
7+
8+
/**
9+
* @author zack <br>
10+
* @create 2022-12-22 20:01 <br>
11+
* @project javas-jhm <br>
12+
*/
13+
@Slf4j
14+
public class ClientTest {
15+
16+
public static void main(String[] args) {
17+
18+
CglibCalculatorProxy proxy = new CglibCalculatorProxy();
19+
// 动态代理创建的class文件存储到本地
20+
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "d:\\code");
21+
// 通过cglib动态代理获取代理对象的过程,创建调用的对象,在后续创建过程中EnhanceKey的对象
22+
// 所以在进行enhancer对象创建的时候需要把EnhancerKey(newInstance)对象准备好,恰好这个对象也需要动态代理来生成
23+
Enhancer enhancer = new Enhancer();
24+
// 设置enhancer对象的父类
25+
enhancer.setSuperclass(CalculatorImpl.class);
26+
// 设置enhancer的回调对象
27+
enhancer.setCallback(new CglibCalculatorProxy());
28+
// 创建代理对象
29+
CalculatorImpl myCalculator = (CalculatorImpl) enhancer.create();
30+
// 通过代理对象调用目标方法
31+
myCalculator.add(1, 1);
32+
System.out.println(myCalculator.getClass());
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.edu.ntu.proxy.dynamic.jdk;
2+
3+
import cn.edu.ntu.proxy.Calculator;
4+
import cn.edu.ntu.proxy.CalculatorImpl;
5+
import lombok.extern.slf4j.Slf4j;
6+
7+
/**
8+
* @author zack <br>
9+
* @create 2022-12-22 20:01 <br>
10+
* @project javas-jhm <br>
11+
*/
12+
@Slf4j
13+
public class ClientTest {
14+
15+
public static void main(String[] args) {
16+
17+
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
18+
Calculator proxy = JdkCalculatorProxy.getProxy(new CalculatorImpl());
19+
proxy.add(1, 1);
20+
log.info("{}", proxy.getClass());
21+
}
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cn.edu.ntu.proxy.dynamic.jdk;
2+
3+
import cn.edu.ntu.proxy.Calculator;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
import java.lang.reflect.InvocationHandler;
7+
import java.lang.reflect.Method;
8+
import java.lang.reflect.Proxy;
9+
10+
/**
11+
* @author zack <br>
12+
* @create 2022-12-22 20:07 <br>
13+
* @project javas-jhm <br>
14+
*/
15+
@Slf4j
16+
public class JdkCalculatorProxy {
17+
18+
public static Calculator getProxy(Calculator calculator) {
19+
20+
ClassLoader loader = calculator.getClass().getClassLoader();
21+
Class<?>[] interfaces = calculator.getClass().getInterfaces();
22+
InvocationHandler h =
23+
new InvocationHandler() {
24+
@Override
25+
public Object invoke(Object proxy, Method method, Object[] args)
26+
throws Throwable {
27+
Object result = null;
28+
try {
29+
log.info("The method {} begins with: {}", method.getName(), args);
30+
result = method.invoke(calculator, args);
31+
log.info("The method {} ends with: {}", method.getName(), result);
32+
} catch (Exception e) {
33+
} finally {
34+
}
35+
return result;
36+
}
37+
};
38+
39+
Object proxy = Proxy.newProxyInstance(loader, interfaces, h);
40+
return (Calculator) proxy;
41+
}
42+
}

java/javase/javase-reflect/src/main/java/cn/edu/ntu/java/javase/reflect/proxy/Handler.java renamed to java/javase/javase-proxy/src/main/java/cn/edu/ntu/proxy/dynamic/jdk/deprecate/Handler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.edu.ntu.java.javase.reflect.proxy;
1+
package cn.edu.ntu.proxy.dynamic.jdk.deprecate;
22

33
/**
44
* @author zack <br>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package cn.edu.ntu.java.javase.reflect.proxy;
1+
package cn.edu.ntu.proxy.dynamic.jdk.deprecate;
22

3-
import cn.edu.ntu.java.javase.reflect.proxy.impl.HandlerImpl;
4-
import cn.edu.ntu.java.javase.reflect.proxy.impl.HandlerInvocation;
5-
import cn.edu.ntu.java.javase.reflect.proxy.impl.HandlerProxy;
3+
import cn.edu.ntu.proxy.dynamic.jdk.deprecate.impl.HandlerImpl;
4+
import cn.edu.ntu.proxy.dynamic.jdk.deprecate.impl.HandlerInvocation;
5+
import cn.edu.ntu.proxy.dynamic.jdk.deprecate.impl.StaticHandlerProxy;
66
import org.junit.Test;
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
@@ -28,7 +28,7 @@ public class ProxyStrategyTest {
2828
@Test
2929
public void testHandlerProxy() {
3030
Handler handler = new HandlerImpl();
31-
Handler proxy = new HandlerProxy(handler);
31+
Handler proxy = new StaticHandlerProxy(handler);
3232
proxy.handle("Test");
3333
}
3434

java/javase/javase-reflect/src/main/java/cn/edu/ntu/java/javase/reflect/proxy/impl/HandlerImpl.java renamed to java/javase/javase-proxy/src/main/java/cn/edu/ntu/proxy/dynamic/jdk/deprecate/impl/HandlerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package cn.edu.ntu.java.javase.reflect.proxy.impl;
1+
package cn.edu.ntu.proxy.dynamic.jdk.deprecate.impl;
22

3-
import cn.edu.ntu.java.javase.reflect.proxy.Handler;
3+
import cn.edu.ntu.proxy.dynamic.jdk.deprecate.Handler;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

0 commit comments

Comments
 (0)