public static Optional < SuggestedFix > suggestWhitelistAnnotation ( String whitelistAnnotation , TreePath where , VisitorState state ) { // TODO(bangert): Support annotations that do not have @Target(CLASS). if ( whitelistAnnotation . equals ( "com.google.errorprone.annotations.DontSuggestFixes" ) ) { return Optional . empty ( ) ; } SuggestedFix . Builder builder = SuggestedFix . builder ( ) ; Type whitelistAnnotationType = state . getTypeFromString ( whitelistAnnotation ) ; ImmutableSet < Tree . Kind > supportedWhitelistLocationKinds ; String annotationName ; if ( whitelistAnnotationType != null ) { supportedWhitelistLocationKinds = supportedTreeTypes ( whitelistAnnotationType . asElement ( ) ) ; annotationName = qualifyType ( state , builder , whitelistAnnotationType ) ; } else { // If we can't resolve the type, fall back to an approximation. int idx = whitelistAnnotation . lastIndexOf ( ' ' ) ; Verify . verify ( idx > 0 && idx + 1 < whitelistAnnotation . length ( ) ) ; supportedWhitelistLocationKinds = TREE_TYPE_UNKNOWN_ANNOTATION ; annotationName = whitelistAnnotation . substring ( idx + 1 ) ; builder . addImport ( whitelistAnnotation ) ; } Optional < Tree > whitelistLocation = StreamSupport . stream ( where . spliterator ( ) , false ) . filter ( tree -> supportedWhitelistLocationKinds . contains ( tree . getKind ( ) ) ) . filter ( Predicates . not ( SuggestedFixes :: isAnonymousClassTree ) ) . findFirst ( ) ; return whitelistLocation . map ( location -> builder . prefixWith ( location , "@" + annotationName + " " ) . build ( ) ) ; }
Create a fix to add a suppression annotation on the surrounding class .