private boolean invokeCleanups ( Class < ? > clazz , Object target , Class < ? > cleanupClazz , Throwable exception ) throws Exception { boolean invoked = false ; if ( ! cleanupClazz . equals ( Object . class ) ) { // Christian Beikov 29.07.2013: Traverse whole hierarchy // instead of retrieving the annotation directly from // the class object. List < Method > methods = ReflectionUtils . getMethods ( target . getClass ( ) , Cleanup . class ) ; Method m = null ; for ( Method candidate : methods ) { Cleanup c = AnnotationUtils . findAnnotation ( candidate , Cleanup . class ) ; if ( cleanupClazz . equals ( c . value ( ) ) ) { m = candidate ; break ; } } if ( m != null ) { final Class < ? > [ ] parameterTypes = m . getParameterTypes ( ) ; if ( parameterTypes . length == 1 ) { if ( ReflectionUtils . isSubtype ( exception . getClass ( ) , parameterTypes [ 0 ] ) ) { m . invoke ( target , exception ) ; invoked = true ; } else { throw new IllegalArgumentException ( "Cleanup method with name " + cleanupClazz . getName ( ) + " requires a parameter that is not a subtype of the exception class " + exception . getClass ( ) . getName ( ) ) ; } } else { m . invoke ( target ) ; invoked = true ; } } } return invoked ; }
Invokes the cleanup methods of the exception handler or exception handling .