various bugfixes and tweaks in reflection

This commit is contained in:
Joel Dice 2007-11-20 13:40:07 -07:00
parent ded1016b32
commit 6fe0c4636f
3 changed files with 17 additions and 11 deletions

View File

@ -105,7 +105,7 @@ public final class Class <T> {
public Class getComponentType() { public Class getComponentType() {
if (isArray()) { if (isArray()) {
return (Class) (Object) staticTable; return (Class) staticTable;
} else { } else {
return null; return null;
} }
@ -279,9 +279,10 @@ public final class Class <T> {
public Field[] getFields() { public Field[] getFields() {
Field[] array = new Field[countPublicFields()]; Field[] array = new Field[countPublicFields()];
if (fieldTable != null) { if (fieldTable != null) {
int ai = 0;
for (int i = 0; i < fieldTable.length; ++i) { for (int i = 0; i < fieldTable.length; ++i) {
if (((fieldTable[i].getModifiers() & Modifier.PUBLIC)) != 0) { if (((fieldTable[i].getModifiers() & Modifier.PUBLIC)) != 0) {
array[i] = fieldTable[i]; array[ai++] = fieldTable[i];
} }
} }
} }
@ -306,10 +307,10 @@ public final class Class <T> {
public Method[] getDeclaredMethods() { public Method[] getDeclaredMethods() {
Method[] array = new Method[countMethods(false)]; Method[] array = new Method[countMethods(false)];
if (methodTable != null) { if (methodTable != null) {
int index = 0; int ai = 0;
for (int i = 0; i < methodTable.length; ++i) { for (int i = 0; i < methodTable.length; ++i) {
if (! methodTable[i].getName().startsWith("<")) { if (! methodTable[i].getName().startsWith("<")) {
array[index++] = methodTable[i]; array[ai++] = methodTable[i];
} }
} }
} }
@ -346,7 +347,7 @@ public final class Class <T> {
} }
public T[] getEnumConstants() { public T[] getEnumConstants() {
if (isAssignableFrom(Enum.class)) { if (Enum.class.isAssignableFrom(this)) {
try { try {
return (T[]) getMethod("values").invoke(null); return (T[]) getMethod("values").invoke(null);
} catch (Exception e) { } catch (Exception e) {
@ -374,7 +375,7 @@ public final class Class <T> {
} }
public boolean isInstance(Object o) { public boolean isInstance(Object o) {
return isAssignableFrom(o.getClass()); return o != null && isAssignableFrom(o.getClass());
} }
public boolean isPrimitive() { public boolean isPrimitive() {

View File

@ -68,10 +68,14 @@ public final class Array {
break; break;
case 'L': case 'L':
case '[': case '[':
if (array.getClass().getComponentType().isInstance(value)) { if (value == null
|| array.getClass().getComponentType().isInstance(value))
{
((Object[]) array)[index] = value; ((Object[]) array)[index] = value;
} else { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException
("need " + array.getClass().getComponentType() +
", got " + value.getClass().getName());
} }
break; break;

View File

@ -43,7 +43,7 @@ public class Field<T> extends AccessibleObject {
} }
public Class getType() { public Class getType() {
return Class.forCanonicalName(getName()); return Class.forCanonicalName(new String(spec, 0, spec.length - 1, false));
} }
public Object get(Object instance) throws IllegalAccessException { public Object get(Object instance) throws IllegalAccessException {
@ -139,10 +139,11 @@ public class Field<T> extends AccessibleObject {
break; break;
case ObjectField: case ObjectField:
if (getType().isInstance(value)) { if (value == null || getType().isInstance(value)) {
setObject(target, offset, value); setObject(target, offset, value);
} else { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException
("need " + getType() + ", got " + value.getClass().getName());
} }
break; break;