8012716: java.beans.EventHandler.create method should check if the given listenerInterface is a public interface
authormalenkov
Fri, 27 Sep 2013 22:17:24 +0400
changeset 20432 5060b1928712
parent 20431 3845c63e933d
child 20433 f6d501f12376
8012716: java.beans.EventHandler.create method should check if the given listenerInterface is a public interface Reviewed-by: art, mchung
jdk/src/share/classes/java/beans/EventHandler.java
--- a/jdk/src/share/classes/java/beans/EventHandler.java	Fri Sep 27 18:35:00 2013 +0400
+++ b/jdk/src/share/classes/java/beans/EventHandler.java	Fri Sep 27 22:17:24 2013 +0400
@@ -33,6 +33,7 @@
 import java.security.PrivilegedAction;
 
 import sun.reflect.misc.MethodUtil;
+import sun.reflect.misc.ReflectUtil;
 
 /**
  * The <code>EventHandler</code> class provides
@@ -677,22 +678,38 @@
      *
      * @see EventHandler
      */
-    @SuppressWarnings("unchecked")
     public static <T> T create(Class<T> listenerInterface,
                                Object target, String action,
                                String eventPropertyName,
                                String listenerMethodName)
     {
         // Create this first to verify target/action are non-null
-        EventHandler eventHandler = new EventHandler(target, action,
+        final EventHandler handler = new EventHandler(target, action,
                                                      eventPropertyName,
                                                      listenerMethodName);
         if (listenerInterface == null) {
             throw new NullPointerException(
                           "listenerInterface must be non-null");
         }
-        return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(),
-                                         new Class<?>[] {listenerInterface},
-                                         eventHandler);
+        final ClassLoader loader = getClassLoader(listenerInterface);
+        final Class<?>[] interfaces = {listenerInterface};
+        return AccessController.doPrivileged(new PrivilegedAction<T>() {
+            @SuppressWarnings("unchecked")
+            public T run() {
+                return (T) Proxy.newProxyInstance(loader, interfaces, handler);
+            }
+        });
+    }
+
+    private static ClassLoader getClassLoader(Class<?> type) {
+        ReflectUtil.checkPackageAccess(type);
+        ClassLoader loader = type.getClassLoader();
+        if (loader == null) {
+            loader = Thread.currentThread().getContextClassLoader(); // avoid use of BCP
+            if (loader == null) {
+                loader = ClassLoader.getSystemClassLoader();
+            }
+        }
+        return loader;
     }
 }