Skip to content

Commit 535a127

Browse files
authored
Merge pull request #2 from jshiell/master
Sort order for static factory methods is now stable via sorting by arity,methodName, so as to avoid indeterminate results when constructor a parameter class with multiple static methods with the same arity
2 parents c1419a4 + 0b151e2 commit 535a127

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/com/googlecode/yadic/resolvers/StaticMethodResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import static com.googlecode.totallylazy.reflection.Methods.modifier;
2121
import static com.googlecode.totallylazy.Option.none;
2222
import static com.googlecode.totallylazy.Option.some;
23+
import static com.googlecode.totallylazy.comparators.Comparators.ascending;
24+
import static com.googlecode.totallylazy.comparators.Comparators.comparators;
2325
import static com.googlecode.totallylazy.predicates.Predicates.not;
2426
import static com.googlecode.totallylazy.predicates.Predicates.where;
2527
import static com.googlecode.totallylazy.Sequences.sequence;
2628
import static com.googlecode.yadic.generics.TypeConverter.convertParametersToInstances;
29+
import static com.googlecode.totallylazy.reflection.Methods.methodName;
2730
import static com.googlecode.totallylazy.reflection.Types.classOf;
2831
import static com.googlecode.totallylazy.reflection.Types.matches;
2932
import static java.lang.String.format;
@@ -49,7 +52,7 @@ public T resolve(Type type) throws Exception {
4952
filter(modifier(PUBLIC).and(modifier(STATIC)).
5053
and(where(genericReturnType(), matches(type)).
5154
and(where(genericParameterTypes(), not(exists(matches(type))))))).
52-
sortBy(descending(arity()));
55+
sortBy(comparators(descending(arity()), ascending(methodName())));
5356

5457
if (methods.isEmpty()) {
5558
throw new ContainerException(concreteClass.getName() + " does not have any public static methods that return " + type);

test/com/googlecode/yadic/examples/MyStaticMethodClass.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public static MyStaticMethodClass myStaticMethodClass1(String parameter) {
1111
return new MyStaticMethodClass("myStaticMethodClass1");
1212
}
1313

14+
public static MyStaticMethodClass myStaticMethodClass2a(String parameter1, Boolean parameter2) {
15+
return new MyStaticMethodClass("myStaticMethodClass2a");
16+
}
17+
1418
public static MyStaticMethodClass myStaticMethodClass2(String parameter1, Integer parameter2) {
1519
return new MyStaticMethodClass("myStaticMethodClass2");
1620
}

test/com/googlecode/yadic/resolvers/StaticMethodResolverTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public void choosesLargestArity() throws Exception {
4444
assertThat(staticMethodClass.constructedBy, is("myStaticMethodClass2"));
4545
}
4646

47+
@Test
48+
public void sortOrderIsStableWhenMultipleMethodsExistWithTheSameArity() throws Exception {
49+
StaticMethodResolver<MyStaticMethodClass> resolver = StaticMethodResolver
50+
.staticMethodResolver(
51+
containerWith("foobar").addInstance(Integer.class, 1).addInstance(Boolean.class, Boolean.TRUE),
52+
MyStaticMethodClass.class);
53+
MyStaticMethodClass staticMethodClass = resolver.resolve(MyStaticMethodClass.class);
54+
assertThat(staticMethodClass, is(notNullValue()));
55+
assertThat(staticMethodClass.constructedBy, is("myStaticMethodClass2"));
56+
}
57+
4758
@Test(expected = ContainerException.class)
4859
public void ignoresSelfReferencingStaticMethods() throws Exception {
4960
Container resolver = new SimpleContainer();

0 commit comments

Comments
 (0)