8012277: Improve AWT DataFlavor
authormalenkov
Fri, 31 May 2013 21:25:42 +0400
changeset 20803 140bc93ee613
parent 20802 758273542f3d
child 20804 18285d130365
8012277: Improve AWT DataFlavor Reviewed-by: art, skoivu
jdk/src/share/classes/java/awt/datatransfer/DataFlavor.java
--- a/jdk/src/share/classes/java/awt/datatransfer/DataFlavor.java	Mon May 20 19:49:20 2013 +0400
+++ b/jdk/src/share/classes/java/awt/datatransfer/DataFlavor.java	Fri May 31 21:25:42 2013 +0400
@@ -30,6 +30,9 @@
 import java.util.*;
 
 import sun.awt.datatransfer.DataTransferer;
+import sun.reflect.misc.ReflectUtil;
+
+import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
 
 /**
  * A {@code DataFlavor} provides meta information about data. {@code DataFlavor}
@@ -116,26 +119,36 @@
                                                    ClassLoader fallback)
         throws ClassNotFoundException
     {
-        ClassLoader systemClassLoader = (ClassLoader)
-            java.security.AccessController.doPrivileged(
-                new java.security.PrivilegedAction() {
-                    public Object run() {
-                        ClassLoader cl = Thread.currentThread().
-                            getContextClassLoader();
-                        return (cl != null)
-                            ? cl
-                            : ClassLoader.getSystemClassLoader();
+        ReflectUtil.checkPackageAccess(className);
+        try {
+            SecurityManager sm = System.getSecurityManager();
+            if (sm != null) {
+                sm.checkPermission(GET_CLASSLOADER_PERMISSION);
+            }
+            ClassLoader loader = ClassLoader.getSystemClassLoader();
+            try {
+                // bootstrap class loader and system class loader if present
+                return Class.forName(className, true, loader);
+            }
+            catch (ClassNotFoundException exception) {
+                // thread context class loader if and only if present
+                loader = Thread.currentThread().getContextClassLoader();
+                if (loader != null) {
+                    try {
+                        return Class.forName(className, true, loader);
                     }
-                    });
-
-        try {
-            return Class.forName(className, true, systemClassLoader);
-        } catch (ClassNotFoundException e2) {
-            if (fallback != null) {
-                return Class.forName(className, true, fallback);
-            } else {
-                throw new ClassNotFoundException(className);
+                    catch (ClassNotFoundException e) {
+                        // fallback to user's class loader
+                    }
+                }
             }
+        } catch (SecurityException exception) {
+            // ignore secured class loaders
+        }
+        if (fallback != null) {
+            return Class.forName(className, true, fallback);
+        } else {
+            throw new ClassNotFoundException(className);
         }
     }