8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
authornaoto
Thu, 29 Nov 2018 10:13:42 -0800
changeset 52766 26b7f3038e27
parent 52765 265209adbe77
child 52767 db7a459e10eb
8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public. Reviewed-by: rriggs, mchung
src/java.base/share/classes/java/util/ResourceBundle.java
test/jdk/java/util/ResourceBundle/Control/MissingResourceCauseTest.java
test/jdk/java/util/ResourceBundle/Control/MissingResourceCauseTest.sh
test/jdk/java/util/ResourceBundle/Control/NoNoArgConstructorRB.java
--- a/src/java.base/share/classes/java/util/ResourceBundle.java	Mon Nov 19 15:01:59 2018 -0800
+++ b/src/java.base/share/classes/java/util/ResourceBundle.java	Thu Nov 29 10:13:42 2018 -0800
@@ -3184,10 +3184,16 @@
                                 bundleClass.getName() + " in " + m.toString());
                         }
                         try {
-                            // bundle in a unnamed module
-                            Constructor<ResourceBundle> ctor = bundleClass.getConstructor();
+                            Constructor<ResourceBundle> ctor = AccessController.doPrivileged(
+                                new PrivilegedExceptionAction<>() {
+                                    @Override
+                                    public Constructor<ResourceBundle> run() throws NoSuchMethodException {
+                                        return bundleClass.getDeclaredConstructor();
+                                    }
+                                });
                             if (!Modifier.isPublic(ctor.getModifiers())) {
-                                return null;
+                                throw new IllegalAccessException("no-arg constructor in " +
+                                    bundleClass.getName() + " is not publicly accessible.");
                             }
 
                             // java.base may not be able to read the bundleClass's module.
@@ -3196,12 +3202,16 @@
                             bundle = ctor.newInstance((Object[]) null);
                         } catch (InvocationTargetException e) {
                             uncheckedThrow(e);
+                        } catch (PrivilegedActionException e) {
+                            assert e.getException() instanceof NoSuchMethodException;
+                            throw new InstantiationException("public no-arg constructor " +
+                                "does not exist in " + bundleClass.getName());
                         }
                     } else {
                         throw new ClassCastException(c.getName()
                                 + " cannot be cast to ResourceBundle");
                     }
-                } catch (ClassNotFoundException|NoSuchMethodException e) {
+                } catch (ClassNotFoundException e) {
                 }
             } else if (format.equals("java.properties")) {
                 final String resourceName = toResourceName0(bundleName, "properties");
--- a/test/jdk/java/util/ResourceBundle/Control/MissingResourceCauseTest.java	Mon Nov 19 15:01:59 2018 -0800
+++ b/test/jdk/java/util/ResourceBundle/Control/MissingResourceCauseTest.java	Thu Nov 29 10:13:42 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
         callGetBundle("AbstractRB", InstantiationException.class);
         callGetBundle("BadStaticInitRB", ExceptionInInitializerError.class);
         callGetBundle("UnreadableRB", IOException.class);
+        callGetBundle("NoNoArgConstructorRB", InstantiationException.class);
     }
 
     private static void callGetBundle(String baseName,
--- a/test/jdk/java/util/ResourceBundle/Control/MissingResourceCauseTest.sh	Mon Nov 19 15:01:59 2018 -0800
+++ b/test/jdk/java/util/ResourceBundle/Control/MissingResourceCauseTest.sh	Thu Nov 29 10:13:42 2018 -0800
@@ -1,5 +1,5 @@
 # 
-# Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 # 
 # This code is free software; you can redistribute it and/or modify it
@@ -28,30 +28,29 @@
 # shared with other test cases.)
 # @build MissingResourceCauseTest
 # @build NonResourceBundle PrivateConstructorRB AbstractRB BadStaticInitRB
+#   NoNoArgConstructorRB
 # @run shell MissingResourceCauseTest.sh
 
 case "`uname`" in
-Windows*)
-    DEL=";";
+Windows* | CYGWIN*)
+    exit 0;
     ;;
 *)
     DEL=":";
+    PS="/";
     ;;
 esac
 
 #
 # Create an unreadble properties file
 #
-UNREADABLE=UnreadableRB.properties
+UNREADABLE=${TESTCLASSPATH}${PS}UnreadableRB.properties
 rm -f $UNREADABLE
 echo "type=unreadable" >$UNREADABLE
 chmod 000 $UNREADABLE
 
-: ${TESTCLASS:=.}
-: ${TESTSRC:=.}
-
-${TESTJAVA}/bin/java ${TESTVMOPTS} -esa -cp ${TESTCLASS}${DEL}${TESTSRC} MissingResourceCauseTest
+${TESTJAVA}/bin/java ${TESTVMOPTS} -esa -cp ${TESTCLASSES}${DEL}${TESTSRC} MissingResourceCauseTest
 STATUS=$?
 chmod 666 $UNREADABLE
 rm -f $UNREADABLE
-exit $?
+exit $STATUS
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/util/ResourceBundle/Control/NoNoArgConstructorRB.java	Thu Nov 29 10:13:42 2018 -0800
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ *
+ */
+
+import java.util.*;
+
+public class NoNoArgConstructorRB extends ListResourceBundle {
+    public NoNoArgConstructorRB(Object o) {
+    }
+    public Object[][] getContents() {
+        return new Object[][] {
+            { "type", "class (no no-arg constructor)" }
+        };
+    }
+}