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
This commit is contained in:
parent
ab3ec830b3
commit
eab09cdf1a
4 changed files with 89 additions and 62 deletions
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue