diff --git a/visural-common/src/com/visural/common/datastruct/BeanList.java b/visural-common/src/com/visural/common/datastruct/BeanList.java index 97e46ae..b30832a 100644 --- a/visural-common/src/com/visural/common/datastruct/BeanList.java +++ b/visural-common/src/com/visural/common/datastruct/BeanList.java @@ -99,6 +99,7 @@ public BeanList resort() { private BeanList sort(final String... properties) { Collections.sort(list, new Comparator() { + @Override public int compare(T o1, T o2) { try { for (int n = 0; n < properties.length; n++) { @@ -109,12 +110,16 @@ public int compare(T o1, T o2) { property = property.substring(1); } Class type = BeanUtil.getPropertyType(o1, property); - Method getter = BeanUtil.findGetter(o1.getClass(), property); - if (getter == null) { - throw new IllegalStateException("Property '"+property+"' can not be resolved."); + Method getter1 = BeanUtil.findGetter(o1.getClass(), property); + if (getter1 == null) { + throw new IllegalStateException(String.format("Property '%s' on class '%s' can not be resolved.", property, o1.getClass().getName())); } - Object val1 = getter.invoke(o1); - Object val2 = getter.invoke(o2); + Method getter2 = BeanUtil.findGetter(o2.getClass(), property); + if (getter2 == null) { + throw new IllegalStateException(String.format("Property '%s' on class '%s' can not be resolved.", property, o2.getClass().getName())); + } + Object val1 = getter1.invoke(o1); + Object val2 = getter2.invoke(o2); int result = 0; diff --git a/visural-common/test/com/visural/common/datastruct/BeanListTest.java b/visural-common/test/com/visural/common/datastruct/BeanListTest.java index 6e509fe..a19083f 100644 --- a/visural-common/test/com/visural/common/datastruct/BeanListTest.java +++ b/visural-common/test/com/visural/common/datastruct/BeanListTest.java @@ -111,7 +111,59 @@ public void setS3(String s3) { public String toString() { return String.format("%s %s %s\n", s1, s2, s3); } + } + + //////////////////////////////////////////////// + + public abstract static class Parent { + private String parent = "p"; + + public String getParent() { + return parent; + } + public void setParent(String parent) { + this.parent = parent; + } + + public abstract String getAbstract(); + } + + public class Child1 extends Parent { + private String abstractStr = Double.valueOf(Math.random()).toString(); + + @Override + public String getAbstract() { + return abstractStr; + } + } + + public class Child2 extends Parent { + + private String abstractStr = Double.valueOf(Math.random()).toString(); + + @Override + public String getAbstract() { + return abstractStr+20; + } + } + + /** + * A test which ensures that beans which descend from a common parent can + * be sorted on an abstract parent property even + * when different implementations are provided. + * @throws Exception + */ + public void testAbstractBean() throws Exception { + BeanList bl = new BeanList(new ArrayList(), 4); + for (int n = 0; n < 100; n++) { + if (Math.random() < 0.5d) { + bl.add(new Child1()); + } else { + bl.add(new Child2()); + } + } + bl.sortByProperties("abstract"); } }