Ray Milkey
Committed by Gerrit Code Review

Refactor event dispatch injector to use TestUtils field setter

Change-Id: I793183de883c7b20784957eaa213d79a42951639
......@@ -15,6 +15,7 @@
*/
package org.onosproject.net;
import org.onlab.junit.TestUtils;
import org.onlab.packet.ChassisId;
import org.onlab.packet.IpAddress;
import org.onosproject.TestApplicationId;
......@@ -126,11 +127,11 @@ public final class NetTestTools {
for (Field f : mc.getSuperclass().getDeclaredFields()) {
if (f.getType().equals(EventDeliveryService.class)) {
try {
f.setAccessible(true);
f.set(manager, svc);
} catch (IllegalAccessException e) {
TestUtils.setField(manager, f.getName(), svc);
} catch (TestUtils.TestUtilsException e) {
throw new IllegalArgumentException("Unable to inject reference", e);
}
break;
}
}
}
......
......@@ -40,13 +40,23 @@ public final class TestUtils {
public static <T, U> void setField(T subject, String fieldName, U value)
throws TestUtilsException {
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) subject.getClass();
Class clazz = subject.getClass();
try {
while (clazz != null) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(subject, value);
} catch (NoSuchFieldException | SecurityException |
IllegalArgumentException | IllegalAccessException e) {
break;
} catch (NoSuchFieldException ex) {
if (clazz == clazz.getSuperclass()) {
break;
}
clazz = clazz.getSuperclass();
}
}
} catch (SecurityException | IllegalArgumentException |
IllegalAccessException e) {
throw new TestUtilsException("setField failed", e);
}
}
......