Skip to content

Commit 4aa18b2

Browse files
committed
Establish order of methods in source code
1 parent 29a0534 commit 4aa18b2

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/test/java/io/github/spannm/leetcode/tool/UnitTestCreator.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,17 @@ boolean createUnitTest(Class<?> _clazz) throws IOException {
182182
}
183183

184184
Method findTestMethod(Class<?> _clazz) {
185-
Method[] methods = _clazz.getDeclaredMethods();
186-
if (methods.length == 0) {
187-
methods = Arrays.stream(_clazz.getDeclaredClasses())
185+
List<Method> list = Arrays.stream(_clazz.getDeclaredMethods().length > 0 ? _clazz.getDeclaredMethods() :
186+
Arrays.stream(_clazz.getDeclaredClasses())
188187
.filter(c -> Modifier.isStatic(c.getModifiers()))
189188
.map(Class::getDeclaredMethods)
190189
.filter(arr -> arr.length > 0)
191190
.findFirst().orElseThrow(
192-
() -> new LeetcodeRuntimeException("No non-static methods found in inner classes of " + _clazz));
193-
}
194-
return Arrays.stream(methods)
191+
() -> new LeetcodeRuntimeException("No non-static methods found in inner classes of " + _clazz)))
195192
.filter(m -> !Modifier.isStatic(m.getModifiers()))
196-
.filter(m -> !Modifier.isPrivate(m.getModifiers()))
193+
.filter(m -> !Modifier.isPrivate(m.getModifiers())).collect(Collectors.toCollection(ArrayList::new));
194+
return IntStream.rangeClosed(1, list.size())
195+
.mapToObj(i -> list.get(list.size() - i)) // establish order of methods in source code
197196
.min(Comparator.comparingInt(Method::getModifiers)
198197
// prioritize non-void return types
199198
.thenComparing((m1, m2) -> Integer.compare(m1.getReturnType() == void.class ? -1 : 1, m2.getReturnType() == void.class ? -1 : 1)))

0 commit comments

Comments
 (0)