From c1409aacef9d409f645ebeac2daf00cc27304f1b Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Thu, 11 Apr 2013 19:12:59 +0400 Subject: [PATCH] 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions Reviewed-by: alexsch --- .../classes/java/beans/Introspector.java | 16 ++++++- .../java/beans/Introspector/Test4683761.java | 45 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 jdk/test/java/beans/Introspector/Test4683761.java diff --git a/jdk/src/share/classes/java/beans/Introspector.java b/jdk/src/share/classes/java/beans/Introspector.java index 48e7e3be4a1..65d95eb4e80 100644 --- a/jdk/src/share/classes/java/beans/Introspector.java +++ b/jdk/src/share/classes/java/beans/Introspector.java @@ -28,6 +28,7 @@ package java.beans; 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 @@ public class Introspector { 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); diff --git a/jdk/test/java/beans/Introspector/Test4683761.java b/jdk/test/java/beans/Introspector/Test4683761.java new file mode 100644 index 00000000000..dbf6f857580 --- /dev/null +++ b/jdk/test/java/beans/Introspector/Test4683761.java @@ -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 map = new HashMap(); + map.put("key", "value"); + Map.Entry entry = map.entrySet().iterator().next(); + for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(entry.getClass())) { + System.out.println(pd.getName() + " = " + pd.getReadMethod().invoke(entry)); + } + } +}