File tree Expand file tree Collapse file tree
core-java-modules/core-java-reflection-2/src
main/java/com/baeldung/reflection/proxy
test/java/com/baeldung/reflection/proxy Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments