Skip to content

Commit

Permalink
Issue sevntu-checkstyle#374: Fix SingleBreakOrContinue violations
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziza committed Jan 11, 2016
1 parent 1024957 commit 603ef9a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 95 deletions.
Expand Up @@ -596,37 +596,27 @@ private int getPositionInOrderDeclaration(final DetailAST ast)
if (currentRule.getClassMember() == ast.getType()
&& currentRule.getRegexp().matcher(modifiers).find())
{
if (currentRule.hasRule(ANNON_CLASS_FIELD_MACRO)) {
if (isAnonymousClassField(ast)) {
result = index;
break;
}
}
else if (currentRule.hasRule(GETTER_SETTER_MACRO)) {
if (currentRule.hasRule(ANNON_CLASS_FIELD_MACRO)
|| currentRule.hasRule(GETTER_SETTER_MACRO)
|| currentRule.hasRule(MAIN_METHOD_MACRO)) {

final String methodName = getIdentifier(ast);
final ClassDetail classDetail = classDetails.peek();
if (classDetail.containsGetter(methodName)
|| classDetail.containsSetter(methodName))
{
result = index;
break;
}
}
else if (currentRule.hasRule(MAIN_METHOD_MACRO)) {
if (isMainMethod(ast)) {

if (isAnonymousClassField(ast)
|| classDetail.containsGetter(methodName)
|| classDetail.containsSetter(methodName)
|| isMainMethod(ast)) {
result = index;
break;
}
}
else {
// if more than one rule matches current AST node, then keep first one
result = (result == -1) ? index : result;
if (ast.getType() == TokenTypes.METHOD_DEF
|| ast.getType() == TokenTypes.VARIABLE_DEF)
result = result == -1 ? index : result;
if (ast.getType() != TokenTypes.METHOD_DEF
&& ast.getType() != TokenTypes.VARIABLE_DEF)
{
// continue to find more specific rule
continue;
} else {
break;
}
}
Expand Down
Expand Up @@ -89,23 +89,17 @@ public void visitToken(DetailAST variableDefNode) {
* AST subtree to process.
*/
private static DetailAST getFirstChildTokenOfType(DetailAST rootToken, int tokenType) {

DetailAST resultNode = null;
DetailAST currentNode = rootToken.getFirstChild();
while (currentNode != null) {
if (currentNode.getType() == tokenType) {
resultNode = currentNode;
break;
}
DetailAST childNode = getFirstChildTokenOfType(currentNode, tokenType);
DetailAST resultNode = rootToken.getFirstChild();

if (resultNode != null
&& resultNode.getType() != tokenType) {
DetailAST childNode = getFirstChildTokenOfType(resultNode, tokenType);

if (childNode == null) {
currentNode = currentNode.getNextSibling();
}
else {
resultNode = childNode;
break;
resultNode = resultNode.getNextSibling();
}
}

return resultNode;
}

Expand Down
Expand Up @@ -555,22 +555,17 @@ private static boolean containsExceptionParameter(
{
boolean result = false;
DetailAST parameterAst = parametersAst.getFirstChild();

while (parameterAst != null) {
if (exceptionVariableName.equals(getIdentifier(parameterAst)))
{
if (exceptionVariableName.equals(getIdentifier(parameterAst))
|| isInstanceMethodCall(exceptionVariableName,
parameterAst.getFirstChild())) {
result = true;
break;
parameterAst = null;
}
else {
final DetailAST methodCallAst = parameterAst.getFirstChild();
if (isInstanceMethodCall(exceptionVariableName,
methodCallAst))
{
result = true;
break;
}
parameterAst = parameterAst.getNextSibling();
}
parameterAst = parameterAst.getNextSibling();
}
return result;
}
Expand Down
Expand Up @@ -164,26 +164,57 @@ private boolean addedUsingForbiddenImport(final String className,
boolean result = false;

for (String importText : importsList) {
if (importText.endsWith("*")) {
final String importTextWithoutAsterisk =
importText.substring(0, importText.length() - 1);
if (forbiddenClassNameAndPath.equals(
importTextWithoutAsterisk + className))
{
result = true;
break;
}
}
else if (importText.equals(forbiddenClassNameAndPath)
&& importText.endsWith(className))
{
if (isWildcardForbiddenImported(importText, forbiddenClassNameAndPath, className)
|| isForbiddenImported(importText, forbiddenClassNameAndPath, className)) {
result = true;
break;
}
}

return result;
}

/**
* Tests if the class with given className is imported with the forbidden
* import and false otherwise.
*
* @param importText
* - String representation of imports from the processed class.
* @param className
* - the name of the class to check.
* @param forbiddenClassNameAndPath
* - full name&path of the given forbidden class.
* @return true if the class with given className is imported with the
* forbidden import and false otherwise.
*/
private boolean isWildcardForbiddenImported(String importText, String forbiddenClassNameAndPath,
String className) {
final String importTextWithoutAsterisk =
importText.substring(0, importText.length() - 1);

return importText.endsWith("*")
&& forbiddenClassNameAndPath.equals(importTextWithoutAsterisk + className);
}

/**
* Tests if the class with given className is imported with the forbidden
* import and false otherwise.
*
* @param importText
* - String representation of imports from the processed class.
* @param className
* - the name of the class to check.
* @param forbiddenClassNameAndPath
* - full name&path of the given forbidden class.
* @return true if the class with given className is imported with the
* forbidden import and false otherwise.
*/
private boolean isForbiddenImported(String importText, String forbiddenClassNameAndPath,
String className) {
return importText.equals(forbiddenClassNameAndPath)
&& importText.endsWith(className);
}

/**
* Gets the class name from full (dotted) classPath.
* @param classNameAndPath
Expand Down
Expand Up @@ -378,39 +378,31 @@ private boolean isOverridableMethodCall(final DetailAST methodCallAST)
visitedMethodCalls.add(methodCallAST);

final String methodName = getMethodName(methodCallAST);
final DetailAST methodDef = getMethodDef(methodCallAST);

if (methodName != null) {
final DetailAST methodDef = getMethodDef(methodCallAST);
if (methodDef != null) {

if (hasModifier(methodDef, TokenTypes.LITERAL_STATIC)) {
// do nothing
}
else if (hasModifier(methodDef, TokenTypes.LITERAL_PRIVATE)
|| hasModifier(methodDef, TokenTypes.FINAL))
{
final List<DetailAST> methodCallsList = getMethodCallsList(
methodDef);
for (DetailAST curNode : methodCallsList) {
if (visitedMethodCalls.contains(curNode)) {
result = false;
break;
}
else if (isOverridableMethodCall(curNode)) {
result = true;
break;
}
if (methodName != null
&& methodDef != null)
{
if (hasModifier(methodDef, TokenTypes.LITERAL_STATIC)) {
// do nothing
}
else if (hasModifier(methodDef, TokenTypes.LITERAL_PRIVATE)
|| hasModifier(methodDef, TokenTypes.FINAL))
{
final List<DetailAST> methodCallsList = getMethodCallsList(
methodDef);
for (DetailAST curNode : methodCallsList) {
if (!visitedMethodCalls.contains(curNode)
&& isOverridableMethodCall(curNode)) {
result = true;
break;
}
}
else {
curOverridableMetName = methodName;
result = true;
}
}
}
else
{
result = false;
else {
curOverridableMetName = methodName;
result = true;
}
}
return result;
}
Expand Down Expand Up @@ -890,7 +882,7 @@ private static DetailAST getClassDef(DetailAST rootNode, String className)

while (curNode != null) {
DetailAST toVisit = curNode.getFirstChild();
while ((curNode != null) && (toVisit == null)) {
while (curNode != null && toVisit == null) {
toVisit = curNode.getNextSibling();
if (toVisit == null) {
curNode = curNode.getParent();
Expand All @@ -899,11 +891,8 @@ private static DetailAST getClassDef(DetailAST rootNode, String className)

curNode = toVisit;

if (curNode == null) {
break;
}

if (curNode.getType() == TokenTypes.CLASS_DEF
if (curNode != null
&& curNode.getType() == TokenTypes.CLASS_DEF
&& curNode.findFirstToken(TokenTypes.IDENT).getText()
.equals(className))
{
Expand Down

0 comments on commit 603ef9a

Please sign in to comment.