Skip to content

Commit 772002f

Browse files
dkornishevpivovarit
authored andcommitted
Dynamic Proxy (eugenp#1702)
1 parent 594920a commit 772002f

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.dynamicproxy;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.lang.reflect.InvocationHandler;
7+
import java.lang.reflect.Method;
8+
9+
public class DynamicInvocationHandler implements InvocationHandler {
10+
11+
private static Logger LOGGER = LoggerFactory.getLogger(DynamicInvocationHandler.class);
12+
13+
@Override
14+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
15+
LOGGER.info("Invoked method: {}", method.getName());
16+
17+
return 42;
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.dynamicproxy;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
public class TimingDynamicInvocationHandler implements InvocationHandler {
12+
13+
private static Logger LOGGER = LoggerFactory.getLogger(TimingDynamicInvocationHandler.class);
14+
private final Map<String, Method> methods = new HashMap<>();
15+
16+
private Object target;
17+
18+
public TimingDynamicInvocationHandler(Object target) {
19+
this.target = target;
20+
21+
for(Method method: target.getClass().getDeclaredMethods()) {
22+
this.methods.put(method.getName(), method);
23+
}
24+
}
25+
26+
@Override
27+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
28+
long start = System.nanoTime();
29+
Object result = methods.get(method.getName()).invoke(target, args);
30+
long elapsed = System.nanoTime() - start;
31+
32+
LOGGER.info("Executing {} finished in {} ns", method.getName(), elapsed);
33+
34+
return result;
35+
}
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.dynamicproxy;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Proxy;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.testng.Assert.fail;
11+
12+
public class DynamicProxyTest {
13+
14+
@Test
15+
public void givenDynamicProxy_thenPutWorks() {
16+
Map proxyInstance = (Map) Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(), new Class[] { Map.class }, new DynamicInvocationHandler());
17+
18+
proxyInstance.put("hello", "world");
19+
}
20+
21+
@Test
22+
public void givenInlineDynamicProxy_thenGetWorksOtherMethodsDoNot() {
23+
Map proxyInstance = (Map) Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(), new Class[] { Map.class }, (proxy, method, methodArgs) -> {
24+
25+
if (method.getName().equals("get")) {
26+
return 42;
27+
} else {
28+
throw new UnsupportedOperationException("Unsupported method: " + method.getName());
29+
}
30+
});
31+
32+
int result = (int) proxyInstance.get("hello");
33+
34+
assertEquals(42, result);
35+
36+
try {
37+
proxyInstance.put("hello", "world");
38+
fail();
39+
} catch(UnsupportedOperationException e) {
40+
// expected
41+
}
42+
}
43+
44+
@Test
45+
public void givenTimingDynamicProxy_thenMethodInvokationsProduceTiming() {
46+
Map mapProxyInstance = (Map) Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(), new Class[] { Map.class }, new TimingDynamicInvocationHandler(new HashMap<>()));
47+
48+
mapProxyInstance.put("hello", "world");
49+
assertEquals("world", mapProxyInstance.get("hello"));
50+
51+
CharSequence csProxyInstance = (CharSequence) Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(), new Class[] { CharSequence.class }, new TimingDynamicInvocationHandler("Hello World"));
52+
53+
assertEquals('l', csProxyInstance.charAt(2));
54+
assertEquals(11, csProxyInstance.length());
55+
}
56+
57+
}

0 commit comments

Comments
 (0)