親クラスを全て参照する方法を応用すれば、
以前書いた、yipuran-core で FieldUtil なるものを作り、
yipuran-core/FieldUtil.java at master · yipuran/yipuran-core · GitHub
public static <R, T> R copy(T t, Supplier<R> s)
public static <R, T> R copylenient(T t, Supplier<R> s)
も、以下のように書けるはずだ。
public static<T,U> U copylenient(T t, U u){ UnaryOperator<Class<?>> superFind = c->c.getSuperclass(); UnaryOperator<String> topUpper = s->s.substring(0, 1).toUpperCase() + s.substring(1); Class<?> c = t.getClass(); try{ do{ for(Field f : c.getDeclaredFields()){ String n = f.getName(); String name = topUpper.apply(n); Method getter = c.getDeclaredMethod( (c.getDeclaredField(n).getType().equals(boolean.class) ? "is" : "get") + name); try{ Method setter = u.getClass().getDeclaredMethod( "set"+ name, getter.getReturnType()); setter.invoke(u, getter.invoke(t)); }catch(NoSuchMethodException e){ } } }while(!(c=superFind.apply(c)).equals(Object.class)); }catch(SecurityException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e){ throw new RuntimeException(e); } return u; }