EntityUtils { public static void copyPropertiesExclude(Object from, Object to, String[] excludeArray) throws Exception { List<String> excludesList = null; if (excludeArray != null && excludeArray.length > 0) { excludesList = Arrays.asList(excludeArray); } Class fromClass = from.getClass(); Class toClass = to.getClass(); Field[] fromFields = fromClass.getDeclaredFields(); Field[] toFields = toClass.getDeclaredFields(); for (Field fromField : fromFields) { fromField.setAccessible(true); String fromFieldName = fromField.getName(); Field toField; if ((excludesList != null && excludesList.contains(fromFieldName)) || (toField = findFieldByName(toFields, fromFieldName)) == null) { continue; } toField.setAccessible(true); toField.set(to, fromField.get(from)); } }  static void copyProperties(Object from, Object to); static void copyPropertiesExclude(Object from, Object to, String[] excludeArray); @SuppressWarnings("unchecked") static void copyPropertiesInclude(Object from, Object to, String[] includeArray);  }
@Test public void copyPropertiesExclude() throws Exception { FromClassGetter fromClass = new FromClassGetter(); fromClass.setStringValue("[FromClass] = Save"); InnerClass innerClass = new InnerClass(); innerClass.setStringValue("[InnerClass] = Save"); fromClass.setInnerClass(innerClass); ToClassSetter toClass = new ToClassSetter(); EntityUtils.copyPropertiesExclude(fromClass, toClass, new String[] { "stringValue" }); assertNull(toClass.getStringValue()); assertEquals(fromClass.getInnerClass(), toClass.getInnerClass()); }