33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.IdentityHashMap;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.Set;
41 import java.util.concurrent.atomic.AtomicInteger;
42 import java.util.concurrent.atomic.AtomicLong;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45
46 import jdk.internal.loader.BootLoader;
47 import jdk.internal.module.Modules;
48 import jdk.internal.misc.Unsafe;
49 import jdk.internal.misc.VM;
50 import jdk.internal.reflect.CallerSensitive;
51 import jdk.internal.reflect.Reflection;
52 import sun.reflect.misc.ReflectUtil;
53 import sun.security.util.SecurityConstants;
54
55 /**
56 *
57 * {@code Proxy} provides static methods for creating objects that act like instances
58 * of interfaces but allow for customized method invocation.
59 * To create a proxy instance for some interface {@code Foo}:
60 * <pre>{@code
61 * InvocationHandler handler = new MyInvocationHandler(...);
62 * Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
63 * new Class<?>[] { Foo.class },
64 * handler);
65 * }</pre>
66 *
67 * <p>
68 * A <em>proxy class</em> is a class created at runtime that implements a specified
69 * list of interfaces, known as <em>proxy interfaces</em>. A <em>proxy instance</em>
70 * is an instance of a proxy class.
71 *
72 * Each proxy instance has an associated <i>invocation handler</i>
564 access = "package-private";
565 } else {
566 access = "module-private";
567 }
568 ClassLoader ld = c.getClassLoader();
569 return String.format(" %s/%s %s loader %s",
570 c.getModule().getName(), c.getName(), access, ld);
571 }
572
573 static void trace(String cn, Module module, ClassLoader loader, List<Class<?>> interfaces) {
574 if (isDebug()) {
575 System.out.format("PROXY: %s/%s defined by %s%n", module.getName(), cn, loader);
576 }
577 if (isDebug("debug")) {
578 interfaces.stream()
579 .forEach(c -> System.out.println(toDetails(c)));
580 }
581 }
582
583 private static final String DEBUG =
584 AccessController.doPrivileged(new PrivilegedAction<>() {
585 public String run() {
586 return System.getProperty("jdk.proxy.debug", "");
587 }
588 });
589
590 private static boolean isDebug() {
591 return !DEBUG.isEmpty();
592 }
593 private static boolean isDebug(String flag) {
594 return DEBUG.equals(flag);
595 }
596
597 // ProxyBuilder instance members start here....
598
599 private final ClassLoader loader;
600 private final List<Class<?>> interfaces;
601 private final Module module;
602 ProxyBuilder(ClassLoader loader, List<Class<?>> interfaces) {
603 if (!VM.isModuleSystemInited()) {
604 throw new InternalError("Proxy is not supported until module system is fully initialzed");
605 }
606 if (interfaces.size() > 65535) {
607 throw new IllegalArgumentException("interface limit exceeded");
608 }
|
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.IdentityHashMap;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.Set;
41 import java.util.concurrent.atomic.AtomicInteger;
42 import java.util.concurrent.atomic.AtomicLong;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45
46 import jdk.internal.loader.BootLoader;
47 import jdk.internal.module.Modules;
48 import jdk.internal.misc.Unsafe;
49 import jdk.internal.misc.VM;
50 import jdk.internal.reflect.CallerSensitive;
51 import jdk.internal.reflect.Reflection;
52 import sun.reflect.misc.ReflectUtil;
53 import sun.security.action.GetPropertyAction;
54 import sun.security.util.SecurityConstants;
55
56 /**
57 *
58 * {@code Proxy} provides static methods for creating objects that act like instances
59 * of interfaces but allow for customized method invocation.
60 * To create a proxy instance for some interface {@code Foo}:
61 * <pre>{@code
62 * InvocationHandler handler = new MyInvocationHandler(...);
63 * Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
64 * new Class<?>[] { Foo.class },
65 * handler);
66 * }</pre>
67 *
68 * <p>
69 * A <em>proxy class</em> is a class created at runtime that implements a specified
70 * list of interfaces, known as <em>proxy interfaces</em>. A <em>proxy instance</em>
71 * is an instance of a proxy class.
72 *
73 * Each proxy instance has an associated <i>invocation handler</i>
565 access = "package-private";
566 } else {
567 access = "module-private";
568 }
569 ClassLoader ld = c.getClassLoader();
570 return String.format(" %s/%s %s loader %s",
571 c.getModule().getName(), c.getName(), access, ld);
572 }
573
574 static void trace(String cn, Module module, ClassLoader loader, List<Class<?>> interfaces) {
575 if (isDebug()) {
576 System.out.format("PROXY: %s/%s defined by %s%n", module.getName(), cn, loader);
577 }
578 if (isDebug("debug")) {
579 interfaces.stream()
580 .forEach(c -> System.out.println(toDetails(c)));
581 }
582 }
583
584 private static final String DEBUG =
585 GetPropertyAction.getProperty("jdk.proxy.debug", "");
586
587 private static boolean isDebug() {
588 return !DEBUG.isEmpty();
589 }
590 private static boolean isDebug(String flag) {
591 return DEBUG.equals(flag);
592 }
593
594 // ProxyBuilder instance members start here....
595
596 private final ClassLoader loader;
597 private final List<Class<?>> interfaces;
598 private final Module module;
599 ProxyBuilder(ClassLoader loader, List<Class<?>> interfaces) {
600 if (!VM.isModuleSystemInited()) {
601 throw new InternalError("Proxy is not supported until module system is fully initialzed");
602 }
603 if (interfaces.size() > 65535) {
604 throw new IllegalArgumentException("interface limit exceeded");
605 }
|