Merge pull request #12184 from lsieun/master

BAEL-4463: What is com.sun.proxy.$Proxy
This commit is contained in:
davidmartinezbarua 2022-05-18 13:34:46 -03:00 committed by GitHub
commit 67303db98b
3 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package com.baeldung.reflection.proxy;
public interface AdvancedOperation {
int multiply(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 subtract(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);
}
}