Skip to content

Commit

Permalink
Merge 0c885e8 into e5c995e
Browse files Browse the repository at this point in the history
  • Loading branch information
Zopsss committed May 3, 2024
2 parents e5c995e + 0c885e8 commit d0309b0
Show file tree
Hide file tree
Showing 36 changed files with 1,209 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,28 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocLeadingAsteriskAlignCheck.java</fileName>
<specifier>array.access.unsafe.high</specifier>
<message>Potentially unsafe array access: the index could be larger than the array&apos;s bound</message>
<lineContent>final String startLine = getLines()[rootAst.getLineNumber() - 1];</lineContent>
<details>
found : int
required: @IndexFor(&quot;this.getLines()&quot;) or @LTLengthOf(&quot;this.getLines()&quot;) -- an integer less than this.getLines()&apos;s length
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocLeadingAsteriskAlignCheck.java</fileName>
<specifier>array.access.unsafe.low</specifier>
<message>Potentially unsafe array access: the index could be negative.</message>
<lineContent>final String startLine = getLines()[rootAst.getLineNumber() - 1];</lineContent>
<details>
found : int
required: an integer &gt;= 0 (@NonNegative or @Positive)
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.java</fileName>
<specifier>array.access.unsafe.high</specifier>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3901,6 +3901,13 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocLeadingAsteriskAlignCheck.java</fileName>
<specifier>dereference.of.nullable</specifier>
<message>dereference of possibly-null reference matcher.group(1)</message>
<lineContent>columnNumber = matcher.group(1).length() + 1;</lineContent>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.java</fileName>
<specifier>argument</specifier>
Expand Down
1 change: 1 addition & 0 deletions config/checkstyle-checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@
<property name="validateThrows" value="true"/>
</module>
<module name="JavadocMissingLeadingAsterisk"/>
<module name="JavadocLeadingAsteriskAlign"/>
<module name="JavadocMissingWhitespaceAfterAsterisk"/>
<module name="JavadocParagraph"/>
<module name="JavadocStyle">
Expand Down
2 changes: 2 additions & 0 deletions config/checkstyle-resources-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
files="[\\/]InputMagicNumberAnnotationElement\.java"/>
<suppress checks="FileTabCharacter"
files="[\\/]InputInnerTypeLastClass\.java"/>
<suppress checks="FileTabCharacter"
files="[\\/]InputJavadocLeadingAsteriskAlignTabs\.java"/>
<suppress checks="FileTabCharacter"
files="[\\/]InputJavadocMissingWhitespaceAfterAsteriskValidWithTab\.java"/>
<suppress checks="FileTabCharacter"
Expand Down
1 change: 1 addition & 0 deletions config/jsoref-spellchecker/whitelist.words
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ javadoc
javadocblocktaglocation
javadoccontentlocation
javadocdetailnodeparser
javadocleadingasteriskalign
javadocmetadatascraper
javadocmethod
javadocmissingleadingasterisk
Expand Down
2 changes: 2 additions & 0 deletions config/linkcheck-suppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocBlockTagLocationCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocContentLocationCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocContentLocationOption.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocLeadingAsteriskAlignCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.ClassInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.Token)">#%3Cinit%3E(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.Token)</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.ClassInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.Token)">../JavadocMethodCheck.ClassInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.Token)</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.ExceptionInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.ClassInfo)">#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.ClassInfo)</a>: doesn't exist.
Expand Down Expand Up @@ -1109,6 +1110,7 @@
<a href="com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocBlockTagLocationCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocBlockTagLocationCheck.html#%3Cinit%3E()</a>: doesn't exist.
<a href="com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocContentLocationCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocContentLocationCheck.html#%3Cinit%3E()</a>: doesn't exist.
<a href="com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocContentLocationOption.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocContentLocationOption.html#%3Cinit%3E()</a>: doesn't exist.
<a href="com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocLeadingAsteriskAlignCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocLeadingAsteriskAlignCheck.html#%3Cinit%3E()</a>: doesn't exist.
<a href="com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.ClassInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.Token)">com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.ClassInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.Token)</a>: doesn't exist.
<a href="com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.ExceptionInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.ClassInfo)">com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.ExceptionInfo.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.ClassInfo)</a>: doesn't exist.
<a href="com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.Token.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.FullIdent)">com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.Token.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.FullIdent)</a>: doesn't exist.
Expand Down
2 changes: 2 additions & 0 deletions config/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
<Class name="com.puppycrawl.tools.checkstyle.checks.coding.MatchXpathCheck"/>
<Class name="com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck" />
<Class name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck" />
<Class name=
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocLeadingAsteriskAlignCheck" />
<!-- createJavadocNode is private and can not be launched without visitToken -->
<Class name="com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck" />
<!-- has only one public method and all initialized in that method -->
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,7 @@
<exclude>**/JavadocMethodCheck.class</exclude>
<exclude>**/JavadocMissingLeadingAsteriskCheck.class</exclude>
<exclude>**/JavadocMissingWhitespaceAfterAsteriskCheck.class</exclude>
<exclude>**/JavadocLeadingAsteriskAlignCheck.class</exclude>
<exclude>**/JavadocParagraphCheck.class</exclude>
<exclude>**/JavadocStyleCheck.class</exclude>
<exclude>**/JavadocTagContinuationIndentationCheck.class</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ private static void fillChecksFromJavadocPackage() {
BASE_PACKAGE + ".checks.javadoc.JavadocContentLocationCheck");
NAME_TO_FULL_MODULE_NAME.put("JavadocMethodCheck",
BASE_PACKAGE + ".checks.javadoc.JavadocMethodCheck");
NAME_TO_FULL_MODULE_NAME.put("JavadocLeadingAsteriskAlignCheck",
BASE_PACKAGE + ".checks.javadoc.JavadocLeadingAsteriskAlignCheck");
NAME_TO_FULL_MODULE_NAME.put("JavadocMissingLeadingAsteriskCheck",
BASE_PACKAGE + ".checks.javadoc.JavadocMissingLeadingAsteriskCheck");
NAME_TO_FULL_MODULE_NAME.put("JavadocMissingWhitespaceAfterAsteriskCheck",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2024 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.checks.javadoc;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;

/**
* <p>
* Checks the alignment of
* <a href="https://docs.oracle.com/en/java/javase/14/docs/specs/javadoc/doc-comment-spec.html#leading-asterisks">
* leading asterisks</a> in a Javadoc comment. The Check ensures that leading asterisks
* are aligned vertically under the second asterisk ( &#42; )
* of opening Javadoc tag. The alignment of closing Javadoc tag ( &#42;/ ) is also checked.
* If a line contains a non-whitespace character before the leading asterisk then that line will be
* ignored. Same applies for closing Javadoc tag.
* If the ending javadoc line contains a leading asterisk, then that leading asterisk's alignment
* will be considered, the closing Javadoc tag will be ignored.
* </p>
* <p>
* If you're using tabs then specify the the tab width in the
* <a href="https://checkstyle.org/config.html#tabWidth">tabWidth</a> property.
* </p>
* <ul>
* <li>
* Property {@code violateExecutionOnNonTightHtml} - Control when to print violations if the
* Javadoc being examined by this check violates the tight html rules defined at
* <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
* Type is {@code boolean}.
* Default value is {@code false}.
* </li>
* </ul>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
* Violation Message Keys:
* </p>
* <ul>
* <li>
* {@code javadoc.asterisk.indentation}
* </li>
* <li>
* {@code javadoc.missed.html.close}
* </li>
* <li>
* {@code javadoc.parse.rule.error}
* </li>
* <li>
* {@code javadoc.unclosedHtml}
* </li>
* <li>
* {@code javadoc.wrong.singleton.html.tag}
* </li>
* </ul>
*
* @since 10.17.0
*/
@GlobalStatefulCheck
public class JavadocLeadingAsteriskAlignCheck extends AbstractJavadocCheck {

/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "javadoc.asterisk.indentation";

/** Specifies the column number of starting block of the javadoc comment with tabs expanded. */
private int commentBeginColNumber;

/** Specifies the line number of starting block of the javadoc comment. */
private int commentBeginLineNumber;

/**
* Specifies the column number of the leading asterisk
* where tabs are converted to a single whitespace.
*/
private int singleSpaceAsteriskColNumber;

@Override
public int[] getDefaultJavadocTokens() {
return new int[] {
JavadocTokenTypes.LEADING_ASTERISK,
};
}

@Override
public int[] getRequiredJavadocTokens() {
return getAcceptableJavadocTokens();
}

@Override
public void beginJavadocTree(DetailNode rootAst) {
// this method processes and sets information of starting javadoc tag.
final String startLine = getLines()[rootAst.getLineNumber() - 1];
commentBeginLineNumber = rootAst.getLineNumber();
commentBeginColNumber = CommonUtil.lengthExpandedTabs(
startLine, rootAst.getColumnNumber() - 1, getTabWidth());
}

@Override
public void visitJavadocToken(DetailNode ast) {
// this method checks the alignment of leading asterisks.
if (ast.getLineNumber() != commentBeginLineNumber) {
final int asteriskColNumber = getAsteriskColumnNumberWithTabsExpanded(ast.getText());

checkAsteriskAlignment(
ast.getLineNumber(),
commentBeginColNumber,
asteriskColNumber);
}
}

@Override
public void finishJavadocTree(DetailNode rootAst) {
// this method checks the alignment of closing javadoc tag.
final DetailAST javadocEndToken = getBlockCommentAst().getLastChild();

if (commentBeginLineNumber != javadocEndToken.getLineNo()) {
final String lastLine = getLastLine(javadocEndToken);
final int asteriskColNumber = getAsteriskColumnNumberWithTabsExpanded(lastLine);

checkAsteriskAlignment(
javadocEndToken.getLineNo(),
commentBeginColNumber,
asteriskColNumber);
}
}

/**
* Processes and sets the last line of javadoc which includes comment's ending token.
*
* @param endToken javadoc ending token
* @return last line of javadoc comment including ending token
*/
private String getLastLine(DetailAST endToken) {
final String lastLine;

if (endToken.getColumnNo() == 0) {
lastLine = endToken.getText();
}
else {
final String blockComment = JavadocUtil.getBlockCommentContent(getBlockCommentAst());
final List<String> commentTextLineList = Arrays.asList(blockComment.split("\n"));
lastLine = commentTextLineList.get(commentTextLineList.size() - 1) + "*/";
}

return lastLine;
}

/**
* Processes and returns the column number of leading asterisk from javadoc comment text
* with tabs expanded.
* Also sets 'singleSpaceAsteriskColNumber' if the leading asterisk is found.
*
* @param line single line from block comment text
* @return column number of leading asterisk with tabs expanded
*/
private int getAsteriskColumnNumberWithTabsExpanded(String line) {
int columnNumber = getAsteriskColumnNumber(line);

if (columnNumber != -1) {
singleSpaceAsteriskColNumber = columnNumber;
columnNumber = CommonUtil.lengthExpandedTabs(line, columnNumber, getTabWidth());
}

return columnNumber;
}

/**
* Processes and returns the column number of leading asterisk without tabs expanded.
* Returns -1 if there is no leading asterisk found.
*
* @param line javadoc comment line
* @return asterisk's column number
*/
private static int getAsteriskColumnNumber(String line) {
final Pattern pattern = Pattern.compile("^(\\s*)\\*");
final Matcher matcher = pattern.matcher(line);
final int columnNumber;

if (matcher.find()) {
columnNumber = matcher.group(1).length() + 1;
}
else {
columnNumber = -1;
}

return columnNumber;
}

/**
* Checks alignment of asterisks and logs violations.
*
* @param lineNumber line number of current comment line
* @param commentBeginColumn column number of javadoc starting token
* @param asteriskColumn column number of current line's leading asterisk
*/
private void checkAsteriskAlignment(int lineNumber,
int commentBeginColumn,
int asteriskColumn) {

if (asteriskColumn != -1) {
final int columnDifference = commentBeginColumn - asteriskColumn;
if (columnDifference != 0) {
log(
lineNumber,
singleSpaceAsteriskColNumber - 1,
MSG_KEY,
asteriskColumn,
commentBeginColNumber);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public final class SiteUtil {
Map.entry("LineLengthCheck.fileExtensions", VERSION_8_24),
// until https://github.com/checkstyle/checkstyle/issues/14052
Map.entry("JavadocBlockTagLocationCheck.violateExecutionOnNonTightHtml", VERSION_8_24),
Map.entry("JavadocLeadingAsteriskAlignCheck.violateExecutionOnNonTightHtml", "10.17.0"),
Map.entry("JavadocMissingLeadingAsteriskCheck.violateExecutionOnNonTightHtml", "8.38"),
Map.entry(
"RequireEmptyLineBeforeBlockTagGroupCheck.violateExecutionOnNonTightHtml",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
at.clause.order=Block tags have to appear in the order ''{0}''.
invalid.position=Javadoc comment is placed in the wrong location.
javadoc.asterisk.indentation=Leading asterisk has incorrect indentation level {0}, expected is {1}.
javadoc.blockTagLocation=The Javadoc block tag ''@{0}'' should be placed at the beginning of the line.
javadoc.classInfo=Unable to get class information for {0} tag ''{1}''.
javadoc.content.first.line=Javadoc content should start from the same line as /**.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
at.clause.order=Block-Tags müssen in der folgenden Reihenfolge erscheinen: ''{0}''.
invalid.position=Javadoc-Kommentar ist an der falschen Stelle platziert.
javadoc.asterisk.indentation=Das führende Sternchen hat die falsche Einrückungsebene {0}, erwartet wird {1}.
javadoc.blockTagLocation=Das Javadoc-block-tag ''@{0}'' sollte sich am Anfang der Zeile befinden.
javadoc.classInfo=Kann für das Tag ''{1}'' keine Klasseninformation zu {0} laden.
javadoc.content.first.line=Javadoc-Inhalt sollte in der gleichen Zeile wie /** beginnen.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
at.clause.order=Las etiquetas de bloque deben aparecer en el orden ''{0}''.
invalid.position=El comentario de Javadoc se coloca en la ubicación incorrecta.
javadoc.asterisk.indentation=El asterisco inicial tiene un nivel de sangría incorrecto {0}; lo esperado es {1}.
javadoc.blockTagLocation=La etiqueta de bloque ''@{0}'' de Javadoc debe colocarse al principio de la línea.
javadoc.classInfo=No se puede obtener la información de clase para {0} etiqueta ''{1}''.
javadoc.content.first.line=El contenido de Javadoc debe comenzar desde la misma línea que /**.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
at.clause.order=Estotunnisteiden on oltava järjestyksessä ''{0}''.
invalid.position=Javadoc-kommentti sijoitetaan väärään paikkaan.
javadoc.asterisk.indentation=Etutähdellä on virheellinen sisennystaso {0}, odotusarvo on {1}.
javadoc.blockTagLocation=Javadoc lohkon tunniste ''@{0}'' olisi sijoitettava rivin alkuun.
javadoc.classInfo=Luokkatiedot ei saatavilla: {0} tagi ''{1}''.
javadoc.content.first.line=Javadoc-sisällön pitäisi alkaa samalta riviltä kuin /**.
Expand Down

0 comments on commit d0309b0

Please sign in to comment.