if (token.equals(vertex) && vertex.getLineNo() <= endLineNumber) { result.add(vertex);
/** * Collects all tokens of specific type starting with the current ast node. * @param ast ast node. * @param tokenType token type. * @return a set of all tokens of specific type starting with the current ast node. */ private static Set<DetailAST> getAllTokensOfType(DetailAST ast, int tokenType) { DetailAST vertex = ast; final Set<DetailAST> result = new HashSet<>(); final Deque<DetailAST> stack = new ArrayDeque<>(); while (vertex != null || !stack.isEmpty()) { if (!stack.isEmpty()) { vertex = stack.pop(); } while (vertex != null) { if (vertex.getType() == tokenType && !vertex.equals(ast)) { result.add(vertex); } if (vertex.getNextSibling() != null) { stack.push(vertex.getNextSibling()); } vertex = vertex.getFirstChild(); } } return result; }
/** * Checks whether given instance member has final modifier. * @param instanceMember an instance member of a class. * @return true if given instance member has final modifier. */ public boolean hasFinalField(final DetailAST instanceMember) { boolean result = false; for (DetailAST member : instanceMembers) { final DetailAST mods = member.getParent().findFirstToken(TokenTypes.MODIFIERS); final boolean finalMod = mods.findFirstToken(TokenTypes.FINAL) != null; if (finalMod && member.equals(instanceMember)) { result = true; break; } } return result; }
/** * Gets the next node of a syntactical tree (child of a current node or * sibling of a current node, or sibling of a parent of a current node). * @param currentNodeAst Current node in considering * @param subTreeRootAst SubTree root * @return Current node after bypassing, if current node reached the root of a subtree * method returns null */ private static DetailAST getNextSubTreeNode(DetailAST currentNodeAst, DetailAST subTreeRootAst) { DetailAST currentNode = currentNodeAst; DetailAST toVisitAst = currentNode.getFirstChild(); while (toVisitAst == null) { toVisitAst = currentNode.getNextSibling(); if (toVisitAst == null) { if (currentNode.getParent().equals(subTreeRootAst)) { break; } currentNode = currentNode.getParent(); } } return toVisitAst; }
/** * Checks line for end of scope. Handles occurrences of close braces and close parenthesis on * the same line. * * @param lastAnnotationNode the last node of the annotation * @param node the node indicating where to begin checking * @return true if all the nodes up to the last annotation node are end of scope nodes * false otherwise */ private static boolean isEndOfScope(final DetailAST lastAnnotationNode, final DetailAST node) { DetailAST checkNode = node; boolean endOfScope = true; while (endOfScope && !checkNode.equals(lastAnnotationNode)) { switch (checkNode.getType()) { case TokenTypes.RCURLY: case TokenTypes.RBRACK: while (checkNode.getNextSibling() == null) { checkNode = checkNode.getParent(); } checkNode = checkNode.getNextSibling(); break; default: endOfScope = false; } } return endOfScope; }
/** * Checks if Ast node contains given element. * @param parent * Node of AST. * @param ast * Ast element which is checked for content in Ast node. * @return true if Ast element was found in Ast node, otherwise - false. */ private static boolean isChild(DetailAST parent, DetailAST ast) { boolean isChild = false; final ASTEnumeration astList = parent.findAllPartial(ast); while (astList.hasMoreNodes()) { final DetailAST astNode = (DetailAST) astList.nextNode(); DetailAST astParent = astNode.getParent(); while (astParent != null) { if (astParent.equals(parent) && astParent.getLineNo() == parent.getLineNo()) { isChild = true; break; } astParent = astParent.getParent(); } } return isChild; }
/** * Collects all tokens of specific type starting with the current ast node. * @param ast ast node. * @param tokenType token type. * @return a set of all tokens of specific type starting with the current ast node. */ private static Set<DetailAST> getAllTokensOfType(DetailAST ast, int tokenType) { DetailAST vertex = ast; final Set<DetailAST> result = new HashSet<>(); final Deque<DetailAST> stack = new ArrayDeque<>(); while (vertex != null || !stack.isEmpty()) { if (!stack.isEmpty()) { vertex = stack.pop(); } while (vertex != null) { if (vertex.getType() == tokenType && !vertex.equals(ast)) { result.add(vertex); } if (vertex.getNextSibling() != null) { stack.push(vertex.getNextSibling()); } vertex = vertex.getFirstChild(); } } return result; }
if (token.equals(vertex) && vertex.getLineNo() <= endLineNumber) { result.add(vertex);
/** * Gets the depth level of given "return" statement. There are few supported * coding blocks when depth counting: "if-else", "for", "while"/"do-while" * and "switch". * @param methodDefNode * a DetailAST node that points to the current method`s definition. * @param returnStmtNode * given "return" statement node. * @return the depth of given */ private static int getDepth(DetailAST methodDefNode, DetailAST returnStmtNode) { int result = 0; DetailAST curNode = returnStmtNode; while (!curNode.equals(methodDefNode)) { curNode = curNode.getParent(); final int type = curNode.getType(); if (type == TokenTypes.LITERAL_IF || type == TokenTypes.LITERAL_SWITCH || type == TokenTypes.LITERAL_FOR || type == TokenTypes.LITERAL_DO || type == TokenTypes.LITERAL_WHILE || type == TokenTypes.LITERAL_TRY) { result++; } } return result; }
/** * Checks whether given instance member has final modifier. * @param instanceMember an instance member of a class. * @return true if given instance member has final modifier. */ public boolean hasFinalField(final DetailAST instanceMember) { boolean result = false; for (DetailAST member : instanceMembers) { final DetailAST mods = member.getParent().findFirstToken(TokenTypes.MODIFIERS); final boolean finalMod = mods.findFirstToken(TokenTypes.FINAL) != null; if (finalMod && member.equals(instanceMember)) { result = true; break; } } return result; }
/** * Gets the next node of a syntactical tree (child of a current node or * sibling of a current node, or sibling of a parent of a current node). * @param currentNodeAst Current node in considering * @param subTreeRootAst SubTree root * @return Current node after bypassing, if current node reached the root of a subtree * method returns null */ private static DetailAST getNextSubTreeNode(DetailAST currentNodeAst, DetailAST subTreeRootAst) { DetailAST currentNode = currentNodeAst; DetailAST toVisitAst = currentNode.getFirstChild(); while (toVisitAst == null) { toVisitAst = currentNode.getNextSibling(); if (toVisitAst == null) { if (currentNode.getParent().equals(subTreeRootAst)) { break; } currentNode = currentNode.getParent(); } } return toVisitAst; }
/** * Gets the next node of a syntactical tree (child of a current node or sibling of a current * node, or sibling of a parent of a current node). * * @param node * Current node in considering * @param subTreeRoot * The root node of the subtree. Can be a top level root node * @return Current node after bypassing, if current node reached the root of a subtree * method returns null */ public static DetailAST getNextSubTreeNode(DetailAST node, DetailAST subTreeRoot) { DetailAST toVisitAst = node.getFirstChild(); DetailAST currentNode = node; while (toVisitAst == null) { toVisitAst = currentNode.getNextSibling(); if (toVisitAst == null) { if (currentNode.getParent().equals(subTreeRoot)) { break; } currentNode = currentNode.getParent(); } } return toVisitAst; }
/** * Checks line for end of scope. Handles occurrences of close braces and close parenthesis on * the same line. * * @param lastAnnotationNode the last node of the annotation * @param node the node indicating where to begin checking * @return true if all the nodes up to the last annotation node are end of scope nodes * false otherwise */ private static boolean isEndOfScope(final DetailAST lastAnnotationNode, final DetailAST node) { DetailAST checkNode = node; boolean endOfScope = true; while (endOfScope && !checkNode.equals(lastAnnotationNode)) { switch (checkNode.getType()) { case TokenTypes.RCURLY: case TokenTypes.RBRACK: while (checkNode.getNextSibling() == null) { checkNode = checkNode.getParent(); } checkNode = checkNode.getNextSibling(); break; default: endOfScope = false; } } return endOfScope; }
/** * Checks if Ast node contains given element. * @param parent * Node of AST. * @param ast * Ast element which is checked for content in Ast node. * @return true if Ast element was found in Ast node, otherwise - false. */ private static boolean isChild(DetailAST parent, DetailAST ast) { boolean isChild = false; final ASTEnumeration astList = parent.findAllPartial(ast); while (astList.hasMoreNodes()) { final DetailAST astNode = (DetailAST) astList.nextNode(); DetailAST astParent = astNode.getParent(); while (astParent != null) { if (astParent.equals(parent) && astParent.getLineNo() == parent.getLineNo()) { isChild = true; break; } astParent = astParent.getParent(); } } return isChild; }
DetailAST temp = currentNode; while (!temp.equals(currentCatchAST) && temp.getType() != TokenTypes.ASSIGN) { temp = temp.getParent();