/** * Checks whether a token has a javadoc comment. * * @param methodDef method definition token. * @param tokenType token type. * @return true if a token has a javadoc comment. */ private static boolean hasJavadocCommentOnToken(DetailAST methodDef, int tokenType) { final DetailAST token = methodDef.findFirstToken(tokenType); return token.branchContains(TokenTypes.BLOCK_COMMENT_BEGIN); }
/** * Checks for expanded style type violations. * * @param annotation the annotation token */ private void checkExpandedStyle(final DetailAST annotation) { final int valuePairCount = annotation.getChildCount(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR); if (valuePairCount == 0 && annotation.branchContains(TokenTypes.EXPR)) { log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE, ElementStyle.EXPANDED); } }
/** * 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) { if (!parenExists) { log(ast.getLineNo(), MSG_KEY_ANNOTATION_PARENS_MISSING); } } else if (parenExists && !ast.branchContains(TokenTypes.EXPR) && !ast.branchContains(TokenTypes.ANNOTATION_ARRAY_INIT)) { log(ast.getLineNo(), MSG_KEY_ANNOTATION_PARENS_PRESENT); } } }
/** * Looks for all "safe" Token combinations in the argument * expression branch. * @param expr the argument expression * @return - true if any child matches the set of tokens, false if not */ private static boolean containsAllSafeTokens(final DetailAST expr) { DetailAST arg = expr.getFirstChild(); arg = skipVariableAssign(arg); boolean argIsNotNull = false; if (arg.getType() == TokenTypes.PLUS) { DetailAST child = arg.getFirstChild(); while (child != null && !argIsNotNull) { argIsNotNull = child.getType() == TokenTypes.STRING_LITERAL || child.getType() == TokenTypes.IDENT; child = child.getNextSibling(); } } return argIsNotNull || !arg.branchContains(TokenTypes.IDENT) && !arg.branchContains(TokenTypes.LITERAL_NULL); }
@Test public void testBranchContains() { final DetailAST root = createToken(null, TokenTypes.CLASS_DEF); final DetailAST modifiers = createToken(root, TokenTypes.MODIFIERS); createToken(modifiers, TokenTypes.LITERAL_PUBLIC); assertTrue("invalid result", root.branchContains(TokenTypes.LITERAL_PUBLIC)); assertFalse("invalid result", root.branchContains(TokenTypes.OBJBLOCK)); }
private static boolean hasPrivatStaticFinalModifier(final DetailAST ast) { final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS); return modifiers != null && modifiers.branchContains(TokenTypes.LITERAL_PRIVATE) && modifiers.branchContains(TokenTypes.LITERAL_STATIC) && modifiers.branchContains(TokenTypes.FINAL); } }
/** * Determines whether a method is {@code abstract} or {@code native}. * @param method Method to check. * @return True if method is abstract or native. */ private static boolean isInAbstractOrNativeMethod(final DetailAST method) { final DetailAST modifiers = method.findFirstToken(TokenTypes.MODIFIERS); return modifiers.branchContains(TokenTypes.ABSTRACT) || modifiers.branchContains(TokenTypes.LITERAL_NATIVE); }
/** * Determines whether a method is {@code abstract} or {@code native}. * @param method Method to check. * @return True if method is abstract or native. */ private static boolean isInAbstractOrNativeMethod(final DetailAST method) { final DetailAST modifiers = method.findFirstToken(TokenTypes.MODIFIERS); return modifiers.branchContains(TokenTypes.ABSTRACT) || modifiers.branchContains(TokenTypes.LITERAL_NATIVE); }
/** * Checks whether a token has a javadoc comment. * * @param methodDef method definition token. * @param tokenType token type. * @return true if a token has a javadoc comment. */ private static boolean hasJavadocCommentOnToken(DetailAST methodDef, int tokenType) { final DetailAST token = methodDef.findFirstToken(tokenType); return token.branchContains(TokenTypes.BLOCK_COMMENT_BEGIN); }
/** * Returns true if specified node has modifiers of type * <code>PRIVATE</code>. * @param node Node to check. * @return True if specified node contains modifiers of type * <code>PRIVATE</code>, else returns <code>false</code>. */ private static boolean isPrivate(final DetailAST node) { final DetailAST modifiers = node.findFirstToken(TokenTypes.MODIFIERS); return modifiers.branchContains(TokenTypes.LITERAL_PRIVATE); }
/** * Returns true if specified node has modifiers of type <code>FINAL</code>. * @param node Node to check. * @return True if specified node contains modifiers of type * <code>FINAL</code>, else returns <code>false</code>. */ private static boolean isFinal(final DetailAST node) { final DetailAST modifiers = node.findFirstToken(TokenTypes.MODIFIERS); return modifiers.branchContains(TokenTypes.FINAL); }
/** * Returns true if specified node has modifiers of type <code>FINAL</code>. * @param node Node to check. * @return True if specified node contains modifiers of type * <code>FINAL</code>, else returns <code>false</code>. */ private static boolean isFinal(final DetailAST node) { final DetailAST modifiers = node.findFirstToken(TokenTypes.MODIFIERS); return modifiers.branchContains(TokenTypes.FINAL); }
/** * @param ast class, variable or method ast.. * @return true if a given class declared as static. */ public static Boolean isStatic(DetailAST ast) { return ast.findFirstToken(TokenTypes.MODIFIERS) .branchContains(TokenTypes.LITERAL_STATIC); }
/** * Returns true if specified node has modifiers of type * <code>PRIVATE</code>. * @param node Node to check. * @return True if specified node contains modifiers of type * <code>PRIVATE</code>, else returns <code>false</code>. */ private static boolean isPrivate(final DetailAST node) { final DetailAST modifiers = node.findFirstToken(TokenTypes.MODIFIERS); return modifiers.branchContains(TokenTypes.LITERAL_PRIVATE); }
/** * @param classAst class definition for check. * @return true if a given class declared as abstract. */ public static Boolean isAbstract(DetailAST classAst) { return classAst.findFirstToken(TokenTypes.MODIFIERS) .branchContains(TokenTypes.ABSTRACT); } }
/** * Check IF or not that contained NULL in the expression IF. * * @param literalIf The token to examine. * @return true if the IF contains a NULL. * @see ignoreNullCaseInIf */ private static boolean isIfWithNull(DetailAST literalIf) { return literalIf.getFirstChild().getNextSibling() .branchContains(TokenTypes.LITERAL_NULL); }
/** * Check IF - ELSE or not that contained THROW in the expression in a block * ELSE. * * @param literalIf The token to examine. * @return true if the ELSE contains a THROW. */ private static boolean isElseWithThrow(DetailAST literalIf) { final DetailAST lastChildAfterIf = literalIf.getLastChild(); return lastChildAfterIf.getFirstChild().branchContains( TokenTypes.LITERAL_THROW); }
/** * @return simple name(last name after last DOT if exists) */ public static String getSimpleName(DetailAST ast) { DetailAST identifier = ast.findFirstToken(TokenTypes.IDENT); if (isNull(identifier) && ast.branchContains(TokenTypes.DOT)) { DetailAST dotAst = ast.findFirstToken(TokenTypes.DOT); identifier = dotAst.findFirstToken(TokenTypes.IDENT); } return identifier.getText(); }
/** * Checks for expanded style type violations. * * @param annotation the annotation token */ private void checkExpandedStyle(final DetailAST annotation) { final int valuePairCount = annotation.getChildCount(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR); if (valuePairCount == 0 && annotation.branchContains(TokenTypes.EXPR)) { log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE, ElementStyle.EXPANDED); } }
/** * Check the inner IF or not. * * @param literalIf The token to examine. * @return true if the if is inner. */ private static boolean isInnerIf(DetailAST literalIf) { final DetailAST childIf = literalIf.getFirstChild().getNextSibling() .getNextSibling().getNextSibling(); return childIf.branchContains(TokenTypes.LITERAL_IF); }