4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
authormalenkov
Thu, 11 Apr 2013 19:12:59 +0400
changeset 16837 1af9ab28cf6d
parent 16711 7a5dc9467315
child 16838 ea714f11d40d
4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions Reviewed-by: alexsch
jdk/src/share/classes/java/beans/Introspector.java
jdk/test/java/beans/Introspector/Test4683761.java
--- a/jdk/src/share/classes/java/beans/Introspector.java	Mon Apr 08 17:18:16 2013 +0400
+++ b/jdk/src/share/classes/java/beans/Introspector.java	Thu Apr 11 19:12:59 2013 +0400
@@ -28,6 +28,7 @@
 import com.sun.beans.TypeResolver;
 import com.sun.beans.WeakCache;
 import com.sun.beans.finder.ClassFinder;
+import com.sun.beans.finder.MethodFinder;
 
 import java.awt.Component;
 
@@ -1281,7 +1282,20 @@
                 for (int i = 0; i < result.length; i++) {
                     Method method = result[i];
                     if (!method.getDeclaringClass().equals(clz)) {
-                        result[i] = null;
+                        result[i] = null; // ignore methods declared elsewhere
+                    }
+                    else {
+                        try {
+                            method = MethodFinder.findAccessibleMethod(method);
+                            Class<?> type = method.getDeclaringClass();
+                            result[i] = type.equals(clz) || type.isInterface()
+                                    ? method
+                                    : null; // ignore methods from superclasses
+                        }
+                        catch (NoSuchMethodException exception) {
+                            // commented out because of 6976577
+                            // result[i] = null; // ignore inaccessible methods
+                        }
                     }
                 }
                 declaredMethodCache.put(clz, result);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/Introspector/Test4683761.java	Thu Apr 11 19:12:59 2013 +0400
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2012, 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.
+ */
+
+/*
+ * @test
+ * @bug 4683761
+ * @summary Tests that all public methods in a public class
+ * @author Sergey Malenkov
+ */
+
+import java.beans.PropertyDescriptor;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Test4683761 {
+    public static void main(String[] args) throws Exception {
+        System.setSecurityManager(new SecurityManager());
+        Map<String, String> map = new HashMap<String, String>();
+        map.put("key", "value");
+        Map.Entry<String, String> entry = map.entrySet().iterator().next();
+        for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(entry.getClass())) {
+            System.out.println(pd.getName() + " = " + pd.getReadMethod().invoke(entry));
+        }
+    }
+}