throw new IllegalArgumentException(String.format("%s does not have a copy constructor", clazz), e);
}
- final Field f;
- try {
- f = clazz.getDeclaredField("_value");
- } catch (NoSuchFieldException e) {
- throw new IllegalArgumentException(String.format("%s does not have required internal field", clazz), e);
- }
+ final Field f = findValueField(clazz);
f.setAccessible(true);
final StringValueObjectFactory<T> ret;
return false;
}
+ private static Field findValueField(final Class<?> orig) {
+ NoSuchFieldException cause = null;
+ Class<?> clazz = orig;
+ do {
+ try {
+ return clazz.getDeclaredField("_value");
+ } catch (NoSuchFieldException e) {
+ if (cause != null) {
+ e.addSuppressed(cause);
+ }
+ cause = e;
+ }
+ clazz = clazz.getSuperclass();
+ } while (clazz != null);
+
+ throw new IllegalArgumentException(orig + " nor its superclasses define required internal field _value", cause);
+ }
+
@SuppressWarnings("checkstyle:illegalCatch")
public T newInstance(final String string) {
Preconditions.checkNotNull(string, "Argument may not be null");