|
5 | 5 |
|
6 | 6 | import java.lang.reflect.Method; |
7 | 7 | import java.lang.reflect.ParameterizedType; |
8 | | -import java.util.Arrays; |
| 8 | +import java.util.Collections; |
9 | 9 | import java.util.List; |
10 | 10 | import java.util.function.Supplier; |
11 | 11 |
|
@@ -36,49 +36,49 @@ abstract class AbstractIdentifyUsingCollectionImpl implements Implementation { |
36 | 36 | List<? extends PageFragment> getStreamOfPageFragmentsFor(Method method) { |
37 | 37 |
|
38 | 38 | IdentifyUsing identifyUsing = method.getAnnotation(IdentifyUsing.class); |
39 | | - Class<?> type = method.getReturnType(); |
40 | 39 | Class<PageFragment> genericType = getGenericType(method); |
41 | 40 | By by = ByProducers.createBy(identifyUsing); |
42 | 41 |
|
43 | | - waitIfAnnotationPresent(method, type, genericType, by); |
| 42 | + waitIfAnnotationPresent(method, genericType, by); |
44 | 43 | return findPageFragments(genericType, by); |
45 | 44 |
|
46 | 45 | } |
47 | 46 |
|
48 | | - private void waitIfAnnotationPresent(Method method, Class<?> type, Class<PageFragment> genericType, By by) { |
| 47 | + private void waitIfAnnotationPresent(Method method, Class<PageFragment> genericType, By by) { |
49 | 48 | WaitUntil annotation = method.getAnnotation(WaitUntil.class); |
50 | 49 | if (annotation != null) { |
51 | | - doWaitUntil(annotation, type, genericType, by); |
| 50 | + doWaitUntil(annotation, genericType, by); |
52 | 51 | } |
53 | 52 | } |
54 | 53 |
|
55 | 54 | @SuppressWarnings("unchecked") |
56 | | - private void doWaitUntil(WaitUntil annotation, Class<?> type, Class<PageFragment> genericType, By by) { |
| 55 | + private void doWaitUntil(WaitUntil annotation, Class<PageFragment> genericType, By by) { |
| 56 | + Condition condition = getConditionInstance(annotation); |
| 57 | + assertConditionCanHandleCollection(condition); |
| 58 | + if (annotation.timeout() > 0) { |
| 59 | + Wait.withTimeoutOf(annotation.timeout(), annotation.unit()) |
| 60 | + .untilSupplied(() -> findPageFragments(genericType, by)) |
| 61 | + .is(condition); |
| 62 | + } else { |
| 63 | + Wait.untilSupplied(() -> findPageFragments(genericType, by)).is(condition); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private Condition getConditionInstance(WaitUntil annotation) { |
57 | 68 | try { |
58 | | - Condition condition = annotation.value().newInstance(); |
59 | | - assertConditionCanHandleCollection(type, condition); |
60 | | - if (annotation.timeout() > 0) { |
61 | | - Wait.withTimeoutOf(annotation.timeout(), annotation.unit()) |
62 | | - .untilSupplied(() -> findPageFragments(genericType, by)) |
63 | | - .is(condition); |
64 | | - } else { |
65 | | - Wait.untilSupplied(() -> findPageFragments(genericType, by)).is(condition); |
66 | | - } |
| 69 | + return annotation.value().newInstance(); |
67 | 70 | } catch (InstantiationException | IllegalAccessException e) { |
68 | 71 | throw new IllegalSignatureException(COULD_NOT_CREATE_PREDICATE_INSTANCE_MSG, e); |
69 | 72 | } |
70 | 73 | } |
71 | 74 |
|
72 | | - private void assertConditionCanHandleCollection(Class<?> type, Condition condition) { |
73 | | - Class<? extends Condition> conditionClass = condition.getClass(); |
74 | | - Method testMethod = Arrays.stream(conditionClass.getMethods())// |
75 | | - .filter(method -> "test".equals(method.getName())) |
76 | | - .findFirst() |
77 | | - .orElseThrow(() -> new IllegalStateException("test method not found")); |
78 | | - if (!testMethod.getParameterTypes()[0].isAssignableFrom(type)) { |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + private void assertConditionCanHandleCollection(Condition condition) { |
| 77 | + try { |
| 78 | + condition.test(Collections.<List<? extends PageFragment>>emptyList()); |
| 79 | + } catch (ClassCastException e) { |
79 | 80 | throw new IllegalSignatureException( |
80 | | - "Condition '" + conditionClass.getSimpleName() + "' cant't handle method return type '" |
81 | | - + type.getSimpleName() + "'"); |
| 81 | + "Condition '" + condition.getClass().getSimpleName() + "' cant't handle List<? extends PageFragment>"); |
82 | 82 | } |
83 | 83 | } |
84 | 84 |
|
|
0 commit comments