use existing API to get all interfaces

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@654273 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2008-05-07 21:06:48 +00:00
parent 30e77453fc
commit 98781dc64f
1 changed files with 17 additions and 20 deletions

View File

@ -18,6 +18,9 @@
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Iterator;
import org.apache.commons.lang.ClassUtils;
/** /**
* Utilities for working with fields by reflection. Adapted and refactored * Utilities for working with fields by reflection. Adapted and refactored
@ -113,27 +116,21 @@ public static Field getField(final Class cls, String fieldName, boolean forceAcc
// incase there is a public supersuperclass field hidden by a private/package // incase there is a public supersuperclass field hidden by a private/package
// superclass field. // superclass field.
Field match = null; Field match = null;
for (Class acls = cls; acls != null; acls = acls.getSuperclass()) { for (Iterator intf = ClassUtils.getAllInterfaces(cls).iterator(); intf
Class[] ints = acls.getInterfaces(); .hasNext();) {
for (int i = 0; i < ints.length; i++) { try {
// getField is fine here, because everything is public, and thus it works Field test = ((Class) intf.next()).getField(fieldName);
try { if (match != null) {
Field test = ints[i].getField(fieldName); throw new IllegalArgumentException(
if (match != null) { "Reference to field "
if (match.getDeclaringClass().equals(test.getDeclaringClass())) { + fieldName
continue; + " is ambiguous relative to "
} + cls
throw new IllegalArgumentException( + "; a matching field exists on two or more parent interfaces.");
"Reference to field "
+ fieldName
+ " is ambiguous relative to "
+ cls
+ "; a matching field exists on two or more parent interfaces.");
}
match = test;
} catch (NoSuchFieldException ex) {
// ignore
} }
match = test;
} catch (NoSuchFieldException ex) {
// ignore
} }
} }
return match; return match;