1 /* 2 * Copyright (c) 2011-2025 Yegor Bugayenko 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 1) Redistributions of source code must retain the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer. 2) Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following 12 * disclaimer in the documentation and/or other materials provided 13 * with the distribution. 3) Neither the name of the Qulice.com nor 14 * the names of its contributors may be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 package com.qulice.checkstyle; 32 33 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 34 import com.puppycrawl.tools.checkstyle.api.DetailAST; 35 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 36 37 /** 38 * Check for empty line at the beginning and at the end of Javadoc. 39 * 40 * <p>You can't have empty line at the beginning or at the end of Javadoc. 41 * 42 * <p>The following red lines in class Javadoc will be reported as violations. 43 * <pre> 44 * /** 45 * <span style="color:red" >*</span> 46 * * This is my class. 47 * <span style="color:red" >*</span> 48 * */ 49 * public final class Foo { 50 * // ... 51 * </pre> 52 * 53 * @since 0.17 54 */ 55 public final class JavadocEmptyLineCheck extends AbstractCheck { 56 57 @Override 58 public int[] getDefaultTokens() { 59 return new int[] { 60 TokenTypes.PACKAGE_DEF, 61 TokenTypes.CLASS_DEF, 62 TokenTypes.INTERFACE_DEF, 63 TokenTypes.ANNOTATION_DEF, 64 TokenTypes.ANNOTATION_FIELD_DEF, 65 TokenTypes.ENUM_DEF, 66 TokenTypes.ENUM_CONSTANT_DEF, 67 TokenTypes.VARIABLE_DEF, 68 TokenTypes.CTOR_DEF, 69 TokenTypes.METHOD_DEF, 70 }; 71 } 72 73 @Override 74 public int[] getAcceptableTokens() { 75 return this.getDefaultTokens(); 76 } 77 78 @Override 79 public int[] getRequiredTokens() { 80 return this.getDefaultTokens(); 81 } 82 83 @Override 84 public void visitToken(final DetailAST ast) { 85 final String[] lines = this.getLines(); 86 final int current = ast.getLineNo(); 87 final int start = 88 JavadocEmptyLineCheck.findCommentStart(lines, current) + 1; 89 if (JavadocEmptyLineCheck.isNodeHavingJavadoc(ast, start)) { 90 if (JavadocEmptyLineCheck.isJavadocLineEmpty(lines[start])) { 91 this.log(start + 1, "Empty Javadoc line at the beginning"); 92 } 93 final int end = 94 JavadocEmptyLineCheck.findCommentEnd(lines, current) - 1; 95 if (JavadocEmptyLineCheck.isJavadocLineEmpty(lines[end])) { 96 this.log(end + 1, "Empty Javadoc line at the end"); 97 } 98 } 99 } 100 101 /** 102 * Check if Javadoc line is empty. 103 * @param line Javadoc line 104 * @return True when Javadoc line is empty 105 */ 106 private static boolean isJavadocLineEmpty(final String line) { 107 return "*".equals(line.trim()); 108 } 109 110 /** 111 * Check if node has Javadoc. 112 * @param node Node to be checked for Javadoc. 113 * @param start Line number where comment starts. 114 * @return True when node has Javadoc 115 */ 116 private static boolean isNodeHavingJavadoc(final DetailAST node, 117 final int start) { 118 return start > getLineNoOfPreviousNode(node); 119 } 120 121 /** 122 * Returns line number of previous node. 123 * @param node Current node. 124 * @return Line number of previous node 125 */ 126 private static int getLineNoOfPreviousNode(final DetailAST node) { 127 int start = 0; 128 final DetailAST previous = node.getPreviousSibling(); 129 if (previous != null) { 130 start = previous.getLineNo(); 131 } 132 return start; 133 } 134 135 /** 136 * Find Javadoc starting comment. 137 * @param lines List of lines to check. 138 * @param start Start searching from this line number. 139 * @return Line number with found starting comment or -1 otherwise. 140 */ 141 private static int findCommentStart(final String[] lines, final int start) { 142 return JavadocEmptyLineCheck.findTrimmedTextUp(lines, start, "/**"); 143 } 144 145 /** 146 * Find Javadoc ending comment. 147 * @param lines Array of lines to check. 148 * @param start Start searching from this line number. 149 * @return Line number with found ending comment, or -1 if it wasn't found. 150 */ 151 private static int findCommentEnd(final String[] lines, final int start) { 152 return JavadocEmptyLineCheck.findTrimmedTextUp(lines, start, "*/"); 153 } 154 155 /** 156 * Find a text in lines, by going up. 157 * @param lines Array of lines to check. 158 * @param start Start searching from this line number. 159 * @param text Text to find. 160 * @return Line number with found text, or -1 if it wasn't found. 161 */ 162 private static int findTrimmedTextUp(final String[] lines, 163 final int start, final String text) { 164 int found = -1; 165 for (int pos = start - 1; pos >= 0; pos -= 1) { 166 if (lines[pos].trim().equals(text)) { 167 found = pos; 168 break; 169 } 170 } 171 return found; 172 } 173 }