1 package com.puppycrawl.tools.checkstyle.checks; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TextBlock; 23 import com.puppycrawl.tools.checkstyle.api.Utils; 24 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.regex.Pattern ; 31 import java.util.regex.PatternSyntaxException ; 32 33 import org.apache.commons.beanutils.ConversionException; 34 35 103 public class TrailingCommentCheck extends AbstractFormatCheck 104 { 105 106 private static final String DEFAULT_FORMAT = "^[\\s\\}\\);]*$"; 107 108 109 private Pattern mLegalComment; 110 111 116 public void setLegalComment(final String aFormat) 117 throws ConversionException 118 { 119 try { 120 mLegalComment = Utils.getPattern(aFormat); 121 } 122 catch (final PatternSyntaxException e) { 123 throw new ConversionException("unable to parse " + aFormat, e); 124 } 125 } 126 130 public TrailingCommentCheck() throws ConversionException 131 { 132 super(DEFAULT_FORMAT); 133 } 134 135 136 public int[] getDefaultTokens() 137 { 138 return new int[0]; 139 } 140 141 142 public void visitToken(DetailAST aAST) 143 { 144 throw new IllegalStateException ("visitToken() shouldn't be called."); 145 } 146 147 148 public void beginTree(DetailAST aRootAST) 149 { 150 final Pattern blankLinePattern = getRegexp(); 151 final Map cppComments = getFileContents().getCppComments(); 152 final Map cComments = getFileContents().getCComments(); 153 final Set lines = new HashSet (); 154 lines.addAll(cppComments.keySet()); 155 lines.addAll(cComments.keySet()); 156 157 final Iterator linesIter = lines.iterator(); 158 while (linesIter.hasNext()) { 159 final Integer lineNo = (Integer ) linesIter.next(); 160 161 final String line = getLines()[lineNo.intValue() - 1]; 162 String lineBefore = ""; 163 TextBlock comment = null; 164 if (cppComments.containsKey(lineNo)) { 165 comment = (TextBlock) cppComments.get(lineNo); 166 lineBefore = line.substring(0, comment.getStartColNo()); 167 } 168 else if (cComments.containsKey(lineNo)) { 169 final List commentList = (List ) cComments.get(lineNo); 170 comment = (TextBlock) commentList.get(commentList.size() - 1); 171 lineBefore = line.substring(0, comment.getStartColNo()); 172 if (comment.getText().length == 1) { 173 final String lineAfter = 174 line.substring(comment.getEndColNo() + 1).trim(); 175 if (!"".equals(lineAfter)) { 176 continue; 178 } 179 } 180 } 181 if ((comment != null) 182 && !blankLinePattern.matcher(lineBefore).find() 183 && !isLegalComment(comment)) 184 { 185 log(lineNo.intValue(), "trailing.comments"); 186 } 187 } 188 } 189 190 196 private boolean isLegalComment(final TextBlock aComment) 197 { 198 if (mLegalComment == null) { 199 return false; 200 } 201 if (aComment.getStartLineNo() != aComment.getEndLineNo()) { 203 return false; 204 } 205 String commentText = aComment.getText()[0]; 206 commentText = commentText.substring(2); 208 if (commentText.endsWith("*/")) { 210 commentText = commentText.substring(0, commentText.length() - 2); 211 } 212 commentText = commentText.trim(); 213 return mLegalComment.matcher(commentText).find(); 214 } 215 } 216 | Popular Tags |