Skip to content

Commit

Permalink
poartial fix for #46 - Sonarqube found problems in Checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
isopov committed Dec 5, 2013
1 parent 5ce771a commit 1d614c3
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void realExecute()
log("compiled on " + compileTimestamp, Project.MSG_VERBOSE);

// Check for no arguments
if ((mFileName == null) && (mFileSets.size() == 0)) {
if ((mFileName == null) && mFileSets.isEmpty()) {
throw new BuildException(
"Must specify at least one of 'file' or nested 'fileset'.",
getLocation());
Expand Down Expand Up @@ -449,7 +449,7 @@ protected AuditListener[] getListeners() throws ClassNotFoundException,
final AuditListener[] listeners = new AuditListener[formatterCount];

// formatters
if (mFormatters.size() == 0) {
if (mFormatters.isEmpty()) {
final OutputStream debug = new LogOutputStream(this,
Project.MSG_DEBUG);
final OutputStream err = new LogOutputStream(this, Project.MSG_ERR);
Expand Down
8 changes: 5 additions & 3 deletions src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter;
import com.puppycrawl.tools.checkstyle.api.Utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -44,8 +45,8 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
* This class provides the functionality to check a set of files.
Expand Down Expand Up @@ -254,7 +255,7 @@ public int process(List<File> aFiles)
for (final File f : aFiles) {
final String fileName = f.getAbsolutePath();
fireFileStarted(fileName);
final TreeSet<LocalizedMessage> fileMessages = Sets.newTreeSet();
final SortedSet<LocalizedMessage> fileMessages = Sets.newTreeSet();
try {
final FileText theText = new FileText(f.getAbsoluteFile(),
mCharset);
Expand Down Expand Up @@ -501,7 +502,8 @@ public void fireFileFinished(String aFileName)
* @param aFileName the audited file
* @param aErrors the audit errors from the file
*/
public void fireErrors(String aFileName, TreeSet<LocalizedMessage> aErrors)
public void fireErrors(String aFileName,
SortedSet<LocalizedMessage> aErrors)
{
final String stripped = getStrippedFileName(aFileName);
for (final LocalizedMessage element : aErrors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;

import java.util.TreeSet;
import java.util.SortedSet;

/**
* Used by FileSetChecks to distribute AuditEvents to AuditListeners.
Expand All @@ -43,5 +43,5 @@ public interface MessageDispatcher
* @param aFileName the audited file
* @param aErrors the audit errors from the file
*/
void fireErrors(String aFileName, TreeSet<LocalizedMessage> aErrors);
void fireErrors(String aFileName, SortedSet<LocalizedMessage> aErrors);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public void visitToken(DetailAST aAST)
aAST.getText());
}
}
else if (getAbstractOption() == BlockOption.TEXT) {
if (!hasText(slistAST)) {
log(slistAST.getLineNo(),
slistAST.getColumnNo(),
"block.empty",
aAST.getText());
}
else if (getAbstractOption() == BlockOption.TEXT
&& !hasText(slistAST))
{
log(slistAST.getLineNo(),
slistAST.getColumnNo(),
"block.empty",
aAST.getText());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public void visitToken(DetailAST aAST)
if (objBlock != null) {
DetailAST child = objBlock.getFirstChild();
while (child != null) {
if (child.getType() == TokenTypes.METHOD_DEF) {
if (CheckUtils.isEqualsMethod(child)) {
if (hasObjectParameter(child)) {
hasEqualsObject = true;
}
else {
mEqualsMethods.add(child);
}
if (child.getType() == TokenTypes.METHOD_DEF
&& CheckUtils.isEqualsMethod(child))
{
if (hasObjectParameter(child)) {
hasEqualsObject = true;
}
else {
mEqualsMethods.add(child);
}
}
child = child.getNextSibling();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,18 @@ public void visitToken(final DetailAST aMethodCall)
final DetailAST expr = dot.getNextSibling().getFirstChild();

if ("equals".equals(method.getText())
|| (!mIgnoreEqualsIgnoreCase && "equalsIgnoreCase"
.equals(method.getText())))
&& containsOneArg(expr) && containsAllSafeTokens(expr))
{
if (containsOneArg(expr) && containsAllSafeTokens(expr)) {
log(aMethodCall.getLineNo(), aMethodCall.getColumnNo(),
"equals".equals(method.getText())
? "equals.avoid.null"
: "equalsIgnoreCase.avoid.null");
}
log(aMethodCall.getLineNo(), aMethodCall.getColumnNo(),
"equals.avoid.null");
}

if (!mIgnoreEqualsIgnoreCase
&& "equalsIgnoreCase".equals(method.getText())
&& containsOneArg(expr) && containsAllSafeTokens(expr))
{
log(aMethodCall.getLineNo(), aMethodCall.getColumnNo(),
"equalsIgnoreCase.avoid.null");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public void visitToken(DetailAST aAST)

final DetailAST slist = aAST.findFirstToken(TokenTypes.SLIST);

if (!isTerminated(slist, true, true)) {
if (!hasFallTruComment(aAST, nextGroup)) {
if (!isLastGroup) {
log(nextGroup, "fall.through");
}
else {
log(aAST, "fall.through.last");
}
if (!isTerminated(slist, true, true)
&& !hasFallTruComment(aAST, nextGroup))
{
if (!isLastGroup) {
log(nextGroup, "fall.through");
}
else {
log(aAST, "fall.through.last");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ public void leaveToken(DetailAST aAst)
private void visitExpr(DetailAST aAst)
{
mExprDepth++;
if (mExprDepth == 1) {
if (!mInForHeader && (mLastStatementEnd == aAst.getLineNo())) {
log(aAst, "multiple.statements.line");
}
if (mExprDepth == 1
&& !mInForHeader
&& (mLastStatementEnd == aAst.getLineNo()))
{
log(aAst, "multiple.statements.line");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,9 @@ private void checkException(FullIdent aExc, List<ClassInfo> aKnownExcs)
final ClassInfo newClassInfo =
createClassInfo(new Token(aExc), getCurrentClassName());

if (!mAllowUnchecked) {
if (isUnchecked(newClassInfo.getClazz())) {
log(aExc.getLineNo(), aExc.getColumnNo(),
"redundant.throws.unchecked", aExc.getText());
}
if (!mAllowUnchecked && isUnchecked(newClassInfo.getClazz())) {
log(aExc.getLineNo(), aExc.getColumnNo(),
"redundant.throws.unchecked", aExc.getText());
}

boolean shouldAdd = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,8 @@ private void findDuplicateFromLine(
}

final Collection<Integer> ignoreEntries = aIgnore.get(aILine);
// avoid Integer constructor whenever we can
if (ignoreEntries != null) {
if (ignoreEntries.contains(jLine)) {
continue;
}
if (ignoreEntries != null && ignoreEntries.contains(jLine)) {
continue;
}

final int duplicateLines =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public void startElement(final String aNamespaceURI,
mStack.push(new PkgControl(pkg));
}
else if ("subpackage".equals(aQName)) {
assert mStack.size() > 0;
assert !mStack.isEmpty();
final String name = safeGet(aAtts, "name");
mStack.push(new PkgControl(mStack.peek(), name));
}
else if ("allow".equals(aQName) || "disallow".equals(aQName)) {
assert mStack.size() > 0;
assert !mStack.isEmpty();
// Need to handle either "pkg" or "class" attribute.
// May have "exact-match" for "pkg"
// May have "local-only"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ private void doVisitToken(FullIdent aIdent, boolean aIsStatic,
}
}
else if (groupIdx == mLastGroup) {
doVisitTokenInSameGroup(aIdent, aIsStatic, aPrevious, name,
line);
doVisitTokenInSameGroup(aIsStatic, aPrevious, name, line);
}
else {
log(line, "import.ordering", name);
Expand All @@ -309,14 +308,13 @@ else if (groupIdx == mLastGroup) {
/**
* Shares processing...
*
* @param aIdent the import to process.
* @param aIsStatic whether the token is static or not.
* @param aPrevious previous non-static but current is static (above), or
* previous static but current is non-static (under).
* @param aName the name of the current import.
* @param aLine the line of the current import.
*/
private void doVisitTokenInSameGroup(FullIdent aIdent, boolean aIsStatic,
private void doVisitTokenInSameGroup(boolean aIsStatic,
boolean aPrevious, String aName, int aLine)
{
if (!mOrdered) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected final void processAST(DetailAST aAST)
}
}
else {
checkComment(aAST, cmt, theScope);
checkComment(aAST, cmt);
}
}
}
Expand Down Expand Up @@ -313,13 +313,12 @@ private boolean shouldCheck(final DetailAST aAST, final Scope aScope)
*
* @param aAST the token for the method
* @param aComment the Javadoc comment
* @param aScope the scope of the method.
*/
private void checkComment(DetailAST aAST, TextBlock aComment, Scope aScope)
private void checkComment(DetailAST aAST, TextBlock aComment)
{
final List<JavadocTag> tags = getMethodTags(aComment);

if (hasShortCircuitTag(aAST, tags, aScope)) {
if (hasShortCircuitTag(aAST, tags)) {
return;
}

Expand Down Expand Up @@ -354,11 +353,10 @@ private void checkComment(DetailAST aAST, TextBlock aComment, Scope aScope)
*
* @param aAST the construct being checked
* @param aTags the list of Javadoc tags associated with the construct
* @param aScope the scope of the construct
* @return true if the construct has a short circuit tag.
*/
private boolean hasShortCircuitTag(final DetailAST aAST,
final List<JavadocTag> aTags, final Scope aScope)
final List<JavadocTag> aTags)
{
// Check if it contains {@inheritDoc} tag
if ((aTags.size() != 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public HtmlTag nextTag()
*/
public boolean hasNextTag()
{
return (mTags.size() > 0);
return !mTags.isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ public void visitToken(DetailAST aAST)
processLeft(theAst);
}
}
else if ((theAst.getParent() == null)
else if (((theAst.getParent() == null)
|| (theAst.getParent().getType() != TokenTypes.TYPECAST)
|| (theAst.getParent().findFirstToken(TokenTypes.RPAREN)
!= theAst))
&& !isFollowsEmptyForIterator(theAst))
{
if (!isFollowsEmptyForIterator(theAst)) {
processRight(theAst);
}
processRight(theAst);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,12 @@ public boolean accept(AuditEvent aEvent)
return false;
}

// reject if line matches a line CSV value.
if (mLineFilter != null) {
if (mLineFilter.accept(aEvent.getLine())) {
return false;
}
if (mLineFilter != null && mLineFilter.accept(aEvent.getLine())) {
return false;
}

// reject if column matches a column CSV value.
if (mColumnFilter != null) {
if (mColumnFilter.accept(aEvent.getColumn())) {
return false;
}
if (mColumnFilter != null && mColumnFilter.accept(aEvent.getColumn())) {
return false;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ private void tagSuppressions(Collection<TextBlock> aComments)
for (final TextBlock comment : aComments) {
final int startLineNo = comment.getStartLineNo();
final String[] text = comment.getText();
tagCommentLine(text[0], startLineNo, comment.getStartColNo());
tagCommentLine(text[0], startLineNo);
for (int i = 1; i < text.length; i++) {
tagCommentLine(text[i], startLineNo + i, 0);
tagCommentLine(text[i], startLineNo + i);
}
}
}
Expand All @@ -496,9 +496,8 @@ private void tagSuppressions(Collection<TextBlock> aComments)
* checkstyle reporting on or the format for turning reporting off.
* @param aText the string to tag.
* @param aLine the line number of aText.
* @param aColumn the column number of aText.
*/
private void tagCommentLine(String aText, int aLine, int aColumn)
private void tagCommentLine(String aText, int aLine)
{
final Matcher matcher = mCommentRegexp.matcher(aText);
if (matcher.find()) {
Expand Down

0 comments on commit 1d614c3

Please sign in to comment.