Skip to content

Commit

Permalink
Fixes several Sonar violations
Browse files Browse the repository at this point in the history
  • Loading branch information
damianszczepanik authored and romani committed Mar 7, 2015
1 parent 250fe03 commit fdb9e26
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,10 @@ private static int getCountOfToken(DetailAST detAst, int type) {
while (detAst != null) {
count += detAst.getChildCount(type);
final DetailAST detAstChild = detAst.getFirstChild();
if (detAstChild == null) {
detAst = detAst.getNextSibling();
} else {
if (detAstChild != null) {
count += getCountOfToken(detAstChild, type);
detAst = detAst.getNextSibling();
}

detAst = detAst.getNextSibling();
}
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void setCustomDeclarationOrder(final String inputOrderDeclaration)
}
catch (StringIndexOutOfBoundsException exp) {
//if the structure of the input rule isn't correct
throw new RuntimeException("Unable to parse input rule: "
throw new IllegalArgumentException("Unable to parse input rule: "
+ currentState, exp);
}
}
Expand Down Expand Up @@ -806,7 +806,7 @@ private static boolean isAnonymousClass(DetailAST expressionAst)
*/
private static String getCombinedModifiersList(final DetailAST ast)
{
final StringBuffer modifiers = new StringBuffer();
final StringBuilder modifiers = new StringBuilder();
DetailAST astNode = ast.findFirstToken(TokenTypes.MODIFIERS);
if (astNode.getFirstChild() == null) {
//if we met package level modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,12 @@ public void visitToken(DetailAST detailAST)
// For warnings disable first lvl child must be an EXPR and
// second lvl child must be IDENT or LITERAL_NEW with
// appropriate boolean flags.
final boolean noWarning = (throwAST != null
final boolean noWarning = throwAST != null
&& firstLvlChild != null
&& secondLvlChild != null
&& firstLvlChild.getType() == TokenTypes.EXPR
&& ((allowThrow && secondLvlChild.getType()
== TokenTypes.IDENT)
|| (allowRethrow && secondLvlChild.getType()
== TokenTypes.LITERAL_NEW)));
&& ((allowThrow && secondLvlChild.getType() == TokenTypes.IDENT)
|| (allowRethrow && secondLvlChild.getType() == TokenTypes.LITERAL_NEW));

final DetailAST excType = paramDef.findFirstToken(TokenTypes.TYPE);
final FullIdent ident = CheckUtils.createFullType(excType);
Expand All @@ -137,7 +135,7 @@ public DetailAST getThrowAST(DetailAST parentAST)
if (currentNode.getType() != TokenTypes.PARAMETER_DEF
&& currentNode.getNumberOfChildren() > 0)
{
final DetailAST astResult = (getThrowAST(currentNode));
final DetailAST astResult = getThrowAST(currentNode);
if (astResult != null) {
return astResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public void work(DetailAST ast)
{

DetailAST nextNode = ast.getNextSibling();
final boolean isCommaSeparated = ((nextNode != null) && (nextNode
.getType() == TokenTypes.COMMA));
final boolean isCommaSeparated = (nextNode != null) && (nextNode
.getType() == TokenTypes.COMMA);

if (nextNode == null) {
// no nextMethods statement - no check
Expand Down Expand Up @@ -144,10 +144,8 @@ public void visitToken(DetailAST ast)
{

final DetailAST token = ast;
final boolean inFor = (ast.getParent().getType()
== TokenTypes.FOR_INIT);
final boolean inClass = (ast.getParent().getParent().getType()
== TokenTypes.CLASS_DEF);
final boolean inFor = ast.getParent().getType() == TokenTypes.FOR_INIT;
final boolean inClass = ast.getParent().getParent().getType() == TokenTypes.CLASS_DEF;

if (inClass) {
work(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,13 @@ public void visitToken(DetailAST detailAST)
switch (detailAST.getType())
{
case TokenTypes.METHOD_DEF:
{
if (isReturnCollection(detailAST))
{
methodDefs.push(detailAST);
}
break;
}

case TokenTypes.LITERAL_RETURN:
{
if (!methodDefs.isEmpty())
{
DetailAST currentMethodDef = getMethodDef(detailAST);
Expand All @@ -161,7 +159,7 @@ && isReturnedValueBeNull(detailAST))))
}
}
break;
}

default:
Utils.reportInvalidToken(detailAST.getType());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,26 @@ public class OverridableMethodInConstructorCheck extends Check
* A key is using to build a warning message about calls of an overridable
* methods from any constructor body.
* */
private final String keyCtor = "constructor";
private static final String KEY_CTOR = "constructor";

/**
* A key is using to build a warning message about calls of an overridable
* methods from any clone() method is implemented from Cloneable interface.
* */
private final String keyClone = "'clone()' method";
private static final String KEY_CLONE = "'clone()' method";

/**
* A key is using to build a warning message about calls of an overridable
* methods from any readObject() method is implemented from Serializable
* interface.
* */
private final String keyReadObject = "'readObject()' method";
private static final String KEY_READ_OBJECT = "'readObject()' method";

/**
* A list contains all METHOD_CALL DetailAST nodes that have been already
* visited by check.
* */
private List<DetailAST> visitedMethodCalls = new LinkedList<DetailAST>();
private final List<DetailAST> visitedMethodCalls = new LinkedList<DetailAST>();

/**
* A current MethodDef AST is being processed by check.
Expand Down Expand Up @@ -268,7 +268,7 @@ public void visitToken(final DetailAST detailAST)
switch (detailAST.getType()) {

case TokenTypes.CTOR_DEF:
logWarnings(detailAST, keyCtor);
logWarnings(detailAST, KEY_CTOR);
break;

case TokenTypes.METHOD_DEF:
Expand All @@ -279,13 +279,13 @@ public void visitToken(final DetailAST detailAST)
if (checkCloneMethod && "clone".equals(methodName)
&& realizesAnInterface(classDef, Cloneable.class.getSimpleName()))
{
logWarnings(detailAST, keyClone);
logWarnings(detailAST, KEY_CLONE);
}
else if (checkReadObjectMethod
&& "readObject".equals(methodName)
&& realizesAnInterface(classDef, Serializable.class.getSimpleName()))
{
logWarnings(detailAST, keyReadObject);
logWarnings(detailAST, KEY_READ_OBJECT);
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ private static List<DetailAST> getRedundantReturnsInTryCatchBlock(DetailAST tryA

// look for redundant returns in all catches
for (DetailAST catchBlockAst = getNextCatchBlock(blockAst);
catchBlockAst != null;) {
catchBlockAst != null;
catchBlockAst = getNextCatchBlock(blockAst)) {
DetailAST lastStatementOfCatchBlockAst = catchBlockAst
.getLastChild().getLastChild().getPreviousSibling();

Expand All @@ -243,7 +244,6 @@ private static List<DetailAST> getRedundantReturnsInTryCatchBlock(DetailAST tryA
}
}
blockAst = blockAst.getNextSibling();
catchBlockAst = getNextCatchBlock(blockAst);
}

// if redundant return is in finally block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ private static List<DetailAST> getChildren(final DetailAST node)
*/
private static boolean matches(String string, Collection<String> patterns) {
boolean result = false;
if (string != null && patterns != null && patterns.size() > 0) {
if (string != null && patterns != null && !patterns.isEmpty()) {
for (String pattern : patterns) {
if (string.matches(pattern)) {
result = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ private static DetailAST getNextNode(DetailAST expressionNode,
currentNode = currentNode.getParent();
}
}
currentNode = toVisit;

return currentNode;
return toVisit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void visitToken(DetailAST ast)
final List<DetailAST> badChildBlocks = getBadChildBlocks(
childBlocks, parentBlockSize);

if (badChildBlocks.size() == 0) {
if (badChildBlocks.isEmpty()) {
for (DetailAST childBlock : childBlocks) {
visitToken(childBlock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void visitToken(DetailAST ast)
child = child.getNextSibling();
}

final boolean hasAccessibleCtor = (hasDefaultCtor || hasPublicCtor);
final boolean hasAccessibleCtor = hasDefaultCtor || hasPublicCtor;

// figure out if class extends java.lang.object directly
// keep it simple for now and get a 99% solution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ private boolean isClassEnumeration(DetailAST ast)
hasMembers(DetailAST ast, List<Pattern> excludes)
{
final DetailAST objBlock = ast.getParent();
assert (objBlock.getType() == TokenTypes.OBJBLOCK);
assert objBlock.getType() == TokenTypes.OBJBLOCK;
boolean memberFound = false;
for (DetailAST member = objBlock.getFirstChild(); member != null; member = member
.getNextSibling()) {
if (member.getType() == TokenTypes.METHOD_DEF
|| member.getType() == TokenTypes.VARIABLE_DEF) {
final DetailAST memberIdent = member
.findFirstToken(TokenTypes.IDENT);
assert (memberIdent != null);
assert memberIdent != null;
final String identifierStr = memberIdent.getText();
if (!isAnyMatched(excludes, identifierStr)) {
memberFound = true;
Expand Down

0 comments on commit fdb9e26

Please sign in to comment.