diff options
author | Tom Tromey <tromey@redhat.com> | 2003-10-22 19:29:27 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2003-10-22 19:29:27 +0000 |
commit | eab09cdf1a45d70f86d48138d81c8541bae9b2d4 (patch) | |
tree | 000f378fa5f1a46ba806b0c30692c3bb5de11c5d /libjava/java/lang/Class.java | |
parent | ab3ec830b3a476677a07494e17abb645ebdad6b3 (diff) | |
download | gcc-eab09cdf1a45d70f86d48138d81c8541bae9b2d4.zip gcc-eab09cdf1a45d70f86d48138d81c8541bae9b2d4.tar.gz gcc-eab09cdf1a45d70f86d48138d81c8541bae9b2d4.tar.bz2 |
re PR libgcj/12416 (java.lang.Class.getFields returns duplicate entries.)
PR libgcj/12416:
* java/lang/Class.h: Updated.
* java/lang/natClass.cc (_getFields): Removed.
(getFields): Likewise.
(getDeclaredFields): Added `public_only' parameter.
* java/lang/Class.java (getFields): Now implemented in java; from
Classpath.
(getDeclaredFields): Likewise.
(getDeclaredFields(boolean)): Declare.
(_getFields): Removed.
(internalGetFields): New method, from Classpath.
From-SVN: r72818
Diffstat (limited to 'libjava/java/lang/Class.java')
-rw-r--r-- | libjava/java/lang/Class.java | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/libjava/java/lang/Class.java b/libjava/java/lang/Class.java index 6fd4ff0..8943da4 100644 --- a/libjava/java/lang/Class.java +++ b/libjava/java/lang/Class.java @@ -13,6 +13,8 @@ import java.io.Serializable; import java.io.InputStream; import java.lang.reflect.*; import java.security.*; +import java.util.Arrays; +import java.util.HashSet; /** * @author Tom Tromey <tromey@cygnus.com> @@ -64,7 +66,26 @@ public final class Class implements Serializable public native Field getDeclaredField (String fieldName) throws NoSuchFieldException, SecurityException; - public native Field[] getDeclaredFields () throws SecurityException; + + /** + * Get all the declared fields in this class, but not those inherited from + * superclasses. This returns an array of length 0 if there are no fields, + * including for primitive types. This does not return the implicit length + * field of arrays. A security check may be performed, with + * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as + * <code>checkPackageAccess</code> both having to succeed. + * + * @return all declared fields in this class + * @throws SecurityException if the security check fails + * @since 1.1 + */ + public Field[] getDeclaredFields() + { + memberAccessCheck(Member.DECLARED); + return getDeclaredFields(false); + } + + native Field[] getDeclaredFields (boolean publicOnly); private native Method _getDeclaredMethod (String methodName, Class[] parameterTypes); @@ -101,8 +122,39 @@ public final class Class implements Serializable return fld; } - private native Field[] _getFields (Field[] result, int offset); - public native Field[] getFields () throws SecurityException; + /** + * Get all the public fields declared in this class or inherited from + * superclasses. This returns an array of length 0 if there are no fields, + * including for primitive types. This does not return the implicit length + * field of arrays. A security check may be performed, with + * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as + * <code>checkPackageAccess</code> both having to succeed. + * + * @return all public fields in this class + * @throws SecurityException if the security check fails + * @since 1.1 + */ + public Field[] getFields() + { + memberAccessCheck(Member.PUBLIC); + return internalGetFields(); + } + + /** + * Like <code>getFields()</code> but without the security checks. + */ + private Field[] internalGetFields() + { + HashSet set = new HashSet(); + set.addAll(Arrays.asList(getDeclaredFields(true))); + Class[] interfaces = getInterfaces(); + for (int i = 0; i < interfaces.length; i++) + set.addAll(Arrays.asList(interfaces[i].internalGetFields())); + Class superClass = getSuperclass(); + if (superClass != null) + set.addAll(Arrays.asList(superClass.internalGetFields())); + return (Field[])set.toArray(new Field[set.size()]); + } /** * Returns the <code>Package</code> in which this class is defined |