private void associateJavadoc ( List < Comment > comments , List < Node > nodes ) { final TreeMap < Integer , Node > startPosMap = Maps . newTreeMap ( ) ; for ( Node node : nodes ) node . accept ( new ForwardingAstVisitor ( ) { @ Override public boolean visitNode ( Node node ) { if ( node . isGenerated ( ) ) return false ; int startPos = node . getPosition ( ) . getStart ( ) ; Node current = startPosMap . get ( startPos ) ; if ( current == null || ! ( current instanceof JavadocContainer ) ) { startPosMap . put ( startPos , node ) ; } return false ; } } ) ; for ( Comment comment : comments ) { if ( ! comment . isJavadoc ( ) ) continue ; Map < Integer , Node > tailMap = startPosMap . tailMap ( comment . getPosition ( ) . getEnd ( ) ) ; if ( tailMap . isEmpty ( ) ) continue ; Node assoc = tailMap . values ( ) . iterator ( ) . next ( ) ; if ( ! ( assoc instanceof JavadocContainer ) ) continue ; JavadocContainer jc = ( JavadocContainer ) assoc ; if ( jc . rawJavadoc ( ) != null ) { if ( jc . rawJavadoc ( ) . getPosition ( ) . getEnd ( ) >= comment . getPosition ( ) . getEnd ( ) ) continue ; } jc . rawJavadoc ( comment ) ; } }
Associates comments that are javadocs to the node they belong to by checking if the node that immediately follows a javadoc node is a JavadocContainer .