Skip to content

Commit 67303db

Browse files
Merge pull request #12184 from lsieun/master
BAEL-4463: What is com.sun.proxy.$Proxy
2 parents 4e07d9c + f051711 commit 67303db

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.reflection.proxy;
2+
3+
public interface AdvancedOperation {
4+
int multiply(int a, int b);
5+
6+
int divide(int a, int b);
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.reflection.proxy;
2+
3+
public interface BasicOperation {
4+
int add(int a, int b);
5+
6+
int subtract(int a, int b);
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.reflection.proxy;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Proxy;
6+
import java.util.function.Consumer;
7+
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class DollarProxyUnitTest {
11+
@Test
12+
public void givenProxy_whenInvokingGetProxyClass_thenGeneratingProxyClass() {
13+
// Java 8: -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
14+
// Java 9 or later: -Djdk.proxy.ProxyGenerator.saveGeneratedFiles=true
15+
// Note: System.setProperty() doesn't work here
16+
// because ProxyGenerator.saveGeneratedFiles read its property only once.
17+
// The @Test annotation in this method will generate a $Proxy class.
18+
19+
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
20+
Class<?>[] interfaces = {BasicOperation.class, AdvancedOperation.class};
21+
Class<?> proxyClass = Proxy.getProxyClass(classLoader, interfaces);
22+
23+
boolean isProxyClass = Proxy.isProxyClass(proxyClass);
24+
assertTrue(isProxyClass);
25+
}
26+
27+
@Test
28+
public void givenReflection_whenReadingAnnotation_thenGeneratingProxyClass() {
29+
FunctionalInterface instance = Consumer.class.getDeclaredAnnotation(FunctionalInterface.class);
30+
Class<?> clazz = instance.getClass();
31+
32+
boolean isProxyClass = Proxy.isProxyClass(clazz);
33+
assertTrue(isProxyClass);
34+
}
35+
}

0 commit comments

Comments
 (0)