public static void addAnnotation ( String annotation , LinkedHashMap < String , Object > memberValues , Object toAnnotate ) { if ( toAnnotate instanceof CtClass ) { CtClass classToAnnotate = ( CtClass ) toAnnotate ; ClassFile cf = classToAnnotate . getClassFile ( ) ; ConstPool cp = cf . getConstPool ( ) ; AnnotationsAttribute attr = ( AnnotationsAttribute ) cf . getAttribute ( AnnotationsAttribute . visibleTag ) ; if ( attr == null ) { attr = new AnnotationsAttribute ( cp , AnnotationsAttribute . visibleTag ) ; } Annotation a = new Annotation ( annotation , cp ) ; if ( memberValues != null ) { addMemberValuesToAnnotation ( a , cp , memberValues ) ; } attr . addAnnotation ( a ) ; cf . addAttribute ( attr ) ; } else if ( toAnnotate instanceof CtMethod ) { CtMethod methodToAnnotate = ( CtMethod ) toAnnotate ; MethodInfo mi = methodToAnnotate . getMethodInfo ( ) ; ConstPool cp = mi . getConstPool ( ) ; AnnotationsAttribute attr = ( AnnotationsAttribute ) mi . getAttribute ( AnnotationsAttribute . visibleTag ) ; if ( attr == null ) { attr = new AnnotationsAttribute ( cp , AnnotationsAttribute . visibleTag ) ; } Annotation a = new Annotation ( annotation , cp ) ; if ( memberValues != null ) { addMemberValuesToAnnotation ( a , cp , memberValues ) ; } attr . addAnnotation ( a ) ; mi . addAttribute ( attr ) ; } else if ( toAnnotate instanceof CtField ) { CtField fieldToAnnotate = ( CtField ) toAnnotate ; FieldInfo fi = fieldToAnnotate . getFieldInfo ( ) ; ConstPool cp = fi . getConstPool ( ) ; AnnotationsAttribute attr = ( AnnotationsAttribute ) fi . getAttribute ( AnnotationsAttribute . visibleTag ) ; if ( attr == null ) { attr = new AnnotationsAttribute ( cp , AnnotationsAttribute . visibleTag ) ; } Annotation a = new Annotation ( annotation , cp ) ; if ( memberValues != null ) { addMemberValuesToAnnotation ( a , cp , memberValues ) ; } attr . addAnnotation ( a ) ; fi . addAttribute ( attr ) ; } else { throw new UnsupportedOperationException ( "Unknown object type: " + toAnnotate . getClass ( ) ) ; } }
Adds the selected annotation to the Object along with the specified memberValues .