protected void populateAnnotationsCache ( Class < ? > clazz , Map < String , String > injections ) throws IllegalAccessException , InvocationTargetException { while ( clazz != null ) { List < AnnotationCacheEntry > annotations = null ; synchronized ( annotationCache ) { annotations = annotationCache . get ( clazz ) ; } if ( annotations == null ) { annotations = new ArrayList <> ( ) ; // Initialize methods annotations Method [ ] methods = null ; methods = clazz . getDeclaredMethods ( ) ; Method postConstruct = null ; Method preDestroy = null ; for ( Method method : methods ) { if ( method . isAnnotationPresent ( PostConstruct . class ) ) { if ( ( postConstruct != null ) || ( method . getParameterTypes ( ) . length != 0 ) || ( Modifier . isStatic ( method . getModifiers ( ) ) ) || ( method . getExceptionTypes ( ) . length > 0 ) || ( ! method . getReturnType ( ) . getName ( ) . equals ( "void" ) ) ) { throw new IllegalArgumentException ( "Invalid PostConstruct annotation" ) ; } postConstruct = method ; } if ( method . isAnnotationPresent ( PreDestroy . class ) ) { if ( ( preDestroy != null || method . getParameterTypes ( ) . length != 0 ) || ( Modifier . isStatic ( method . getModifiers ( ) ) ) || ( method . getExceptionTypes ( ) . length > 0 ) || ( ! method . getReturnType ( ) . getName ( ) . equals ( "void" ) ) ) { throw new IllegalArgumentException ( "Invalid PreDestroy annotation" ) ; } preDestroy = method ; } } if ( postConstruct != null ) { annotations . add ( new AnnotationCacheEntry ( postConstruct . getName ( ) , postConstruct . getParameterTypes ( ) , null , AnnotationCacheEntryType . POST_CONSTRUCT ) ) ; } if ( preDestroy != null ) { annotations . add ( new AnnotationCacheEntry ( preDestroy . getName ( ) , preDestroy . getParameterTypes ( ) , null , AnnotationCacheEntryType . PRE_DESTROY ) ) ; } if ( annotations . size ( ) == 0 ) { // Use common empty list to save memory annotations = Collections . emptyList ( ) ; } synchronized ( annotationCache ) { annotationCache . put ( clazz , annotations ) ; } } // CHECKSTYLE:OFF clazz = clazz . getSuperclass ( ) ; // CHECKSTYLE:ON } }
Make sure that the annotations cache has been populated for the provided class .