Skip to content

Commit 7949cc3

Browse files
committed
Added convenience methods for finding methods
1 parent 214a120 commit 7949cc3

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/main/java/org/bbottema/javareflection/MethodUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ public static Set<InvokableObject<Method>> findSimpleCompatibleMethod(final Clas
285285
}
286286
}
287287

288+
/**
289+
* Delegates to {@link #findCompatibleMethod(Class, String, Set, Class[])}, with the types of the given arguments extracted using {@link TypeUtils#collectTypes(Object[])}.
290+
*/
291+
public static Set<InvokableObject<Method>> findCompatibleMethod(final Class<?> datatype, final String methodName, final Set<LookupMode> lookupMode,
292+
final Object... args) throws NoSuchMethodException {
293+
return findCompatibleMethod(datatype, methodName, lookupMode, TypeUtils.collectTypes(args));
294+
}
295+
288296
/**
289297
* Same as <code>getConstructor()</code>, except for getting a {@link Method} of a classtype, using the name to indicate which method should be
290298
* located.
@@ -543,4 +551,14 @@ public static boolean methodHasCollectionParameter(final Method m) {
543551
}
544552
return false;
545553
}
554+
555+
public static Method onlyMethod(Set<InvokableObject<Method>> methods) {
556+
if (methods.size() == 0) {
557+
return null;
558+
} else if (methods.size() == 1) {
559+
return methods.iterator().next().getMethod();
560+
} else {
561+
throw new AssertionError("Expected 1 or less methods, but found more than 1 methods: " + methods);
562+
}
563+
}
546564
}

src/test/java/org/bbottema/javareflection/MethodUtilsTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package org.bbottema.javareflection;
22

3-
import lombok.AllArgsConstructor;
43
import org.bbottema.javareflection.model.InvokableObject;
54
import org.bbottema.javareflection.model.LookupMode;
65
import org.bbottema.javareflection.model.MethodModifier;
76
import org.bbottema.javareflection.model.MethodParameter;
87
import org.bbottema.javareflection.testmodel.*;
98
import org.bbottema.javareflection.util.MetaAnnotationExtractor;
109
import org.bbottema.javareflection.valueconverter.ValueConversionHelper;
11-
import org.jetbrains.annotations.NonNls;
12-
import org.jetbrains.annotations.NotNull;
1310
import org.junit.Before;
1411
import org.junit.Test;
1512
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
@@ -30,6 +27,7 @@
3027
import static org.assertj.core.api.Assertions.fail;
3128
import static org.assertj.core.util.Lists.newArrayList;
3229
import static org.bbottema.javareflection.ClassUtils.collectMethodsByName;
30+
import static org.bbottema.javareflection.MethodUtils.onlyMethod;
3331
import static org.bbottema.javareflection.model.MethodModifier.MATCH_ANY;
3432

3533
public class MethodUtilsTest {
@@ -47,19 +45,19 @@ public void testFindCompatibleMethod()
4745
allButSmartLookup.remove(LookupMode.SMART_CONVERT);
4846

4947
Set<InvokableObject<Method>> m = MethodUtils.findCompatibleMethod(B.class, "foo", allButSmartLookup, double.class, Pear.class, String.class);
50-
assertThat(m).isNotEmpty();
51-
assertThat(m.iterator().next().getMethod()).isEqualTo(Foo.class.getMethod("foo", Double.class, Fruit.class, char.class));
48+
assertThat(m).hasSize(1);
49+
assertThat(onlyMethod(m)).isEqualTo(Foo.class.getMethod("foo", Double.class, Fruit.class, char.class));
5250
Set<InvokableObject<Method>> m2 = MethodUtils.findCompatibleMethod(B.class, "foo", allButSmartLookup, double.class, Pear.class, String.class);
53-
assertThat(m2).isNotEmpty();
51+
assertThat(m2).hasSize(1);
5452
assertThat(m2).isEqualTo(m);
5553
// find the same method, but now the first implementation on C should be returned
5654
m = MethodUtils.findCompatibleMethod(C.class, "foo", allButSmartLookup, double.class, Pear.class, String.class);
57-
assertThat(m).isNotEmpty();
58-
assertThat(m.iterator().next().getMethod()).isEqualTo(C.class.getMethod("foo", Double.class, Fruit.class, char.class));
55+
assertThat(m).hasSize(1);
56+
assertThat(onlyMethod(m)).isEqualTo(C.class.getMethod("foo", Double.class, Fruit.class, char.class));
5957
// find a String method
6058
m = MethodUtils.findCompatibleMethod(String.class, "concat", EnumSet.noneOf(LookupMode.class), String.class);
61-
assertThat(m).isNotEmpty();
62-
assertThat(m.iterator().next().getMethod()).isEqualTo(String.class.getMethod("concat", String.class));
59+
assertThat(m).hasSize(1);
60+
assertThat(onlyMethod(m)).isEqualTo(String.class.getMethod("concat", String.class));
6361
// shouldn't be able to find the following methods
6462
try {
6563
MethodUtils.findCompatibleMethod(B.class, "foos", allButSmartLookup, double.class, Pear.class, String.class);
@@ -80,7 +78,7 @@ public void testFindCompatibleMethod()
8078
// OK
8179
}
8280
Set<InvokableObject<Method>> result = MethodUtils.findCompatibleMethod(B.class, "foo", LookupMode.FULL, double.class, Fruit.class, Math.class);
83-
assertThat(result).isNotEmpty();
81+
assertThat(result).hasSize(1);
8482
}
8583

8684
@Test

0 commit comments

Comments
 (0)