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 | |
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')
-rw-r--r-- | libjava/ChangeLog | 12 | ||||
-rw-r--r-- | libjava/java/lang/Class.h | 5 | ||||
-rw-r--r-- | libjava/java/lang/Class.java | 58 | ||||
-rw-r--r-- | libjava/java/lang/natClass.cc | 76 |
4 files changed, 89 insertions, 62 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog index a6e1d94..6c5c4d1 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,5 +1,17 @@ 2003-10-22 Tom Tromey <tromey@redhat.com> + 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. + PR libgcj/12656: * gnu/gcj/runtime/natFirstThread.cc (call_main): Use _Jv_LookupDeclaredMethod, not _Jv_GetMethodLocal. diff --git a/libjava/java/lang/Class.h b/libjava/java/lang/Class.h index 5cf6e2c..cdfdd7d 100644 --- a/libjava/java/lang/Class.h +++ b/libjava/java/lang/Class.h @@ -157,7 +157,8 @@ public: java::lang::reflect::Constructor *getDeclaredConstructor (JArray<jclass> *); JArray<java::lang::reflect::Constructor *> *getDeclaredConstructors (void); java::lang::reflect::Field *getDeclaredField (jstring); - JArray<java::lang::reflect::Field *> *getDeclaredFields (void); + JArray<java::lang::reflect::Field *> *getDeclaredFields (); + JArray<java::lang::reflect::Field *> *getDeclaredFields (jboolean); java::lang::reflect::Method *getDeclaredMethod (jstring, JArray<jclass> *); JArray<java::lang::reflect::Method *> *getDeclaredMethods (void); @@ -166,7 +167,7 @@ public: java::lang::reflect::Field *getField (jstring); private: - jint _getFields (JArray<java::lang::reflect::Field *> *result, jint offset); + JArray<java::lang::reflect::Field *> internalGetFields (); JArray<java::lang::reflect::Constructor *> *_getConstructors (jboolean); java::lang::reflect::Field *getField (jstring, jint); jint _getMethods (JArray<java::lang::reflect::Method *> *result, 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 diff --git a/libjava/java/lang/natClass.cc b/libjava/java/lang/natClass.cc index 4c71ed1..c9b7014 100644 --- a/libjava/java/lang/natClass.cc +++ b/libjava/java/lang/natClass.cc @@ -295,16 +295,32 @@ java::lang::Class::getDeclaredField (jstring name) } JArray<java::lang::reflect::Field *> * -java::lang::Class::getDeclaredFields (void) +java::lang::Class::getDeclaredFields (jboolean public_only) { - memberAccessCheck(java::lang::reflect::Member::DECLARED); + int size; + if (public_only) + { + size = 0; + for (int i = 0; i < field_count; ++i) + { + _Jv_Field *field = &fields[i]; + if ((field->flags & java::lang::reflect::Modifier::PUBLIC)) + ++size; + } + } + else + size = field_count; + JArray<java::lang::reflect::Field *> *result = (JArray<java::lang::reflect::Field *> *) - JvNewObjectArray (field_count, &java::lang::reflect::Field::class$, NULL); + JvNewObjectArray (size, &java::lang::reflect::Field::class$, NULL); java::lang::reflect::Field** fptr = elements (result); for (int i = 0; i < field_count; i++) { _Jv_Field *field = &fields[i]; + if (public_only + && ! (field->flags & java::lang::reflect::Modifier::PUBLIC)) + continue; java::lang::reflect::Field* rfield = new java::lang::reflect::Field (); rfield->offset = (char*) field - (char*) fields; rfield->declaringClass = this; @@ -461,60 +477,6 @@ java::lang::Class::getDeclaringClass (void) return NULL; } -jint -java::lang::Class::_getFields (JArray<java::lang::reflect::Field *> *result, - jint offset) -{ - int count = 0; - for (int i = 0; i < field_count; i++) - { - _Jv_Field *field = &fields[i]; - if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC)) - continue; - ++count; - - if (result != NULL) - { - java::lang::reflect::Field *rfield - = new java::lang::reflect::Field (); - rfield->offset = (char *) field - (char *) fields; - rfield->declaringClass = this; - rfield->name = _Jv_NewStringUtf8Const (field->name); - (elements (result))[offset++] = rfield; - } - } - jclass superclass = getSuperclass(); - if (superclass != NULL) - { - int s_count = superclass->_getFields (result, offset); - count += s_count; - offset += s_count; - } - for (int i = 0; i < interface_count; ++i) - { - int f_count = interfaces[i]->_getFields (result, offset); - count += f_count; - offset += f_count; - } - return count; -} - -JArray<java::lang::reflect::Field *> * -java::lang::Class::getFields (void) -{ - memberAccessCheck(java::lang::reflect::Member::PUBLIC); - - int count = _getFields (NULL, 0); - - JArray<java::lang::reflect::Field *> *result - = ((JArray<java::lang::reflect::Field *> *) - JvNewObjectArray (count, &java::lang::reflect::Field::class$, NULL)); - - _getFields (result, 0); - - return result; -} - JArray<jclass> * java::lang::Class::getInterfaces (void) { |