AnnotationRetriever { public <T extends Annotation> T getAnnotation(Method method) throws AnnotationNotFoundException { T annotation = (T) method.getAnnotation(anClass); if(annotation == null && originClass != null && !method.getDeclaringClass().equals(originClass)) { try { Method implementedMethod = originClass.getMethod(method.getName(), method.getParameterTypes()); annotation = (T) implementedMethod.getAnnotation(anClass); } catch (NoSuchMethodException e) { if(LOG.isLoggable(Level.FINE)) { LOG.fine("method in implementing class not found. " + e.getMessage()); } } catch (SecurityException e) { if(LOG.isLoggable(Level.FINE)) { LOG. fine("cannot access method. " + e.getMessage()); } } } if(annotation == null) { throw new AnnotationNotFoundException("unable to find annotation with classname " + anClass.getName()); } return annotation; } AnnotationRetriever(Class<? extends Annotation> annotationClass);  AnnotationRetriever(Class<? extends Annotation> annotationClass, Class<?> originClass); T getAnnotation(Method method);  }
@Test(expected = AnnotationNotFoundException.class) public void getAnnotation_WithoutAnnotation_ThrowsException() throws Exception { retriever.getAnnotation(this.getClass().getMethod("setUp", new Class<?>[] {})); }
@Test(timeout = 500) public void getAnnotation_WithAnnotation_ReturnAnnotation() throws Exception { Test testAnnotation = retriever.getAnnotation(this.getClass().getMethod("getAnnotation_WithAnnotation_ReturnAnnotation", new Class<?>[] {})); assertThat(testAnnotation.timeout(), is(500l)); }