/** * Copy from the copy method in StructUtil. Did not want to drag that code in. maybe this actually * should go to struct. * * @param from * @param to * @param excludes * @return * @throws Exception */ public static <T extends struct> T xcopy(struct from, T to, String... excludes) throws Exception { Arrays.sort(excludes); for (Field f : from.fields()) { if (Arrays.binarySearch(excludes, f.getName()) >= 0) continue; Object o = f.get(from); if (o == null) continue; Field tof = to.getField(f.getName()); if (tof != null) try { tof.set(to, Converter.cnv(tof.getGenericType(), o)); } catch (Exception e) { System.out.println( "Failed to convert " + f.getName() + " from " + from.getClass() + " to " + to.getClass() + " value " + o + " exception " + e); } } return to; }
/** * Sets the field represented by this {@code field} object on the specified object argument {@code * target} to the specified new value {@code rsrc}. * * @param field Field where resource should be injected. * @param target Target object. * @param rsrc Resource object which should be injected in target object field. * @throws GridException Thrown if unable to inject resource. */ @SuppressWarnings({"ErrorNotRethrown"}) static void inject(Field field, Object target, Object rsrc) throws GridException { if (rsrc != null && !field.getType().isAssignableFrom(rsrc.getClass())) throw new GridException( "Resource field is not assignable from the resource: " + rsrc.getClass()); try { // Override default Java access check. field.setAccessible(true); field.set(target, rsrc); } catch (SecurityException | ExceptionInInitializerError | IllegalAccessException e) { throw new GridException( "Failed to inject resource [field=" + field.getName() + ", target=" + target + ", rsrc=" + rsrc + ']', e); } }