/** * Whether the ast is the first child of its parent. * @param ast the ast to check. * @return true if the ast is the first child of its parent. */ private static boolean isFirstChild(DetailAST ast) { return ast.getPreviousSibling() == null; }
/** * Checks that a token is preceded by a semi-colon. * @param ast the token to check * @return whether a token is preceded by a semi-colon */ private static boolean hasPrecedingSemiColon(DetailAST ast) { return ast.getPreviousSibling().getType() == TokenTypes.SEMI; }
/** * Retrieves the left side of a conditional. * * @param cond cond a conditional type * {@link TokenTypes#QUESTION QUESTION} * @return either the value * or another conditional */ private static DetailAST getCondLeft(final DetailAST cond) { final DetailAST colon = cond.findFirstToken(TokenTypes.COLON); return colon.getPreviousSibling(); }
/** * Get previous sibling node skipping any comments. * @param node current node * @return previous sibling */ private static DetailAST getPrevSiblingSkipComments(DetailAST node) { DetailAST result = node.getPreviousSibling(); while (result != null && (result.getType() == TokenTypes.SINGLE_LINE_COMMENT || result.getType() == TokenTypes.BLOCK_COMMENT_BEGIN)) { result = result.getPreviousSibling(); } return result; }
/** * Check if given ast is part of double brace initializer and if it * should omit checking if previous token is separated by whitespace. * @param ast ast to check * @return true if it should omit checking for previous token, false otherwise */ private static boolean isPartOfDoubleBraceInitializerForPreviousToken(DetailAST ast) { final boolean initializerBeginsAfterClassBegins = ast.getParent().getType() == TokenTypes.INSTANCE_INIT; final boolean classEndsAfterInitializerEnds = ast.getPreviousSibling() != null && ast.getPreviousSibling().getType() == TokenTypes.INSTANCE_INIT; return initializerBeginsAfterClassBegins || classEndsAfterInitializerEnds; }
/** * Registers given import. This allows us to track imported classes. * @param imp import definition. */ public void registerImport(DetailAST imp) { final FullIdent ident = FullIdent.createFullIdent( imp.getLastChild().getPreviousSibling()); final String fullName = ident.getText(); final int lastDot = fullName.lastIndexOf(DOT); importedClassPackage.put(fullName.substring(lastDot + 1), fullName); }
/** * Sets current package. * @param packageDef node for package definition */ private void visitPackageDef(DetailAST packageDef) { packageName = FullIdent.createFullIdent(packageDef.getLastChild() .getPreviousSibling()); }
/** * Checks if right curly has line break before. * @param rightCurly right curly token. * @return true, if right curly has line break before. */ private static boolean hasLineBreakBefore(DetailAST rightCurly) { final DetailAST previousToken = rightCurly.getPreviousSibling(); return previousToken == null || rightCurly.getLineNo() != previousToken.getLineNo(); }
/** * Checks whether an annotation node has any node before on the same line. * @param annotation annotation node. * @return true if an annotation node has any node before on the same line. */ private static boolean hasNodeBefore(DetailAST annotation) { final int annotationLineNo = annotation.getLineNo(); final DetailAST previousNode = annotation.getPreviousSibling(); return previousNode != null && annotationLineNo == previousNode.getLineNo(); }
/** * Collects the details of a package. * @param ast node containing the package details */ private void processPackage(DetailAST ast) { final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); packageFullIdent = FullIdent.createFullIdent(nameAST); }
/** * Checks whether the previous statement of a comment is distributed over two or more lines. * @param comment comment to check. * @return true if the previous statement of a comment is distributed over two or more lines. */ private boolean isDistributedPreviousStatement(DetailAST comment) { final DetailAST previousSibling = comment.getPreviousSibling(); return isDistributedExpression(comment) || isDistributedReturnStatement(previousSibling) || isDistributedThrowStatement(previousSibling); }
/** * Perform processing for an package token. * @param ast the package token */ private void processPackageDef(DetailAST ast) { final DetailAST packageNameAST = ast.getLastChild() .getPreviousSibling(); final FullIdent packageIdent = FullIdent.createFullIdent(packageNameAST); pkgName = packageIdent.getText(); }
@Override public void visitToken(DetailAST ast) { final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); final FullIdent full = FullIdent.createFullIdent(nameAST); if (!format.matcher(full.getText()).find()) { log(full.getDetailAst(), MSG_KEY, full.getText(), format.pattern()); } }
/** * Returns package text. * @param ast PACKAGE_DEF ast node * @return String that represents full package name */ private static String getPackageText(DetailAST ast) { final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); return FullIdent.createFullIdent(nameAST).getText(); }
@Override public void visitToken(DetailAST ast) { defined = true; if (matchDirectoryStructure) { final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); final String packageName = fullIdent.getText().replace('.', File.separatorChar); final String directoryName = getDirectoryName(); if (!directoryName.endsWith(packageName)) { log(fullIdent.getLineNo(), MSG_KEY_MISMATCH, packageName); } } }
/** * Stores package of current class we check. * @param pkg package definition. */ private void visitPackageDef(DetailAST pkg) { final FullIdent ident = FullIdent.createFullIdent(pkg.getLastChild().getPreviousSibling()); fileContext.setPackageName(ident.getText()); }
@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); } }
@Test public void testCommentChildInJavaAndJavadocMode() { final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null); parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); final DetailAST commentContentNode = tree.getLastChild().getLastChild() .getPreviousSibling().getLastChild().getFirstChild().getFirstChild(); final Object commentChild = parseTree.getChild(commentContentNode, 0); Assert.assertNull("Child must be null", commentChild); }
@Test public void testCommentChildCountInJavaAndJavadocMode() { final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null); parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); final DetailAST commentContentNode = tree.getLastChild().getLastChild() .getPreviousSibling().getLastChild().getFirstChild().getFirstChild(); final int commentChildCount = parseTree.getChildCount(commentContentNode); Assert.assertEquals("Invalid child count", 0, commentChildCount); }
@Test public void testIsReceiverParameter() throws Exception { final DetailAST objBlock = getNodeFromFile(TokenTypes.OBJBLOCK); final DetailAST methodWithReceiverParameter = objBlock.getLastChild().getPreviousSibling(); final DetailAST receiverParameter = getNode(methodWithReceiverParameter, TokenTypes.PARAMETER_DEF); final DetailAST simpleParameter = receiverParameter.getNextSibling().getNextSibling(); assertTrue("Invalid result: parameter provided is receiver parameter", CheckUtil.isReceiverParameter(receiverParameter)); assertFalse("Invalid result: parameter provided is not receiver parameter", CheckUtil.isReceiverParameter(simpleParameter)); }