本文整理汇总了Java中com.puppycrawl.tools.checkstyle.api.DetailAST.getLastChild方法的典型用法代码示例。如果您正苦于以下问题:Java DetailAST.getLastChild方法的具体用法?Java DetailAST.getLastChild怎么用?Java DetailAST.getLastChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.puppycrawl.tools.checkstyle.api.DetailAST
的用法示例。
在下文中一共展示了DetailAST.getLastChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkCheckClosingParens
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Checks to see if the closing parenthesis are present if required or
* prohibited.
*
* @param ast the annotation token
*/
private void checkCheckClosingParens(final DetailAST ast) {
if (closingParens != ClosingParens.IGNORE) {
final DetailAST paren = ast.getLastChild();
final boolean parenExists = paren.getType() == TokenTypes.RPAREN;
if (closingParens == ClosingParens.ALWAYS
&& !parenExists) {
log(ast.getLineNo(), MSG_KEY_ANNOTATION_PARENS_MISSING);
}
else if (closingParens == ClosingParens.NEVER
&& parenExists
&& !ast.branchContains(TokenTypes.EXPR)
&& !ast.branchContains(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR)
&& !ast.branchContains(TokenTypes.ANNOTATION_ARRAY_INIT)) {
log(ast.getLineNo(), MSG_KEY_ANNOTATION_PARENS_PRESENT);
}
}
}
示例2: isSingleLineIf
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Checks if current if statement is single-line statement, e.g.:
* <p>
* {@code
* if (obj.isValid()) return true;
* }
* </p>
* @param literalIf {@link TokenTypes#LITERAL_IF if statement}.
* @return true if current if statement is single-line statement.
*/
private static boolean isSingleLineIf(DetailAST literalIf) {
boolean result = false;
if (literalIf.getParent().getType() == TokenTypes.SLIST) {
final DetailAST literalIfLastChild = literalIf.getLastChild();
final DetailAST block;
if (literalIfLastChild.getType() == TokenTypes.LITERAL_ELSE) {
block = literalIfLastChild.getPreviousSibling();
}
else {
block = literalIfLastChild;
}
final DetailAST ifCondition = literalIf.findFirstToken(TokenTypes.EXPR);
result = ifCondition.getLineNo() == block.getLineNo();
}
return result;
}
示例3: collectFirstNodes
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Finds first nodes on line and puts them into Map.
*
* @param firstNode First node to start examining.
* @param lastNode Last node to examine inclusively.
* @return NavigableMap which contains lines numbers as a key and first
* nodes on lines as a values.
*/
private NavigableMap<Integer, DetailAST> collectFirstNodes(DetailAST firstNode,
DetailAST lastNode) {
final NavigableMap<Integer, DetailAST> result = new TreeMap<Integer, DetailAST>();
result.put(firstNode.getLineNo(), firstNode);
DetailAST curNode = firstNode.getFirstChild();
while (curNode != lastNode) {
if (curNode.getType() == TokenTypes.OBJBLOCK
|| curNode.getType() == TokenTypes.SLIST) {
curNode = curNode.getLastChild();
}
final DetailAST firstTokenOnLine = result.get(curNode.getLineNo());
if (firstTokenOnLine == null
|| expandedTabsColumnNo(firstTokenOnLine) >= expandedTabsColumnNo(curNode)) {
result.put(curNode.getLineNo(), curNode);
}
curNode = getNextCurNode(curNode);
}
return result;
}
示例4: getCommentFirstLine
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Gets the first line of comment in catch block. If comment is single-line -
* returns it fully, else if comment is multi-line - returns the first line.
* @param catchAst {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH}
* @return the first line of comment in catch block, "" if no comment was found.
*/
private static String getCommentFirstLine(DetailAST catchAst) {
final DetailAST slistToken = catchAst.getLastChild();
final DetailAST firstElementInBlock = slistToken.getFirstChild();
String commentContent = "";
if (firstElementInBlock.getType() == TokenTypes.SINGLE_LINE_COMMENT) {
commentContent = firstElementInBlock.getFirstChild().getText();
}
else if (firstElementInBlock.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
commentContent = firstElementInBlock.getFirstChild().getText();
final String[] lines = commentContent.split(System.getProperty("line.separator"));
for (String line : lines) {
if (!line.isEmpty()) {
commentContent = line;
break;
}
}
}
return commentContent;
}
示例5: hasEmptyImplementation
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Checks whether a method has only comments in the body (has an empty implementation).
* Method is OK if its implementation is empty.
* @param ast method definition token.
* @return true if a method has only comments in the body.
*/
private static boolean hasEmptyImplementation(DetailAST ast) {
boolean hasEmptyBody = true;
final DetailAST methodImplOpenBrace = ast.findFirstToken(TokenTypes.SLIST);
final DetailAST methodImplCloseBrace = methodImplOpenBrace.getLastChild();
final Predicate<DetailAST> predicate = new Predicate<DetailAST>() {
@Override
public boolean test(DetailAST currentNode) {
return currentNode != methodImplCloseBrace
&& !TokenUtils.isCommentType(currentNode.getType());
}
};
final Optional<DetailAST> methodBody =
TokenUtils.findFirstTokenByPredicate(methodImplOpenBrace, predicate);
if (methodBody.isPresent()) {
hasEmptyBody = false;
}
return hasEmptyBody;
}
示例6: getDetailsForLoops
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Collects validation details for loops' tokens.
* @param ast a {@code DetailAST} value
* @return an object containing all details to make a validation
*/
private static Details getDetailsForLoops(DetailAST ast) {
DetailAST rcurly = null;
final DetailAST lcurly;
final DetailAST nextToken;
final int tokenType = ast.getType();
if (tokenType == TokenTypes.LITERAL_DO) {
nextToken = ast.findFirstToken(TokenTypes.DO_WHILE);
lcurly = ast.findFirstToken(TokenTypes.SLIST);
if (lcurly != null) {
rcurly = lcurly.getLastChild();
}
}
else {
lcurly = ast.findFirstToken(TokenTypes.SLIST);
if (lcurly != null) {
// SLIST could be absent in code like "while(true);"
rcurly = lcurly.getLastChild();
}
nextToken = getNextToken(ast);
}
return new Details(lcurly, rcurly, nextToken, false);
}
示例7: checkSlist
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Checks if a given SLIST terminated by return, throw or,
* if allowed break, continue.
* @param slistAst SLIST to check
* @param useBreak should we consider break as terminator.
* @param useContinue should we consider continue as terminator.
* @return true if SLIST is terminated.
*/
private boolean checkSlist(final DetailAST slistAst, boolean useBreak,
boolean useContinue) {
DetailAST lastStmt = slistAst.getLastChild();
if (lastStmt.getType() == TokenTypes.RCURLY) {
lastStmt = lastStmt.getPreviousSibling();
}
return lastStmt != null
&& isTerminated(lastStmt, useBreak, useContinue);
}
示例8: checkTry
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Checks if a given try/catch/finally block terminated by return, throw or,
* if allowed break, continue.
* @param ast loop to check
* @param useBreak should we consider break as terminator.
* @param useContinue should we consider continue as terminator.
* @return true if try/catch/finally block is terminated.
*/
private boolean checkTry(final DetailAST ast, boolean useBreak,
boolean useContinue) {
final DetailAST finalStmt = ast.getLastChild();
boolean isTerminated = false;
if (finalStmt.getType() == TokenTypes.LITERAL_FINALLY) {
isTerminated = isTerminated(finalStmt.findFirstToken(TokenTypes.SLIST),
useBreak, useContinue);
}
if (!isTerminated) {
DetailAST firstChild = ast.getFirstChild();
if (firstChild.getType() == TokenTypes.RESOURCE_SPECIFICATION) {
firstChild = firstChild.getNextSibling();
}
isTerminated = isTerminated(firstChild,
useBreak, useContinue);
DetailAST catchStmt = ast.findFirstToken(TokenTypes.LITERAL_CATCH);
while (catchStmt != null
&& isTerminated
&& catchStmt.getType() == TokenTypes.LITERAL_CATCH) {
final DetailAST catchBody =
catchStmt.findFirstToken(TokenTypes.SLIST);
isTerminated = isTerminated(catchBody, useBreak, useContinue);
catchStmt = catchStmt.getNextSibling();
}
}
return isTerminated;
}
示例9: getFirstNodeInsideSwitchBlock
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Gets first Ast node inside SWITCH block if variable usage is met
* only inside the block (not in its declaration!).
* @param block
* Ast node represents SWITCH block.
* @param variable
* Variable which is checked for content in block.
* @return If variable usage is met only inside the block
* (not in its declaration!) than return the first Ast node
* of this block, otherwise - null.
*/
private static DetailAST getFirstNodeInsideSwitchBlock(
DetailAST block, DetailAST variable) {
DetailAST currentNode = block
.findFirstToken(TokenTypes.CASE_GROUP);
final List<DetailAST> variableUsageExpressions =
new ArrayList<DetailAST>();
// Checking variable usage inside all CASE blocks.
while (currentNode.getType() == TokenTypes.CASE_GROUP) {
final DetailAST lastNodeInCaseGroup =
currentNode.getLastChild();
if (isChild(lastNodeInCaseGroup, variable)) {
variableUsageExpressions.add(lastNodeInCaseGroup);
}
currentNode = currentNode.getNextSibling();
}
// If variable usage exists in several related blocks, then
// firstNodeInsideBlock = null, otherwise if variable usage exists
// only inside one block, then get node from
// variableUsageExpressions.
DetailAST firstNodeInsideBlock = null;
if (variableUsageExpressions.size() == 1) {
firstNodeInsideBlock = variableUsageExpressions.get(0);
}
return firstNodeInsideBlock;
}
示例10: isVariableInOperatorExpr
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Checks if variable is in operator declaration. For instance:
* <pre>
* boolean b = true;
* if (b) {...}
* </pre>
* Variable 'b' is in declaration of operator IF.
* @param operator
* Ast node which represents operator.
* @param variable
* Variable which is checked for content in operator.
* @return true if operator contains variable in its declaration, otherwise
* - false.
*/
private static boolean isVariableInOperatorExpr(
DetailAST operator, DetailAST variable) {
boolean isVarInOperatorDeclaration = false;
final DetailAST openingBracket =
operator.findFirstToken(TokenTypes.LPAREN);
// Get EXPR between brackets
DetailAST exprBetweenBrackets = openingBracket.getNextSibling();
// Look if variable is in operator expression
while (exprBetweenBrackets.getType() != TokenTypes.RPAREN) {
if (isChild(exprBetweenBrackets, variable)) {
isVarInOperatorDeclaration = true;
break;
}
exprBetweenBrackets = exprBetweenBrackets.getNextSibling();
}
// Variable may be met in ELSE declaration
// So, check variable usage in these declarations.
if (!isVarInOperatorDeclaration && operator.getType() == TokenTypes.LITERAL_IF) {
final DetailAST elseBlock = operator.getLastChild();
if (elseBlock.getType() == TokenTypes.LITERAL_ELSE) {
// Get IF followed by ELSE
final DetailAST firstNodeInsideElseBlock = elseBlock.getFirstChild();
if (firstNodeInsideElseBlock.getType() == TokenTypes.LITERAL_IF) {
isVarInOperatorDeclaration =
isVariableInOperatorExpr(firstNodeInsideElseBlock, variable);
}
}
}
return isVarInOperatorDeclaration;
}
示例11: visitToken
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Override
public void visitToken(DetailAST ast) {
final int type = ast.getType();
final DetailAST parent = ast.getParent();
if (type == TokenTypes.LAMBDA && isLambdaSingleParameterSurrounded(ast)) {
log(ast, MSG_LAMBDA, ast.getText());
}
else if (type != TokenTypes.ASSIGN
|| parent.getType() != TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) {
final boolean surrounded = isSurrounded(ast);
// An identifier surrounded by parentheses.
if (surrounded && type == TokenTypes.IDENT) {
parentToSkip = ast.getParent();
log(ast, MSG_IDENT, ast.getText());
}
// A literal (numeric or string) surrounded by parentheses.
else if (surrounded && isInTokenList(type, LITERALS)) {
parentToSkip = ast.getParent();
if (type == TokenTypes.STRING_LITERAL) {
log(ast, MSG_STRING,
chopString(ast.getText()));
}
else {
log(ast, MSG_LITERAL, ast.getText());
}
}
// The rhs of an assignment surrounded by parentheses.
else if (isInTokenList(type, ASSIGNMENTS)) {
assignDepth++;
final DetailAST last = ast.getLastChild();
if (last.getType() == TokenTypes.RPAREN) {
log(ast, MSG_ASSIGN);
}
}
}
}
示例12: getPrevStatementWhenCommentIsUnderCase
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Gets previous statement for comment which is placed immediately under case.
* @param parentStatement comment's parent statement.
* @return comment's previous statement or null if previous statement is absent.
*/
private static DetailAST getPrevStatementWhenCommentIsUnderCase(DetailAST parentStatement) {
DetailAST prevStmt = null;
final DetailAST prevBlock = parentStatement.getPreviousSibling();
if (prevBlock.getLastChild() != null) {
DetailAST blockBody = prevBlock.getLastChild().getLastChild();
if (blockBody.getType() == TokenTypes.SEMI) {
blockBody = blockBody.getPreviousSibling();
}
if (blockBody.getType() == TokenTypes.EXPR) {
if (isUsingOfObjectReferenceToInvokeMethod(blockBody)) {
prevStmt = findStartTokenOfMethodCallChain(blockBody);
}
else {
prevStmt = blockBody.getFirstChild().getFirstChild();
}
}
else {
if (blockBody.getType() == TokenTypes.SLIST) {
prevStmt = blockBody.getParent().getParent();
}
else {
prevStmt = blockBody;
}
}
if (isComment(prevStmt)) {
prevStmt = prevStmt.getNextSibling();
}
}
return prevStmt;
}
示例13: getDetailsForTryCatchFinally
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Collects validation details for LITERAL_TRY, LITERAL_CATCH, and LITERAL_FINALLY.
* @param ast a {@code DetailAST} value
* @return object containing all details to make a validation
*/
private static Details getDetailsForTryCatchFinally(DetailAST ast) {
boolean shouldCheckLastRcurly = false;
final DetailAST rcurly;
final DetailAST lcurly;
DetailAST nextToken;
final int tokenType = ast.getType();
if (tokenType == TokenTypes.LITERAL_TRY) {
if (ast.getFirstChild().getType() == TokenTypes.RESOURCE_SPECIFICATION) {
lcurly = ast.getFirstChild().getNextSibling();
}
else {
lcurly = ast.getFirstChild();
}
nextToken = lcurly.getNextSibling();
rcurly = lcurly.getLastChild();
if (nextToken == null) {
shouldCheckLastRcurly = true;
nextToken = getNextToken(ast);
}
}
else if (tokenType == TokenTypes.LITERAL_CATCH) {
nextToken = ast.getNextSibling();
lcurly = ast.getLastChild();
rcurly = lcurly.getLastChild();
if (nextToken == null) {
shouldCheckLastRcurly = true;
nextToken = getNextToken(ast);
}
}
else {
shouldCheckLastRcurly = true;
nextToken = getNextToken(ast);
lcurly = ast.getFirstChild();
rcurly = lcurly.getLastChild();
}
return new Details(lcurly, rcurly, nextToken, shouldCheckLastRcurly);
}
示例14: getDetailsForIfElse
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Collects validation details for LITERAL_IF and LITERAL_ELSE.
* @param ast a {@code DetailAST} value
* @return object containing all details to make a validation
*/
private static Details getDetailsForIfElse(DetailAST ast) {
boolean shouldCheckLastRcurly = false;
DetailAST rcurly = null;
final DetailAST lcurly;
DetailAST nextToken;
final int tokenType = ast.getType();
if (tokenType == TokenTypes.LITERAL_IF) {
nextToken = ast.findFirstToken(TokenTypes.LITERAL_ELSE);
if (nextToken == null) {
shouldCheckLastRcurly = true;
nextToken = getNextToken(ast);
lcurly = ast.getLastChild();
}
else {
lcurly = nextToken.getPreviousSibling();
}
if (lcurly.getType() == TokenTypes.SLIST) {
rcurly = lcurly.getLastChild();
}
}
else {
shouldCheckLastRcurly = true;
nextToken = getNextToken(ast);
lcurly = ast.getFirstChild();
if (lcurly.getType() == TokenTypes.SLIST) {
rcurly = lcurly.getLastChild();
}
}
return new Details(lcurly, rcurly, nextToken, shouldCheckLastRcurly);
}
示例15: isAnonymousClassDef
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Whether the AST is a definition of an anonymous class.
* @param ast the AST to process.
* @return true if the AST is a definition of an anonymous class.
*/
private static boolean isAnonymousClassDef(DetailAST ast) {
final DetailAST lastChild = ast.getLastChild();
return lastChild != null
&& lastChild.getType() == TokenTypes.OBJBLOCK;
}