/** * 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; }
/** * Returns the generic type information of an attribute. * * @param field the field representation of the attribute. * @return an array of types that are used to parameterize the attribute. */ public static Class<?>[] getGenericTypes(Field field) { Type genericFieldType = field.getGenericType(); if (!(genericFieldType instanceof ParameterizedType)) return null; // type is not generic ParameterizedType pType = (ParameterizedType) genericFieldType; Type[] args = pType.getActualTypeArguments(); Class<?>[] types = new Class[args.length]; System.arraycopy(args, 0, types, 0, args.length); return types; }