001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.javadoc; 020 021import java.util.regex.Pattern; 022 023import com.google.common.base.CharMatcher; 024import com.puppycrawl.tools.checkstyle.api.DetailNode; 025import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 026import com.puppycrawl.tools.checkstyle.api.Utils; 027 028/** 029 * <p> 030 * Checks that <a href=" 031 * http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#firstsentence"> 032 * Javadoc summary sentence</a> does not contain phrases that are not recommended to use. 033 * By default Check validate that first sentence is not empty:</p><br/> 034 * <pre> 035 * <module name="SummaryJavadocCheck"/> 036 * </pre> 037 * <p> 038 * To ensure that summary do not contain phrase like "This method returns" , use following config: 039 * <p> 040 * <pre> 041 * <module name="SummaryJavadocCheck"> 042 * <property name="forbiddenSummaryFragments" 043 * value="^This method returns.*"/> 044 * </module> 045 * </pre> 046 * <p> 047 * To specify period symbol at the end of first javadoc sentence - use following config: 048 * <pre> 049 * <module name="SummaryJavadocCheck"> 050 * <property name="period" 051 * value="period"/> 052 * </module> 053 * </pre> 054 * </p> 055 * 056 * @author max 057 * @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a> 058 */ 059public class SummaryJavadocCheck extends AbstractJavadocCheck 060{ 061 062 /** 063 * Regular expression for forbidden summary fragments. 064 */ 065 private Pattern forbiddenSummaryFragments = Utils.createPattern("^$"); 066 067 /** 068 * Period symbol at the end of first javadoc sentence. 069 */ 070 private String period = "."; 071 072 /** 073 * Sets custom value of regular expression for forbidden summary fragments. 074 * @param pattern user's value. 075 */ 076 public void setForbiddenSummaryFragments(String pattern) 077 { 078 forbiddenSummaryFragments = Utils.createPattern(pattern); 079 } 080 081 /** 082 * Sets value of period symbol at the end of first javadoc sentence. 083 * @param period period's value. 084 */ 085 public void setPeriod(String period) 086 { 087 this.period = period; 088 } 089 090 @Override 091 public int[] getDefaultJavadocTokens() 092 { 093 return new int[] { 094 JavadocTokenTypes.JAVADOC, 095 }; 096 } 097 098 @Override 099 public void visitJavadocToken(DetailNode ast) 100 { 101 String firstSentence = getFirstSentence(ast); 102 final int endOfSentence = firstSentence.lastIndexOf(period); 103 if (endOfSentence == -1) { 104 log(ast.getLineNumber(), "summary.first.sentence"); 105 } 106 else { 107 firstSentence = firstSentence.substring(0, endOfSentence); 108 if (containsForbiddenFragment(firstSentence)) { 109 log(ast.getLineNumber(), "summary.javaDoc"); 110 } 111 } 112 } 113 114 /** 115 * Finds and returns first sentence. 116 * @param ast Javadoc root node. 117 * @return first sentence. 118 */ 119 private String getFirstSentence(DetailNode ast) 120 { 121 final StringBuilder result = new StringBuilder(); 122 for (DetailNode child : ast.getChildren()) { 123 if (child.getType() != JavadocTokenTypes.JAVADOC_INLINE_TAG 124 && child.getText().contains(". ")) 125 { 126 result.append(getCharsTillDot(child)); 127 break; 128 } 129 else { 130 result.append(child.getText()); 131 } 132 } 133 return result.toString(); 134 } 135 136 /** 137 * Finds and returns chars till first dot. 138 * @param textNode node with javadoc text. 139 * @return String with chars till first dot. 140 */ 141 private String getCharsTillDot(DetailNode textNode) 142 { 143 final StringBuilder result = new StringBuilder(); 144 for (DetailNode child : textNode.getChildren()) { 145 result.append(child.getText()); 146 if (".".equals(child.getText()) 147 && JavadocUtils.getNextSibling(child).getType() == JavadocTokenTypes.WS) 148 { 149 break; 150 } 151 } 152 return result.toString(); 153 } 154 155 /** 156 * Tests if first sentence contains forbidden summary fragment. 157 * @param firstSentence String with first sentence. 158 * @return true, if first sentence contains forbidden summary fragment. 159 */ 160 private boolean containsForbiddenFragment(String firstSentence) 161 { 162 // This regexp is used to convert multiline javdoc to single line without stars. 163 String javadocText = firstSentence.replaceAll("\n[ ]+(\\*)|^[ ]+(\\*)", " "); 164 javadocText = CharMatcher.WHITESPACE.trimAndCollapseFrom(javadocText, ' '); 165 return forbiddenSummaryFragments.matcher(javadocText).find(); 166 } 167}