Skip to content

Commit

Permalink
1. Fixed a bug when adding extra spaces after the parameter name
Browse files Browse the repository at this point in the history
* @param line   Line position.
* @param column Column position.
2. Fixed a bug when using comment-like text in string literals:
final String comment = "/* some text */";
Fixes for issue #1079
  • Loading branch information
Vladimir.Shapkin authored and Vladimir.Shapkin committed Oct 1, 2024
2 parents 345e149 + 65e871c commit 4844907
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 59 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.2</version>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.13</version>
<version>2.0.16</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion qulice-checkstyle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<version>3.16.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ public void visitToken(final DetailAST ast) {
* @param text Text to find.
* @return Line number with found text, or -1 if it wasn't found.
*/
private static int findTrimmedTextUp(final String[] lines,
final int start, final String text) {
private static int findTrimmedTextUp(
final String[] lines,
final int start,
final String text
) {
int found = -1;
for (int pos = start - 1; pos >= 0; pos -= 1) {
if (lines[pos].trim().equals(text)) {
Expand Down Expand Up @@ -172,8 +175,13 @@ private static int findCommentEnd(final String[] lines, final int start) {
* @param tag Name of the tag.
* @checkstyle ParameterNumber (3 lines)
*/
private void findProhibited(final String[] lines, final int start,
final int cstart, final int cend, final String tag) {
private void findProhibited(
final String[] lines,
final int start,
final int cstart,
final int cend,
final String tag
) {
final List<Integer> found =
this.findTagLineNum(lines, cstart, cend, tag);
if (!found.isEmpty()) {
Expand All @@ -194,8 +202,12 @@ private void findProhibited(final String[] lines, final int start,
* @return Line number with found tag or -1 otherwise.
* @checkstyle ParameterNumber (3 lines)
*/
private List<Integer> findTagLineNum(final String[] lines, final int start,
final int end, final String tag) {
private List<Integer> findTagLineNum(
final String[] lines,
final int start,
final int end,
final String tag
) {
final String prefix = String.format(" * @%s ", tag);
final List<Integer> found = new ArrayList<>(1);
for (int pos = start; pos <= end; pos += 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@

/**
* Multi line comment checker.
* Used by the checkstyle process multiple times as a singleton.
* @since 0.23.1
*/
public final class MultiLineCommentCheck extends AbstractCheck {
/**
* Pattern for check.
* It is not final as it is initialized from the configuration.
*/
private Pattern format = Pattern.compile("^$");

/**
* The message to report for a match.
* It is not final as it is initialized from the configuration.
*/
private String message = "";

/**
* Comment line.
* It is not final because the visitToken method is called many times
* during the class under test and the field is reinitialized with a new object.
*/
@SuppressWarnings("PMD.AvoidStringBufferField")
private StringBuilder line;
Expand Down Expand Up @@ -96,10 +101,27 @@ public void visitToken(final DetailAST ast) {
}
}

/**
* The method is called from checkstyle to configure this class.
* The parameter is set from the checks.xml file
* <module name="com.qulice.checkstyle.MultiLineCommentCheck"/> and
* <property name="format" value=" this regexp "/> property
*
* @param fmt Validatig regexp.
*/
public void setFormat(final String fmt) {
this.format = Pattern.compile(fmt);
}

/**
* The method is called from checkstyle to configure this class.
* The parameter is set from the checks.xml file
* <module name="com.qulice.checkstyle.MultiLineCommentCheck"/> and
* <property name="message" value="First sentence in a comment should start with ....."/>
* property
*
* @param msg Error message.
*/
public void setMessage(final String msg) {
this.message = msg;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
* Copyright (c) 2011-2024 Qulice.com
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the Qulice.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.qulice.checkstyle;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import java.util.LinkedList;
import java.util.List;

/**
* Checks that constructor, declared as private class is used more than once.
*
* @since 0.3
*/
public final class ProhibitUnusedPrivateConstructorCheck extends AbstractCheck {

@Override
public int[] getDefaultTokens() {
return new int[] {TokenTypes.CLASS_DEF};
}

@Override
public int[] getAcceptableTokens() {
return this.getDefaultTokens();
}

@Override
public int[] getRequiredTokens() {
return this.getDefaultTokens();
}

@Override
public void visitToken(final DetailAST ast) {
final DetailAST objblock = ast.findFirstToken(TokenTypes.OBJBLOCK);
if (objblock != null) {
this.checkConstructors(objblock);
}
}

/**
* Collects all private constructors in a given object block.
*
* @param objblock Node which contains constructors
* @return List of DetailAST nodes representing the private constructors
*/
private static List<DetailAST> collectPrivateConstructors(final DetailAST objblock) {
final List<DetailAST> prvctors = new LinkedList<>();
final DetailAST firstchld = objblock.getFirstChild();
for (DetailAST child = firstchld; child != null; child = child.getNextSibling()) {
if (child.getType() == TokenTypes.CTOR_DEF && isPrivate(child)) {
prvctors.add(child);
}
}
return prvctors;
}

/**
* Checks if a private constructor is used in the object block.
*
* @param privatector Node representing the private constructor
* @param objblock Node which contains constructors
* @return True if the private constructor is used, False otherwise
*/
private static boolean isPrivateConstructorUsed(
final DetailAST privatector, final DetailAST objblock) {
return
isPrivateCtorUsedInOtherCtors(privatector, objblock)
||
isPrivateCtorUsedInMethods(privatector, objblock);
}

/**
* Checks if a private constructor is used in other constructors.
*
* @param privatector Node representing the private constructor
* @param objblock Node containing constructors
* @return True if the private constructor is used, False otherwise
*/
private static boolean isPrivateCtorUsedInOtherCtors(
final DetailAST privatector, final DetailAST objblock) {
final List<DetailAST> allctors = collectAllConstructors(objblock);
return allctors.stream()
.anyMatch(
otherCtor -> otherCtor != privatector
&&
isCallingConstructor(otherCtor, privatector));
}

/**
* Checks if a private constructor is used in methods of the object block.
*
* @param privatector Node representing the private constructor
* @param objblock Node containing methods
* @return True if the private constructor is used, False otherwise
*/
private static boolean isPrivateCtorUsedInMethods(
final DetailAST privatector, final DetailAST objblock) {
boolean result = false;
final DetailAST firstchld = objblock.getFirstChild();
for (DetailAST child = firstchld; child != null; child = child.getNextSibling()) {
if (child.getType() == TokenTypes.METHOD_DEF
&&
isCallingConstructor(child, privatector)) {
result = true;
break;
}
}
return result;
}

/**
* Collects all constructors in a given object block.
*
* @param objblock Node which contains constructors
* @return List of DetailAST nodes representing all the constructors
*/
private static List<DetailAST> collectAllConstructors(final DetailAST objblock) {
final List<DetailAST> allctors = new LinkedList<>();
final DetailAST firstchld = objblock.getFirstChild();
for (DetailAST child = firstchld; child != null; child = child.getNextSibling()) {
if (child.getType() == TokenTypes.CTOR_DEF) {
allctors.add(child);
}
}
return allctors;
}

/**
* Returns true if specified node has modifiers of type
* <code>PRIVATE</code>.
*
* @param node Node to check.
* @return True if specified node contains modifiers of type
* <code>PRIVATE</code>, else returns <code>false</code>.
*/
private static boolean isPrivate(final DetailAST node) {
final DetailAST modifiers = node.findFirstToken(TokenTypes.MODIFIERS);
return modifiers.getChildCount(TokenTypes.LITERAL_PRIVATE) > 0;
}

private static boolean isCallingConstructor(
final DetailAST methodorctor, final DetailAST targetctor) {
boolean result = false;
final DetailAST body = methodorctor.findFirstToken(TokenTypes.SLIST);
if (body != null) {
DetailAST stmt = body.getFirstChild();
while (stmt != null && !result) {
result = isMatchingConstructorCall(stmt, targetctor);
stmt = stmt.getNextSibling();
}
}
return result;
}

private static boolean isMatchingConstructorCall(
final DetailAST stmt, final DetailAST targetctor) {
return
stmt.getType() == TokenTypes.CTOR_CALL
&&
matchesConstructorSignature(stmt, targetctor);
}

private static boolean matchesConstructorSignature(
final DetailAST callexpr, final DetailAST ctor) {
final DetailAST callparams = callexpr.findFirstToken(TokenTypes.ELIST);
final DetailAST ctorparams = ctor.findFirstToken(TokenTypes.PARAMETERS);
return parametersCountMatch(callparams, ctorparams);
}

private static boolean parametersCountMatch(
final DetailAST callparams, final DetailAST ctorparams) {
final int ncallparams = callparams.getChildCount(TokenTypes.EXPR);
final int nctorparams = ctorparams.getChildCount(TokenTypes.PARAMETER_DEF);
return ncallparams == nctorparams;
}

/**
* Checks if private constructors are used.
* Logs a message if a private constructor is not used.
*
* @param objblock Node which contains constructors
*/
private void checkConstructors(final DetailAST objblock) {
final List<DetailAST> prvctors = collectPrivateConstructors(objblock);
for (final DetailAST ctor : prvctors) {
if (!isPrivateConstructorUsed(ctor, objblock)) {
this.log(ctor.getLineNo(), "Unused private constructor.");
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ final class RequiredJavaDocTag {
* @param patt Pattern for checking the contents of a tag in a string.
* @param rep Reference to a method for writing a message to the log.
*/
RequiredJavaDocTag(final String name, final Pattern patt,
final Reporter rep) {
RequiredJavaDocTag(
final String name,
final Pattern patt,
final Reporter rep
) {
this(
name,
Pattern.compile(
Expand All @@ -112,8 +115,12 @@ final class RequiredJavaDocTag {
* @param rep Reference to a method for writing a message to the log.
* @checkstyle ParameterNumberCheck (3 lines)
*/
RequiredJavaDocTag(final String cname, final Pattern ptag,
final Pattern patt, final Reporter rep) {
RequiredJavaDocTag(
final String cname,
final Pattern ptag,
final Pattern patt,
final Reporter rep
) {
this.name = cname;
this.tag = ptag;
this.content = patt;
Expand All @@ -126,8 +133,11 @@ final class RequiredJavaDocTag {
* @param start Line number where comment starts.
* @param end Line number where comment ends.
*/
public void matchTagFormat(final String[] lines, final int start,
final int end) {
public void matchTagFormat(
final String[] lines,
final int start,
final int end
) {
final Map<Integer, String> found = new HashMap<>(1);
for (int pos = start; pos <= end; pos += 1) {
final String line = lines[pos];
Expand Down
Loading

0 comments on commit 4844907

Please sign in to comment.