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.Modifier;
import java.util.Iterator;
import org.apache.commons.lang.ClassUtils;
/**
* Utilities for working with fields by reflection. Adapted and refactored
@ -113,16 +116,11 @@ public static Field getField(final Class cls, String fieldName, boolean forceAcc
// incase there is a public supersuperclass field hidden by a private/package
// superclass field.
Field match = null;
for (Class acls = cls; acls != null; acls = acls.getSuperclass()) {
Class[] ints = acls.getInterfaces();
for (int i = 0; i < ints.length; i++) {
// getField is fine here, because everything is public, and thus it works
for (Iterator intf = ClassUtils.getAllInterfaces(cls).iterator(); intf
.hasNext();) {
try {
Field test = ints[i].getField(fieldName);
Field test = ((Class) intf.next()).getField(fieldName);
if (match != null) {
if (match.getDeclaringClass().equals(test.getDeclaringClass())) {
continue;
}
throw new IllegalArgumentException(
"Reference to field "
+ fieldName
@ -135,7 +133,6 @@ public static Field getField(final Class cls, String fieldName, boolean forceAcc
// ignore
}
}
}
return match;
}