# HG changeset patch # User jfranck # Date 1378885552 -7200 # Node ID c43d94c72c41aaee7cb5daa381df7faf523ae63a # Parent e899d1502dc72fbabf3f61702b363bd1e9c33d58 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types Summary: Update spec to match long standing behavior Reviewed-by: darcy, mchung diff -r e899d1502dc7 -r c43d94c72c41 jdk/src/share/classes/java/lang/Class.java --- a/jdk/src/share/classes/java/lang/Class.java Wed Sep 11 15:29:41 2013 +0900 +++ b/jdk/src/share/classes/java/lang/Class.java Wed Sep 11 09:45:52 2013 +0200 @@ -821,6 +821,10 @@ *
If this object represents a primitive type or void, the method * returns an array of length 0. * + *
If this {@code Class} object represents an array type, the + * interfaces {@code Cloneable} and {@code java.io.Serializable} are + * returned in that order. + * * @return an array of interfaces implemented by this class. */ public Class>[] getInterfaces() { @@ -1500,7 +1504,7 @@ *
If this {@code Class} object represents an array type, a primitive * type, or void, then this method returns an array of length 0. * - *
The elements in the array returned are not sorted and are not in any + *
The elements in the returned array are not sorted and are not in any * particular order. * * @return the array of {@code Field} objects representing the @@ -1525,23 +1529,33 @@ /** - * Returns an array containing {@code Method} objects reflecting all - * the public member methods of the class or interface represented - * by this {@code Class} object, including those declared by the class - * or interface and those inherited from superclasses and - * superinterfaces. Array classes return all the (public) member methods - * inherited from the {@code Object} class. The elements in the array - * returned are not sorted and are not in any particular order. This - * method returns an array of length 0 if this {@code Class} object - * represents a class or interface that has no public member methods, or if - * this {@code Class} object represents a primitive type or void. + * Returns an array containing {@code Method} objects reflecting all the + * public methods of the class or interface represented by this {@code + * Class} object, including those declared by the class or interface and + * those inherited from superclasses and superinterfaces. + * + *
If this {@code Class} object represents a type that has multiple + * public methods with the same name and parameter types, but different + * return types, then the returned array has a {@code Method} object for + * each such method. + * + *
If this {@code Class} object represents a type with a class
+ * initialization method {@code The class initialization method {@code If this {@code Class} object represents an array type, then the
+ * returned array has a {@code Method} object for each of the public
+ * methods inherited by the array type from {@code Object}. It does not
+ * contain a {@code Method} object for {@code clone()}.
*
- * See The Java Language Specification, sections 8.2 and 8.4.
+ * If this {@code Class} object represents a class or interface with no
+ * public methods, then the returned array has length 0.
+ *
+ * If this {@code Class} object represents a primitive type or void,
+ * then the returned array has length 0.
+ *
+ * The elements in the returned array are not sorted and are not in any
+ * particular order.
*
* @return the array of {@code Method} objects representing the
* public methods of this class
@@ -1553,6 +1567,8 @@
* s.checkPackageAccess()} denies access to the package
* of this class.
*
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
@CallerSensitive
@@ -1693,7 +1709,8 @@
* method and the method being overridden would have the same
* signature but different return types.
*
- * See The Java Language Specification, sections 8.2 and 8.4.
+ * If this {@code Class} object represents an array type, then this
+ * method does not find the {@code clone()} method.
*
* @param name the name of the method
* @param parameterTypes the list of parameters
@@ -1710,6 +1727,8 @@
* s.checkPackageAccess()} denies access to the package
* of this class.
*
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
@CallerSensitive
@@ -1815,7 +1834,7 @@
* If this {@code Class} object represents an array type, a primitive
* type, or void, then this method returns an array of length 0.
*
- * The elements in the array returned are not sorted and are not in any
+ * The elements in the returned array are not sorted and are not in any
* particular order.
*
* @return the array of {@code Field} objects representing all the
@@ -1853,20 +1872,29 @@
/**
- * Returns an array of {@code Method} objects reflecting all the
- * methods declared by the class or interface represented by this
- * {@code Class} object. This includes public, protected, default
- * (package) access, and private methods, but excludes inherited methods.
- * The elements in the array returned are not sorted and are not in any
- * particular order. This method returns an array of length 0 if the class
- * or interface declares no methods, or if this {@code Class} object
- * represents a primitive type, an array class, or void. The class
- * initialization method {@code If this {@code Class} object represents a type that has multiple
+ * declared methods with the same name and parameter types, but different
+ * return types, then the returned array has a {@code Method} object for
+ * each such method.
*
- * See The Java Language Specification, section 8.2.
+ * If this {@code Class} object represents a type that has a class
+ * initialization method {@code If this {@code Class} object represents a class or interface with no
+ * declared methods, then the returned array has length 0.
+ *
+ * If this {@code Class} object represents an array type, a primitive
+ * type, or void, then the returned array has length 0.
+ *
+ * The elements in the returned array are not sorted and are not in any
+ * particular order.
*
* @return the array of {@code Method} objects representing all the
* declared methods of this class
@@ -1891,6 +1919,8 @@
*
*
*
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
@CallerSensitive
@@ -2011,6 +2041,9 @@
* name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
* is raised.
*
+ * If this {@code Class} object represents an array type, then this
+ * method does not find the {@code clone()} method.
+ *
* @param name the name of the method
* @param parameterTypes the parameter array
* @return the {@code Method} object for the method of this class
@@ -2038,6 +2071,8 @@
*
*
*
+ * @jls 8.2 Class Members
+ * @jls 8.4 Method Declarations
* @since JDK1.1
*/
@CallerSensitive
diff -r e899d1502dc7 -r c43d94c72c41 jdk/test/java/lang/Class/ArrayMethods.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/Class/ArrayMethods.java Wed Sep 11 09:45:52 2013 +0200
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2013, 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 4987375
+ * @summary make sure clone() isn't reflected and that Cloneable and
+ * Serializable are found
+ */
+
+import java.lang.reflect.*;
+import java.util.Arrays;
+
+public class ArrayMethods {
+ public int failed = 0;
+
+ public static void main(String[] args) throws Exception {
+ ArrayMethods m = new ArrayMethods();
+
+ m.testGetMethod();
+ m.testGetMethods();
+ m.testGetDeclaredMethod();
+ m.testGetDeclaredMethods();
+ m.testGetInterfaces();
+
+ if (m.failed != 0)
+ throw new RuntimeException("Test failed, check log for details");
+ }
+
+ public void testGetMethod() {
+ try {
+ Method m = new String[0].getClass().getMethod("clone", (Class>[])null);
+
+ failed++;
+ System.out.println("getMethod(\"clone\", null) Should not find clone()");
+ } catch (NoSuchMethodException e) {
+ ; //all good
+ }
+ }
+
+ public void testGetMethods() {
+ Method[] m = new Integer[0][0][0].getClass().getMethods();
+ for (Method mm : m)
+ if(mm.getName().contentEquals("clone")) {
+ failed++;
+ System.out.println("getMethods() Should not find clone()");
+ }
+ }
+
+ public void testGetDeclaredMethod() {
+ try {
+ Method m = new Object[0][0].getClass().getDeclaredMethod("clone", (Class>[])null);
+
+ failed++;
+ System.out.println("getDeclaredMethod(\"clone\", null) Should not find clone()");
+
+ } catch (NoSuchMethodException e) {
+ ; //all good
+ }
+ }
+
+ public void testGetDeclaredMethods() {
+ Method[] m = new Throwable[0][0][0][0].getClass().getDeclaredMethods();
+ if (m.length != 0) {
+ failed++;
+ System.out.println("getDeclaredMethods().length should be 0");
+ }
+ }
+
+ public void testGetInterfaces() {
+ Class>[] is = new Integer[0].getClass().getInterfaces();
+ boolean thisFailed = false;
+
+ if (is.length != 2)
+ thisFailed = true;
+
+ if (!is[0].getCanonicalName().equals("java.lang.Cloneable"))
+ thisFailed = true;
+
+ if (!is[1].getCanonicalName().equals("java.io.Serializable"))
+ thisFailed = true;
+
+ if (thisFailed) {
+ failed++;
+ System.out.println(Arrays.asList(is));
+ System.out.println("Should contain exactly Cloneable, Serializable in that order.");
+ }
+ }
+}