EventConsumer extends MessageConsumer { @SuppressWarnings("unchecked") static Class<?> getParameterType(Object object, Class<?> expectedType) { Collection<Class<?>> extendedAndImplementedTypes = getExtendedAndImplementedTypes(object.getClass(), new LinkedList<Class<?>>()); for (Class<?> type : extendedAndImplementedTypes) { Type[] implementedInterfaces = type.getGenericInterfaces(); for (Type implementedInterface : implementedInterfaces) { if (implementedInterface instanceof ParameterizedType) { ParameterizedType parameterizedCandidateType = (ParameterizedType) implementedInterface; if (parameterizedCandidateType.getRawType().equals(expectedType)) { Type[] typeArguments = parameterizedCandidateType.getActualTypeArguments(); Type typeArgument; if (typeArguments.length == 0) { typeArgument = Object.class; } else { typeArgument = parameterizedCandidateType.getActualTypeArguments()[0]; } return (Class<?>) typeArgument; } } } } throw new RuntimeException("Expected type " + expectedType + " is not in class hierarchy of " + object.getClass()); } EventConsumer(Event<Object> eventControl, Instance<Object> eventPool); @Override void handleMessage(Message message);  }
@Test public void testGetParameterTypeForDirectImplementer() { Class<?> clazz = EventConsumer.getParameterType(new TestEventContainsId(), ContainsId.class); Assert.assertNotNull(clazz); Assert.assertEquals(Integer.class, clazz); }
@Test public void testGetParameterTypeForIndirectImplementer() { Class<?> clazz = EventConsumer.getParameterType(new TestEventContainsIdChild(), ContainsId.class); Assert.assertNotNull(clazz); Assert.assertEquals(Integer.class, clazz); }
@Test public void testGetParameterTypeForExtensionImplementer() { Class<?> clazz = EventConsumer.getParameterType(new TestEventContainsIdExtension(), ContainsId.class); Assert.assertNotNull(clazz); Assert.assertEquals(Integer.class, clazz); }