4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
authorjfranck
Wed, 11 Sep 2013 09:45:52 +0200
changeset 19834 c43d94c72c41
parent 19833 e899d1502dc7
child 19835 505b6711e79b
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
jdk/src/share/classes/java/lang/Class.java
jdk/test/java/lang/Class/ArrayMethods.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 @@
      * <p> If this object represents a primitive type or void, the method
      * returns an array of length 0.
      *
+     * <p> 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 @@
      * <p> If this {@code Class} object represents an array type, a primitive
      * type, or void, then this method returns an array of length 0.
      *
-     * <p> The elements in the array returned are not sorted and are not in any
+     * <p> 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 <em>member</em> 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.
+     *
+     * <p> 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.
+     *
+     * <p> If this {@code Class} object represents a type with a class
+     * initialization method {@code <clinit>}, then the returned array does
+     * <em>not</em> have a corresponding {@code Method} object.
      *
-     * <p> The class initialization method {@code <clinit>} is not
-     * included in the returned array. If the class declares multiple public
-     * member methods with the same parameter types, they are all included in
-     * the returned array.
+     * <p> 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()}.
      *
-     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
+     * <p> If this {@code Class} object represents a class or interface with no
+     * public methods, then the returned array has length 0.
+     *
+     * <p> If this {@code Class} object represents a primitive type or void,
+     * then the returned array has length 0.
+     *
+     * <p> 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.
      *
-     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
+     * <p> 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 @@
      * <p> If this {@code Class} object represents an array type, a primitive
      * type, or void, then this method returns an array of length 0.
      *
-     * <p> The elements in the array returned are not sorted and are not in any
+     * <p> 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 <clinit>} is not included in the
-     * returned array. If the class declares multiple public member methods
-     * with the same parameter types, they are all included in the returned
-     * array.
+     *
+     * Returns an array containing {@code Method} objects reflecting all the
+     * declared methods of the class or interface represented by this {@code
+     * Class} object, including public, protected, default (package)
+     * access, and private methods, but excluding inherited methods.
+     *
+     * <p> 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.
      *
-     * <p> See <em>The Java Language Specification</em>, section 8.2.
+     * <p> If this {@code Class} object represents a type that has a class
+     * initialization method {@code <clinit>}, then the returned array does
+     * <em>not</em> have a corresponding {@code Method} object.
+     *
+     * <p> If this {@code Class} object represents a class or interface with no
+     * declared methods, then the returned array has length 0.
+     *
+     * <p> If this {@code Class} object represents an array type, a primitive
+     * type, or void, then the returned array has length 0.
+     *
+     * <p> 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 @@
      *
      *          </ul>
      *
+     * @jls 8.2 Class Members
+     * @jls 8.4 Method Declarations
      * @since JDK1.1
      */
     @CallerSensitive
@@ -2011,6 +2041,9 @@
      * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}
      * is raised.
      *
+     * <p> 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 @@
      *
      *          </ul>
      *
+     * @jls 8.2 Class Members
+     * @jls 8.4 Method Declarations
      * @since JDK1.1
      */
     @CallerSensitive
--- /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.");
+        }
+    }
+}