BAEL-4463: What is com.sun.proxy.$Proxy

This commit is contained in:
515882294 2022-05-08 16:00:41 +08:00
parent 44bb031933
commit 9a1d66f852
3 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package com.baeldung.reflection.proxy;
public interface AdvancedOperation {
int multiple(int a, int b);
int divide(int a, int b);
}

View File

@ -0,0 +1,7 @@
package com.baeldung.reflection.proxy;
public interface BasicOperation {
int add(int a, int b);
int sub(int a, int b);
}

View File

@ -0,0 +1,35 @@
package com.baeldung.reflection.proxy;
import org.junit.Test;
import java.lang.reflect.Proxy;
import java.util.function.Consumer;
import static org.junit.Assert.assertTrue;
public class DollarProxyUnitTest {
@Test
public void givenProxy_whenInvokingGetProxyClass_thenGeneratingProxyClass() {
// Java 8: -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
// Java 9 or later: -Djdk.proxy.ProxyGenerator.saveGeneratedFiles=true
// Note: System.setProperty() doesn't work here
// because ProxyGenerator.saveGeneratedFiles read its property only once.
// The @Test annotation in this method will generate a $Proxy class.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?>[] interfaces = {BasicOperation.class, AdvancedOperation.class};
Class<?> proxyClass = Proxy.getProxyClass(classLoader, interfaces);
boolean isProxyClass = Proxy.isProxyClass(proxyClass);
assertTrue(isProxyClass);
}
@Test
public void givenReflection_whenReadingAnnotation_thenGeneratingProxyClass() {
FunctionalInterface instance = Consumer.class.getDeclaredAnnotation(FunctionalInterface.class);
Class<?> clazz = instance.getClass();
boolean isProxyClass = Proxy.isProxyClass(clazz);
assertTrue(isProxyClass);
}
}