@Override public boolean isValidOn(final DetailAST ast) { final int astType = ast.getType(); final DetailAST returnType = ast.findFirstToken(TokenTypes.TYPE); return astType == TokenTypes.METHOD_DEF && returnType.getFirstChild().getType() != TokenTypes.LITERAL_VOID; }
/** * Checks whether type definition is in canonical form. * @param type type definition token. * @return true if type definition is in canonical form. */ private static boolean isCanonicalName(DetailAST type) { return type.getFirstChild().getType() == TokenTypes.DOT; }
/** * Checks if a variable is the loop's one. * @param variableDef variable definition. * @return true if a variable is the loop's one. */ private static boolean isForLoopVariable(DetailAST variableDef) { final int parentType = variableDef.getParent().getType(); return parentType == TokenTypes.FOR_INIT || parentType == TokenTypes.FOR_EACH_CLAUSE; }
/** * Checks if definition body is empty. * @param lcurly left curly. * @return true if definition body is empty. */ private static boolean isEmptyBody(DetailAST lcurly) { boolean result = false; if (lcurly.getParent().getType() == TokenTypes.OBJBLOCK) { if (lcurly.getNextSibling().getType() == TokenTypes.RCURLY) { result = true; } } else if (lcurly.getFirstChild().getType() == TokenTypes.RCURLY) { result = true; } return result; }
/** * Retrieves the first modifier that is not an annotation. * @param modifiers The ast to examine. * @return The first modifier or {@code null} if none found. */ private static DetailAST getFirstModifierAst(DetailAST modifiers) { DetailAST modifier = modifiers.getFirstChild(); while (modifier != null && modifier.getType() == TokenTypes.ANNOTATION) { modifier = modifier.getNextSibling(); } return modifier; }
/** * Checks if the given {@code DetailAST} node is matching line number, column number and token * type. * @param ast {@code DetailAST} ast element * @return true if the given {@code DetailAST} node is matching */ private boolean isMatchingByLineAndColumnAndTokenType(DetailAST ast) { return ast.getLineNo() == lineNumber && expandedTabColumn(ast) == columnNumber && (tokenType == 0 || tokenType == ast.getType()); }
/** * Checks whether {@link TokenTypes#RPAREN} is a closing paren * of a {@link TokenTypes#TYPECAST}. * @param ast of a {@link TokenTypes#RPAREN} to check. * @return true if ast is a closing paren of a {@link TokenTypes#TYPECAST}. */ private static boolean isInTypecast(DetailAST ast) { boolean result = false; if (ast.getParent().getType() == TokenTypes.TYPECAST) { final DetailAST firstRparen = ast.getParent().findFirstToken(TokenTypes.RPAREN); if (firstRparen.getLineNo() == ast.getLineNo() && firstRparen.getColumnNo() == ast.getColumnNo()) { result = true; } } return result; }
@Override public void visitToken(DetailAST arrayInit) { final DetailAST rcurly = arrayInit.findFirstToken(TokenTypes.RCURLY); final DetailAST previousSibling = rcurly.getPreviousSibling(); if (arrayInit.getLineNo() != rcurly.getLineNo() && arrayInit.getChildCount() != 1 && rcurly.getLineNo() != previousSibling.getLineNo() && arrayInit.getLineNo() != previousSibling.getLineNo() && previousSibling.getType() != TokenTypes.COMMA) { log(rcurly.getLineNo(), MSG_KEY); } }
/** * Determines if this 'if' statement is part of an 'else' clause * and on the same line. * * @return true if this 'if' is part of an 'else', false otherwise */ private boolean isIfAfterElse() { // check if there is an 'else' and an 'if' on the same line final DetailAST parent = getMainAst().getParent(); return parent.getType() == TokenTypes.LITERAL_ELSE && parent.getLineNo() == getMainAst().getLineNo(); }
/** * Checks whether a parameter is a receiver. * * @param parameterDefAst the parameter node. * @return true if the parameter is a receiver. */ public static boolean isReceiverParameter(DetailAST parameterDefAst) { return parameterDefAst.getType() == TokenTypes.PARAMETER_DEF && parameterDefAst.findFirstToken(TokenTypes.IDENT) == null; }
/** * Finds the class body of the first class in the DetailAST. * @param top AST to find the class body * @return OBJBLOCK token if found; {@code null} otherwise */ private static DetailAST getClassBody(DetailAST top) { DetailAST ast = top; while (ast != null && ast.getType() != TokenTypes.CLASS_DEF) { ast = ast.getNextSibling(); } DetailAST objBlock = null; if (ast != null) { objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK); } return objBlock; }
@Test public void testAppendHiddenSingleLineCommentNodes2() throws Exception { final DetailAST root = JavaParser.parseFile(new File(getPath("InputJavaParserHiddenComments2.java")), JavaParser.Options.WITH_COMMENTS); final Optional<DetailAST> singleLineComment = TestUtil.findTokenInAstByPredicate(root, ast -> ast.getType() == TokenTypes.SINGLE_LINE_COMMENT); assertTrue("Single line comment should be present", singleLineComment.isPresent()); final DetailAST comment = singleLineComment.get(); assertEquals("Unexpected line number", 1, comment.getLineNo()); assertEquals("Unexpected column number", 4, comment.getColumnNo()); assertEquals("Unexpected comment content", "//", comment.getText()); final DetailAST commentContent = comment.getFirstChild(); assertEquals("Unexpected token type", TokenTypes.COMMENT_CONTENT, commentContent.getType()); assertEquals("Unexpected line number", 1, commentContent.getLineNo()); assertEquals("Unexpected column number", 6, commentContent.getColumnNo()); assertTrue("Unexpected comment content", commentContent.getText().startsWith(" indented comment")); }
@Override public void visitToken(DetailAST ast) { // Strange logic in this method to guard against checking RPAREN tokens // that are not associated with a TYPECAST token. if (ast.getType() == TokenTypes.TYPECAST) { processLeft(ast); } else if (ast.getParent().getType() == TokenTypes.TYPECAST && ast.getParent().findFirstToken(TokenTypes.RPAREN) == ast) { processRight(ast); } }
/** * Collects references made by IDENT. * @param ast the IDENT node to process */ private void processIdent(DetailAST ast) { final DetailAST parent = ast.getParent(); final int parentType = parent.getType(); if (parentType != TokenTypes.DOT && parentType != TokenTypes.METHOD_DEF || parentType == TokenTypes.DOT && ast.getNextSibling() != null) { referenced.add(ast.getText()); } }
@Override public void visitToken(DetailAST ast) { final DetailAST whitespaceFollowedAst = getWhitespaceFollowedNode(ast); if (whitespaceFollowedAst.getNextSibling() == null || whitespaceFollowedAst.getNextSibling().getType() != TokenTypes.ANNOTATIONS) { final int whitespaceColumnNo = getPositionAfter(whitespaceFollowedAst); final int whitespaceLineNo = whitespaceFollowedAst.getLineNo(); if (hasTrailingWhitespace(ast, whitespaceColumnNo, whitespaceLineNo)) { log(ast, MSG_KEY, whitespaceFollowedAst.getText()); } } }
/** * Get next sibling node skipping any comment nodes. * @param node current node * @return next sibling */ private static DetailAST getNextSiblingSkipComments(DetailAST node) { DetailAST result = node.getNextSibling(); while (result.getType() == TokenTypes.SINGLE_LINE_COMMENT || result.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) { result = result.getNextSibling(); } return result; }
/** * Checks if catch block is empty or contains only comments. * @param catchAst {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH} * @return true if catch block is empty. */ private static boolean isEmptyCatchBlock(DetailAST catchAst) { boolean result = true; final DetailAST slistToken = catchAst.findFirstToken(TokenTypes.SLIST); DetailAST catchBlockStmt = slistToken.getFirstChild(); while (catchBlockStmt.getType() != TokenTypes.RCURLY) { if (catchBlockStmt.getType() != TokenTypes.SINGLE_LINE_COMMENT && catchBlockStmt.getType() != TokenTypes.BLOCK_COMMENT_BEGIN) { result = false; break; } catchBlockStmt = catchBlockStmt.getNextSibling(); } return result; }
/** * Skips over an inner assign portion of an argument expression. * @param currentAST current token in the argument expression * @return the next relevant token */ private static DetailAST skipVariableAssign(final DetailAST currentAST) { DetailAST result = currentAST; if (currentAST.getType() == TokenTypes.ASSIGN && currentAST.getFirstChild().getType() == TokenTypes.IDENT) { result = currentAST.getFirstChild().getNextSibling(); } return result; }
/** * Checks if {@link TokenTypes#BOR binary OR} is applied to exceptions * in * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.20"> * multi-catch</a> (pipe-syntax). * @param binaryOr {@link TokenTypes#BOR binary or} * @return true if binary or is applied to exceptions in multi-catch. */ private static boolean isPipeOperator(DetailAST binaryOr) { return binaryOr.getParent().getType() == TokenTypes.TYPE; }
/** * Checks that return type is {@code void}. * @param method the METHOD_DEF node * @return true if check passed, false otherwise */ private static boolean checkType(DetailAST method) { final DetailAST type = method.findFirstToken(TokenTypes.TYPE).getFirstChild(); return type.getType() == TokenTypes.LITERAL_VOID; }