提交 45af944f 编写于 作者: M mchung

4487672: (proxy) Proxy constructor should check for null argument

Reviewed-by: alanb, lancea
上级 adbcdc0d
...@@ -31,6 +31,7 @@ import java.security.PrivilegedAction; ...@@ -31,6 +31,7 @@ import java.security.PrivilegedAction;
import java.util.Arrays; import java.util.Arrays;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import sun.misc.ProxyGenerator; import sun.misc.ProxyGenerator;
...@@ -255,9 +256,13 @@ public class Proxy implements java.io.Serializable { ...@@ -255,9 +256,13 @@ public class Proxy implements java.io.Serializable {
* (typically, a dynamic proxy class) with the specified value * (typically, a dynamic proxy class) with the specified value
* for its invocation handler. * for its invocation handler.
* *
* @param h the invocation handler for this proxy instance * @param h the invocation handler for this proxy instance
*
* @throws NullPointerException if the given invocation handler, {@code h},
* is {@code null}.
*/ */
protected Proxy(InvocationHandler h) { protected Proxy(InvocationHandler h) {
Objects.requireNonNull(h);
this.h = h; this.h = h;
} }
...@@ -698,9 +703,7 @@ public class Proxy implements java.io.Serializable { ...@@ -698,9 +703,7 @@ public class Proxy implements java.io.Serializable {
InvocationHandler h) InvocationHandler h)
throws IllegalArgumentException throws IllegalArgumentException
{ {
if (h == null) { Objects.requireNonNull(h);
throw new NullPointerException();
}
final SecurityManager sm = System.getSecurityManager(); final SecurityManager sm = System.getSecurityManager();
if (sm != null) { if (sm != null) {
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
*/ */
/* @test /* @test
* @bug 4227192 * @bug 4227192 4487672
* @summary This is a basic functional test of the dynamic proxy API (part 1). * @summary This is a basic functional test of the dynamic proxy API (part 1).
* @author Peter Jones * @author Peter Jones
* *
...@@ -42,15 +42,15 @@ public class Basic1 { ...@@ -42,15 +42,15 @@ public class Basic1 {
"\nBasic functional test of dynamic proxy API, part 1\n"); "\nBasic functional test of dynamic proxy API, part 1\n");
try { try {
Class[] interfaces = Class<?>[] interfaces =
new Class[] { Runnable.class, Observer.class }; new Class<?>[] { Runnable.class, Observer.class };
ClassLoader loader = ClassLoader.getSystemClassLoader(); ClassLoader loader = ClassLoader.getSystemClassLoader();
/* /*
* Generate a proxy class. * Generate a proxy class.
*/ */
Class proxyClass = Proxy.getProxyClass(loader, interfaces); Class<?> proxyClass = Proxy.getProxyClass(loader, interfaces);
System.err.println("+ generated proxy class: " + proxyClass); System.err.println("+ generated proxy class: " + proxyClass);
/* /*
...@@ -72,19 +72,19 @@ public class Basic1 { ...@@ -72,19 +72,19 @@ public class Basic1 {
/* /*
* Verify that it is assignable to the proxy interfaces. * Verify that it is assignable to the proxy interfaces.
*/ */
for (int i = 0; i < interfaces.length; i++) { for (Class<?> intf : interfaces) {
if (!interfaces[i].isAssignableFrom(proxyClass)) { if (!intf.isAssignableFrom(proxyClass)) {
throw new RuntimeException( throw new RuntimeException(
"proxy class not assignable to proxy interface " + "proxy class not assignable to proxy interface " +
interfaces[i].getName()); intf.getName());
} }
} }
/* /*
* Verify that it has the given permutation of interfaces. * Verify that it has the given permutation of interfaces.
*/ */
List l1 = Arrays.asList(interfaces); List<Class<?>> l1 = Arrays.asList(interfaces);
List l2 = Arrays.asList(proxyClass.getInterfaces()); List<Class<?>> l2 = Arrays.asList(proxyClass.getInterfaces());
System.err.println("+ proxy class's interfaces: " + l2); System.err.println("+ proxy class's interfaces: " + l2);
if (!l1.equals(l2)) { if (!l1.equals(l2)) {
throw new RuntimeException( throw new RuntimeException(
...@@ -118,14 +118,26 @@ public class Basic1 { ...@@ -118,14 +118,26 @@ public class Basic1 {
* Verify that it has a constructor that takes an * Verify that it has a constructor that takes an
* InvocationHandler instance. * InvocationHandler instance.
*/ */
Constructor cons = proxyClass.getConstructor( Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class);
new Class[] { InvocationHandler.class });
/*
* Test constructor with null InvocationHandler
*/
try {
cons.newInstance(new Object[] { null });
throw new RuntimeException("Expected NullPointerException thrown");
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (!(t instanceof NullPointerException)) {
throw t;
}
}
/* /*
* Construct a proxy instance. * Construct a proxy instance.
*/ */
Handler handler = new Handler(); Handler handler = new Handler();
Object proxy = cons.newInstance(new Object[] { handler }); Object proxy = cons.newInstance(handler);
handler.currentProxy = proxy; handler.currentProxy = proxy;
/* /*
...@@ -141,7 +153,7 @@ public class Basic1 { ...@@ -141,7 +153,7 @@ public class Basic1 {
System.err.println("\nTEST PASSED"); System.err.println("\nTEST PASSED");
} catch (Exception e) { } catch (Throwable e) {
System.err.println("\nTEST FAILED:"); System.err.println("\nTEST FAILED:");
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("TEST FAILED: " + e.toString()); throw new RuntimeException("TEST FAILED: " + e.toString());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册