Skip to content

Commit

Permalink
Fix issue with sub-classed bean list
Browse files Browse the repository at this point in the history
  • Loading branch information
rjnichols committed May 26, 2011
1 parent eebcede commit 1708743
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
15 changes: 10 additions & 5 deletions visural-common/src/com/visural/common/datastruct/BeanList.java
Expand Up @@ -99,6 +99,7 @@ public BeanList<T> resort() {

private BeanList<T> sort(final String... properties) {
Collections.sort(list, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
try {
for (int n = 0; n < properties.length; n++) {
Expand All @@ -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;

Expand Down
Expand Up @@ -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<Parent> bl = new BeanList<BeanListTest.Parent>(new ArrayList<Parent>(), 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");
}
}

0 comments on commit 1708743

Please sign in to comment.