Java Code Examples for com.puppycrawl.tools.checkstyle.api.TokenTypes#LITERAL_ELSE

The following examples show how to use com.puppycrawl.tools.checkstyle.api.TokenTypes#LITERAL_ELSE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableMaker.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * process the given SymTabAST as an if block
 *
 * @param tree the SymTabAST to process
 * @return <code>void</code>
 * @see #makeBlock(SymTabAST)
 * @see #walkTree(SymTabAST, boolean)
 * @see #processElse(SymTabAST)
 */
public void processIf(SymTabAST tree) {
  BlockDef block = makeBlock(tree);

  SymTabAST expr = tree.findFirstToken(TokenTypes.EXPR);
  SymTabAST ifBranch = (SymTabAST)expr.getNextSibling();
  // handle Checkstyle grammar
  if (ifBranch.getType() == TokenTypes.RPAREN) {
      ifBranch = (SymTabAST) ifBranch.getNextSibling();
  }
  SymTabAST elseBranch = (SymTabAST)ifBranch.getNextSibling();
  // handle Checkstyle grammar
  if ((elseBranch != null) && (elseBranch.getType() == TokenTypes.SEMI)) {
          elseBranch = (SymTabAST) elseBranch.getNextSibling();
  }
  if ((elseBranch != null) && (elseBranch.getType() == TokenTypes.LITERAL_ELSE)) {
      elseBranch = (SymTabAST) elseBranch.getFirstChild();
  }

  symbolTable.pushScope( block );
  walkTree(expr, false);
  walkTree(ifBranch, false);
  symbolTable.popScope();

  processElse(elseBranch);
}
 
Example 2
Source File: TableMaker.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * process the given SymTabAST as an if block
 *
 * @param tree the SymTabAST to process
 * @return <code>void</code>
 * @see #makeBlock(SymTabAST)
 * @see #walkTree(SymTabAST, boolean)
 * @see #processElse(SymTabAST)
 */
public void processIf(SymTabAST tree) {
  BlockDef block = makeBlock(tree);

  SymTabAST expr = tree.findFirstToken(TokenTypes.EXPR);
  SymTabAST ifBranch = (SymTabAST)expr.getNextSibling();
  // handle Checkstyle grammar
  if (ifBranch.getType() == TokenTypes.RPAREN) {
      ifBranch = (SymTabAST) ifBranch.getNextSibling();
  }
  SymTabAST elseBranch = (SymTabAST)ifBranch.getNextSibling();
  // handle Checkstyle grammar
  if ((elseBranch != null) && (elseBranch.getType() == TokenTypes.SEMI)) {
          elseBranch = (SymTabAST) elseBranch.getNextSibling();
  }
  if ((elseBranch != null) && (elseBranch.getType() == TokenTypes.LITERAL_ELSE)) {
      elseBranch = (SymTabAST) elseBranch.getFirstChild();
  }

  symbolTable.pushScope( block );
  walkTree(expr, false);
  walkTree(ifBranch, false);
  symbolTable.popScope();

  processElse(elseBranch);
}
 
Example 3
Source File: IfBracesCheck.java    From requestor with Apache License 2.0 5 votes vote down vote up
@Override
public int[] getDefaultTokens() {
    return new int[]{
            TokenTypes.LITERAL_ELSE,
            TokenTypes.LITERAL_IF,
    };
}
 
Example 4
Source File: Resolver.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * processes an if statement and resolves references in it
 *
 * @param block the <code>BlockDef</code> to process
 */
private void handleIf(BlockDef block) {
    SymTabAST node = block.getTreeNode();

    SymTabAST conditional =
        (node.findFirstToken(TokenTypes.EXPR));
    resolveExpression(conditional, block, null, true);

    SymTabAST body = (SymTabAST) conditional.getNextSibling();
    // Handle Checkstyle grammar
    if (body.getType() == TokenTypes.RPAREN) {
        body = (SymTabAST) body.getNextSibling();
    }
    if (body != null) {
    	if (body.getType() == TokenTypes.SLIST) {
        	handleSList(body, block);
    	}
    	else {
        	resolveExpression(body, block, null, true);
    	}

     SymTabAST elseBody = (SymTabAST) body.getNextSibling();
     //handle Checkstyle grammar
     while ((elseBody != null)
         && (elseBody.getType() != TokenTypes.LITERAL_ELSE)) {
         elseBody = (SymTabAST) elseBody.getNextSibling();
     }
     /*
      if (elseBody != null && elseBody.getType() == TokenTypes.SLIST) {
          handleSList(elseBody, block);
      }else{
          resolveExpression(elseBody, block, null, true);
      }
      */
     if (elseBody != null) {
         elseBody = (SymTabAST) elseBody.getFirstChild();
     }
     if (elseBody != null) {
         resolveExpression(elseBody, block.getParentScope(), null, true);
     }
 }
}
 
Example 5
Source File: Resolver.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * processes an if statement and resolves references in it
 *
 * @param block the <code>BlockDef</code> to process
 */
private void handleIf(BlockDef block) {
    SymTabAST node = block.getTreeNode();

    SymTabAST conditional =
        (node.findFirstToken(TokenTypes.EXPR));
    resolveExpression(conditional, block, null, true);

    SymTabAST body = (SymTabAST) conditional.getNextSibling();
    // Handle Checkstyle grammar
    if (body.getType() == TokenTypes.RPAREN) {
        body = (SymTabAST) body.getNextSibling();
    }
    if (body != null) {
    	if (body.getType() == TokenTypes.SLIST) {
        	handleSList(body, block);
    	}
    	else {
        	resolveExpression(body, block, null, true);
    	}

     SymTabAST elseBody = (SymTabAST) body.getNextSibling();
     //handle Checkstyle grammar
     while ((elseBody != null)
         && (elseBody.getType() != TokenTypes.LITERAL_ELSE)) {
         elseBody = (SymTabAST) elseBody.getNextSibling();
     }
     /*
      if (elseBody != null && elseBody.getType() == TokenTypes.SLIST) {
          handleSList(elseBody, block);
      }else{
          resolveExpression(elseBody, block, null, true);
      }
      */
     if (elseBody != null) {
         elseBody = (SymTabAST) elseBody.getFirstChild();
     }
     if (elseBody != null) {
         resolveExpression(elseBody, block.getParentScope(), null, true);
     }
 }
}
 
Example 6
Source File: IfBracesCheck.java    From requestor with Apache License 2.0 4 votes vote down vote up
@Override
public void visitToken(DetailAST aAST) {
    final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST);

    if (aAST.getType() == TokenTypes.LITERAL_ELSE) {
        // If we have an else, it must have braces, except it is an "else if" (then the if must have braces).
        DetailAST ifToken = aAST.findFirstToken(TokenTypes.LITERAL_IF);

        if (ifToken == null) {
            // This is an simple else, it must have brace.
            if (slistAST == null) {
                log(aAST.getLineNo(), "ifBracesElse", aAST.getText());
            }
        } else {
            // This is an "else if", the if must have braces.
            if (ifToken.findFirstToken(TokenTypes.SLIST) == null) {
                log(aAST.getLineNo(), "ifBracesConditional", ifToken.getText(), aAST.getText() + " " + ifToken
                        .getText());
            }
        }
    } else if (aAST.getType() == TokenTypes.LITERAL_IF) {
        // If the if uses braces, nothing as to be checked.
        if (slistAST != null) {
            return;
        }

        // We have an if, we need to check if it has no conditionnal structure as direct child.
        final int[] conditionals = {
                TokenTypes.LITERAL_DO,
                TokenTypes.LITERAL_ELSE,
                TokenTypes.LITERAL_FOR,
                TokenTypes.LITERAL_IF,
                TokenTypes.LITERAL_WHILE,
                TokenTypes.LITERAL_SWITCH,
        };

        for (int conditional : conditionals) {
            DetailAST conditionalAST = aAST.findFirstToken(conditional);

            if (conditionalAST != null) {
                log(aAST.getLineNo(), "ifBracesConditional", aAST.getText(), conditionalAST.getText());

                // Let's trigger this only once.
                return;
            }
        }
    }
}