From 98781dc64fd6c1bddeb26f30ca652acecd16566d Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Wed, 7 May 2008 21:06:48 +0000 Subject: [PATCH] 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 --- .../commons/lang/reflect/FieldUtils.java | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/java/org/apache/commons/lang/reflect/FieldUtils.java b/src/java/org/apache/commons/lang/reflect/FieldUtils.java index b0c76afb1..7480bdff0 100644 --- a/src/java/org/apache/commons/lang/reflect/FieldUtils.java +++ b/src/java/org/apache/commons/lang/reflect/FieldUtils.java @@ -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,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 // 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 - try { - Field test = ints[i].getField(fieldName); - if (match != null) { - if (match.getDeclaringClass().equals(test.getDeclaringClass())) { - continue; - } - throw new IllegalArgumentException( - "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 + for (Iterator intf = ClassUtils.getAllInterfaces(cls).iterator(); intf + .hasNext();) { + try { + Field test = ((Class) intf.next()).getField(fieldName); + if (match != null) { + throw new IllegalArgumentException( + "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 } } return match;