@Test public void basicTest() throws Exception { String srcRoot = "/foo"; String dataRoot = "/bar"; String bugPage = "https: Configuration cfgBase = new Configuration(); cfgBase.setSourceRoot(srcRoot); cfgBase.setDataRoot(dataRoot); Configuration cfgNew = new Configuration(); cfgNew.setBugPage(bugPage); merge(cfgBase, cfgNew); assertEquals(cfgNew.getSourceRoot(), srcRoot); assertEquals(cfgNew.getDataRoot(), dataRoot); assertEquals(cfgNew.getBugPage(), bugPage); }
public static void merge(Configuration cfgBase, Configuration cfgNew) throws Exception { Configuration cfgDefault = new Configuration(); for (Field field : cfgBase.getClass().getDeclaredFields()) { String fieldName = field.getName(); int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers)) { continue; } PropertyDescriptor desc = null; try { desc = new PropertyDescriptor(fieldName, Configuration.class); } catch (IntrospectionException ex) { throw new Exception("cannot get property descriptor for '" + fieldName + "'"); } Method setter = desc.getWriteMethod(); if (setter == null) { throw new Exception("no setter for '" + fieldName + "'"); } Method getter = desc.getReadMethod(); if (getter == null) { throw new Exception("no getter for '" + fieldName + "'"); } try { Object obj = getter.invoke(cfgBase); if ((obj == null && getter.invoke(cfgDefault) == null) || obj.equals(getter.invoke(cfgDefault))) { continue; } } catch (Exception ex) { throw new Exception("failed to invoke getter for " + fieldName + ": " + ex); } try { setter.invoke(cfgNew, getter.invoke(cfgBase)); } catch (Exception ex) { throw new Exception("failed to invoke setter for '" + fieldName + "'"); } } }
ConfigMerge { public static void merge(Configuration cfgBase, Configuration cfgNew) throws Exception { Configuration cfgDefault = new Configuration(); for (Field field : cfgBase.getClass().getDeclaredFields()) { String fieldName = field.getName(); int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers)) { continue; } PropertyDescriptor desc = null; try { desc = new PropertyDescriptor(fieldName, Configuration.class); } catch (IntrospectionException ex) { throw new Exception("cannot get property descriptor for '" + fieldName + "'"); } Method setter = desc.getWriteMethod(); if (setter == null) { throw new Exception("no setter for '" + fieldName + "'"); } Method getter = desc.getReadMethod(); if (getter == null) { throw new Exception("no getter for '" + fieldName + "'"); } try { Object obj = getter.invoke(cfgBase); if ((obj == null && getter.invoke(cfgDefault) == null) || obj.equals(getter.invoke(cfgDefault))) { continue; } } catch (Exception ex) { throw new Exception("failed to invoke getter for " + fieldName + ": " + ex); } try { setter.invoke(cfgNew, getter.invoke(cfgBase)); } catch (Exception ex) { throw new Exception("failed to invoke setter for '" + fieldName + "'"); } } } }
ConfigMerge { public static void merge(Configuration cfgBase, Configuration cfgNew) throws Exception { Configuration cfgDefault = new Configuration(); for (Field field : cfgBase.getClass().getDeclaredFields()) { String fieldName = field.getName(); int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers)) { continue; } PropertyDescriptor desc = null; try { desc = new PropertyDescriptor(fieldName, Configuration.class); } catch (IntrospectionException ex) { throw new Exception("cannot get property descriptor for '" + fieldName + "'"); } Method setter = desc.getWriteMethod(); if (setter == null) { throw new Exception("no setter for '" + fieldName + "'"); } Method getter = desc.getReadMethod(); if (getter == null) { throw new Exception("no getter for '" + fieldName + "'"); } try { Object obj = getter.invoke(cfgBase); if ((obj == null && getter.invoke(cfgDefault) == null) || obj.equals(getter.invoke(cfgDefault))) { continue; } } catch (Exception ex) { throw new Exception("failed to invoke getter for " + fieldName + ": " + ex); } try { setter.invoke(cfgNew, getter.invoke(cfgBase)); } catch (Exception ex) { throw new Exception("failed to invoke setter for '" + fieldName + "'"); } } } private  ConfigMerge(); }
ConfigMerge { public static void merge(Configuration cfgBase, Configuration cfgNew) throws Exception { Configuration cfgDefault = new Configuration(); for (Field field : cfgBase.getClass().getDeclaredFields()) { String fieldName = field.getName(); int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers)) { continue; } PropertyDescriptor desc = null; try { desc = new PropertyDescriptor(fieldName, Configuration.class); } catch (IntrospectionException ex) { throw new Exception("cannot get property descriptor for '" + fieldName + "'"); } Method setter = desc.getWriteMethod(); if (setter == null) { throw new Exception("no setter for '" + fieldName + "'"); } Method getter = desc.getReadMethod(); if (getter == null) { throw new Exception("no getter for '" + fieldName + "'"); } try { Object obj = getter.invoke(cfgBase); if ((obj == null && getter.invoke(cfgDefault) == null) || obj.equals(getter.invoke(cfgDefault))) { continue; } } catch (Exception ex) { throw new Exception("failed to invoke getter for " + fieldName + ": " + ex); } try { setter.invoke(cfgNew, getter.invoke(cfgBase)); } catch (Exception ex) { throw new Exception("failed to invoke setter for '" + fieldName + "'"); } } } private  ConfigMerge(); static void merge(Configuration cfgBase, Configuration cfgNew); static void main(String[] argv); }
ConfigMerge { public static void merge(Configuration cfgBase, Configuration cfgNew) throws Exception { Configuration cfgDefault = new Configuration(); for (Field field : cfgBase.getClass().getDeclaredFields()) { String fieldName = field.getName(); int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers)) { continue; } PropertyDescriptor desc = null; try { desc = new PropertyDescriptor(fieldName, Configuration.class); } catch (IntrospectionException ex) { throw new Exception("cannot get property descriptor for '" + fieldName + "'"); } Method setter = desc.getWriteMethod(); if (setter == null) { throw new Exception("no setter for '" + fieldName + "'"); } Method getter = desc.getReadMethod(); if (getter == null) { throw new Exception("no getter for '" + fieldName + "'"); } try { Object obj = getter.invoke(cfgBase); if ((obj == null && getter.invoke(cfgDefault) == null) || obj.equals(getter.invoke(cfgDefault))) { continue; } } catch (Exception ex) { throw new Exception("failed to invoke getter for " + fieldName + ": " + ex); } try { setter.invoke(cfgNew, getter.invoke(cfgBase)); } catch (Exception ex) { throw new Exception("failed to invoke setter for '" + fieldName + "'"); } } } private  ConfigMerge(); static void merge(Configuration cfgBase, Configuration cfgNew); static void main(String[] argv);  }