Skip to content

Commit 1dcf37b

Browse files
authored
Merge pull request #1 from jshiell/java7
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 cd1d647 + 0bcf8f6 commit 1dcf37b

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@
66
import com.googlecode.totallylazy.Sequence;
77
import com.googlecode.yadic.ContainerException;
88
import com.googlecode.yadic.Resolver;
9-
import com.googlecode.yadic.generics.Types;
109

1110
import java.lang.reflect.Method;
1211
import java.lang.reflect.Type;
1312
import java.util.ArrayList;
1413
import java.util.List;
1514

1615
import static com.googlecode.totallylazy.Arrays.exists;
16+
import static com.googlecode.totallylazy.Callables.ascending;
1717
import static com.googlecode.totallylazy.Callables.cast;
1818
import static com.googlecode.totallylazy.Callables.descending;
1919
import static com.googlecode.totallylazy.Methods.genericParameterTypes;
2020
import static com.googlecode.totallylazy.Methods.genericReturnType;
21+
import static com.googlecode.totallylazy.Methods.methodName;
2122
import static com.googlecode.totallylazy.Methods.modifier;
2223
import static com.googlecode.totallylazy.Option.none;
2324
import static com.googlecode.totallylazy.Option.some;
2425
import static com.googlecode.totallylazy.Predicates.not;
2526
import static com.googlecode.totallylazy.Predicates.where;
2627
import static com.googlecode.totallylazy.Sequences.sequence;
28+
import static com.googlecode.totallylazy.comparators.Comparators.comparators;
2729
import static com.googlecode.yadic.generics.TypeConverter.convertParametersToInstances;
2830
import static com.googlecode.yadic.generics.Types.classOf;
2931
import static com.googlecode.yadic.generics.Types.matches;
@@ -50,7 +52,7 @@ public T resolve(Type type) throws Exception {
5052
filter(modifier(PUBLIC).and(modifier(STATIC)).
5153
and(where(genericReturnType(), matches(type)).
5254
and(where(genericParameterTypes(), not(exists(matches(type))))))).
53-
sortBy(descending(arity()));
55+
sortBy(comparators(descending(arity()), ascending(methodName())));
5456

5557
if (methods.isEmpty()) {
5658
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)