*/
@SuppressWarnings("fallthrough")
static Map<Method, String> validate(Object subscriber, List<Method> subscriberMethods) {
Map<Method, String> errors = new HashMap<Method, String>(subscriberMethods.size());
for (Method method : subscriberMethods) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (null != subscribeAnnotation) {
issueWarningsIfPresent(subscribeAnnotation, subscriber, method);
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length != 1) {
errors.put(method, String.format(
"Subscriber: %s's Method: %s is annotated as a subscriber but defines more that one arguments.",
subscriber.getClass(), method.toGenericString()));
} else if (parameterTypes[0].equals(Object.class)
&& !subscriber.getClass().equals(CatchAllSubscriber.class)
&& !DynamicSubscriber.class.isAssignableFrom(subscriber.getClass())) {
errors.put(method, String.format(
"Subscriber: %s's Method: %s is a subscriber for java.lang.Object, that is too broad an interest.",
subscriber.getClass(), method.toGenericString()));
} else if (DynamicSubscriber.class.isAssignableFrom(subscriber.getClass())
&& !parameterTypes[0].equals(Object.class)) {
Class<?> targetedEventType = ((DynamicSubscriber) subscriber).getEventType();
if (!parameterTypes[0].isAssignableFrom(targetedEventType)) {
errors.put(method, String.format(
"Dynamic subscriber: %s's Method: %s's argument is not compatible with the interested event type %s.",
subscriber.getClass(), method.toGenericString(), targetedEventType.getName()));
}
} else if (subscribeAnnotation.batchingStrategy() != Subscribe.BatchingStrategy.None) {
if (!(Iterable.class.isAssignableFrom(parameterTypes[0]))) {
errors.put(method, String.format(
"Subscriber: %s's Method: %s is annotated with batching strategy: %s but does not accept an Iterable argument.",
subscriber.getClass(), method.toGenericString(),
subscribeAnnotation.batchingStrategy()));
} else {
Type[] genericParameterTypes = method.getGenericParameterTypes();
if (!(genericParameterTypes[0] instanceof ParameterizedType)) {
errors.put(method, String.format(
"Subscriber: %s's Method: %s is a subscriber for java.lang.Object, that is too broad an interest.",
subscriber.getClass(), method.toGenericString()));
}
}
switch (subscribeAnnotation.batchingStrategy()) {
case SizeOrAge:
if (subscribeAnnotation.batchSize() <= 1) {
errors.put(method, String.format(
"Subscriber: %s's Method: %s is annotated with batching strategy: %s but does define a batch size.",
subscriber.getClass(), method.toGenericString(),
subscribeAnnotation.batchingStrategy()));
}
case Age:
if (subscribeAnnotation.batchAge() <= 0) {
errors.put(method, String.format(
"Subscriber: %s's Method: %s is annotated with batching strategy: %s but does define a batch age.",
subscriber.getClass(), method.toGenericString(),
subscribeAnnotation.batchingStrategy()));
}
break;
}
}
}