From: Jim Procter Date: Mon, 2 Feb 2015 17:40:41 +0000 (+0000) Subject: JAL-1640 copy constructor test and implementation by reflection X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=e675b64bdd12a521912d5e6f020e6d8ac9a39add;p=jalview.git JAL-1640 copy constructor test and implementation by reflection --- diff --git a/src/jalview/viewmodel/styles/ViewStyle.java b/src/jalview/viewmodel/styles/ViewStyle.java index c7430ec..ff19573 100644 --- a/src/jalview/viewmodel/styles/ViewStyle.java +++ b/src/jalview/viewmodel/styles/ViewStyle.java @@ -3,6 +3,8 @@ package jalview.viewmodel.styles; import jalview.api.ViewStyleI; import java.awt.Color; +import java.lang.reflect.Method; +import java.util.HashMap; /** * A container for holding alignment view properties. View properties are @@ -120,6 +122,63 @@ public class ViewStyle implements ViewStyleI */ boolean upperCasebold = false; + public ViewStyle(ViewStyleI viewStyle) + { + configureFrom(viewStyle); + } + + public ViewStyle() + { + } + + private void configureFrom(ViewStyleI viewStyle) + { + HashMap getters = new HashMap(), isErs = new HashMap(); + HashMap setters = new HashMap(); + // Match Getters and Setters + for (Method m : ViewStyleI.class.getMethods()) + { + if (m.getDeclaringClass() == ViewStyleI.class) + { + if (m.getName().startsWith("get")) + { + getters.put(m.getName().substring(3), m); + } + if (m.getName().startsWith("is")) + { + isErs.put(m.getName().substring(2), m); + } + if (m.getName().startsWith("set")) + { + setters.put(m.getName().substring(3), m); + } + } + } + // try and do the set thing + for (String prop : setters.keySet()) + { + Method getter = getters.get(prop); + Method setter = setters.get(prop); + if (getter == null) + { + getter = isErs.get(prop); + } + if (getter != null && setter != null) + { + try + { + setter.invoke(this, getter.invoke(viewStyle)); + } catch (Exception q) + { + System.err.println("Unexpected exception setting view property " + + prop + " by reflection"); + q.printStackTrace(); + } + + } + } + } + /** * @return the upperCasebold */ diff --git a/test/jalview/viewmodel/styles/TestviewStyle.java b/test/jalview/viewmodel/styles/TestviewStyle.java new file mode 100644 index 0000000..88ea82e --- /dev/null +++ b/test/jalview/viewmodel/styles/TestviewStyle.java @@ -0,0 +1,23 @@ +package jalview.viewmodel.styles; + +import org.junit.Assert; +import org.junit.Test; + +public class TestviewStyle +{ + + @Test + public void testSetterGetter() + { + ViewStyle orig = new ViewStyle(); + orig.setCharHeight(23); + orig.setWrappedWidth(253); + orig.setWrapAlignment(true); + ViewStyle copy = new ViewStyle(orig); + orig.setWrapAlignment(false); + Assert.assertEquals(copy.getCharHeight(), orig.getCharHeight()); + Assert.assertEquals(copy.getWrappedWidth(), orig.getWrappedWidth()); + Assert.assertEquals(copy.getWrapAlignment(), !orig.getWrapAlignment()); + } + +}