提交 97d0c074 编写于 作者: J jfranck

5047859: (reflect) Class.getField can't find String[].length

Reviewed-by: darcy, mchung
上级 b87045da
......@@ -1484,22 +1484,24 @@ public final class Class<T> implements java.io.Serializable,
/**
* Returns an array containing {@code Field} objects reflecting all
* the accessible public fields of the class or interface represented by
* this {@code Class} object. 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 has no accessible public
* fields, or if it represents an array class, a primitive type, or void.
* this {@code Class} object.
*
* <p> Specifically, if this {@code Class} object represents a class,
* this method returns the public fields of this class and of all its
* superclasses. If this {@code Class} object represents an
* interface, this method returns the fields of this interface and of all
* its superinterfaces.
* <p> If this {@code Class} object represents a class or interface with no
* no accessible public fields, then this method returns an array of length
* 0.
*
* <p> If this {@code Class} object represents a class, then this method
* returns the public fields of the class and of all its superclasses.
*
* <p> If this {@code Class} object represents an interface, then this
* method returns the fields of the interface and of all its
* superinterfaces.
*
* <p> The implicit length field for array class is not reflected by this
* method. User code should use the methods of class {@code Array} to
* manipulate arrays.
* <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> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
* <p> The elements in the array returned are not sorted and are not in any
* particular order.
*
* @return the array of {@code Field} objects representing the
* public fields
......@@ -1512,6 +1514,8 @@ public final class Class<T> implements java.io.Serializable,
* of this class.
*
* @since JDK1.1
* @jls 8.2 Class Members
* @jls 8.3 Field Declarations
*/
@CallerSensitive
public Field[] getFields() throws SecurityException {
......@@ -1595,13 +1599,14 @@ public final class Class<T> implements java.io.Serializable,
/**
* Returns a {@code Field} object that reflects the specified public
* member field of the class or interface represented by this
* {@code Class} object. The {@code name} parameter is a
* {@code String} specifying the simple name of the desired field.
* Returns a {@code Field} object that reflects the specified public member
* field of the class or interface represented by this {@code Class}
* object. The {@code name} parameter is a {@code String} specifying the
* simple name of the desired field.
*
* <p> The field to be reflected is determined by the algorithm that
* follows. Let C be the class represented by this object:
* follows. Let C be the class or interface represented by this object:
*
* <OL>
* <LI> If C declares a public field with the name specified, that is the
* field to be reflected.</LI>
......@@ -1614,7 +1619,8 @@ public final class Class<T> implements java.io.Serializable,
* is thrown.</LI>
* </OL>
*
* <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
* <p> If this {@code Class} object represents an array type, then this
* method does not find the {@code length} field of the array type.
*
* @param name the field name
* @return the {@code Field} object of this class specified by
......@@ -1631,6 +1637,8 @@ public final class Class<T> implements java.io.Serializable,
* of this class.
*
* @since JDK1.1
* @jls 8.2 Class Members
* @jls 8.3 Field Declarations
*/
@CallerSensitive
public Field getField(String name)
......@@ -1800,12 +1808,15 @@ public final class Class<T> implements java.io.Serializable,
* declared by the class or interface represented by this
* {@code Class} object. This includes public, protected, default
* (package) access, and private fields, but excludes inherited fields.
* 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 fields, or if this {@code Class} object
* represents a primitive type, an array class, or void.
*
* <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
* <p> If this {@code Class} object represents a class or interface with no
* declared fields, then this method returns an array of length 0.
*
* <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
* particular order.
*
* @return the array of {@code Field} objects representing all the
* declared fields of this class
......@@ -1831,6 +1842,8 @@ public final class Class<T> implements java.io.Serializable,
* </ul>
*
* @since JDK1.1
* @jls 8.2 Class Members
* @jls 8.3 Field Declarations
*/
@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException {
......@@ -1935,9 +1948,11 @@ public final class Class<T> implements java.io.Serializable,
/**
* Returns a {@code Field} object that reflects the specified declared
* field of the class or interface represented by this {@code Class}
* object. The {@code name} parameter is a {@code String} that
* specifies the simple name of the desired field. Note that this method
* will not reflect the {@code length} field of an array class.
* object. The {@code name} parameter is a {@code String} that specifies
* the simple name of the desired field.
*
* <p> If this {@code Class} object represents an array type, then this
* method does not find the {@code length} field of the array type.
*
* @param name the name of the field
* @return the {@code Field} object for the specified field in this
......@@ -1967,6 +1982,8 @@ public final class Class<T> implements java.io.Serializable,
* </ul>
*
* @since JDK1.1
* @jls 8.2 Class Members
* @jls 8.3 Field Declarations
*/
@CallerSensitive
public Field getDeclaredField(String name)
......
/*
* 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 5047859
* @summary verify that for an array type class instance, getField("length")
* throws an exception, and getFields() does not contain a Field for
* 'length'
*/
import java.lang.reflect.Field;
public class ArrayLength {
public static void main(String [] args) {
int failed = 0;
try {
new String[0].getClass().getField("length");
failed++;
System.out.println("getField(\"length\") should throw NoSuchFieldException");
} catch (NoSuchFieldException e) {
}
try {
new String[0].getClass().getDeclaredField("length");
failed++;
System.out.println("getDeclaredField(\"length\") should throw NoSuchFieldException");
} catch (NoSuchFieldException e) {
}
if (new String[0].getClass().getFields().length != 0) {
failed++;
System.out.println("getFields() for an array type should return a zero length array");
}
if (new String[0].getClass().getDeclaredFields().length != 0) {
failed++;
System.out.println("getDeclaredFields() for an array type should return a zero length array");
}
if (failed != 0)
throw new RuntimeException("Test failed see log for details");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册