Java Code Examples for com.puppycrawl.tools.checkstyle.api.DetailAST#getFirstChild()
The following examples show how to use
com.puppycrawl.tools.checkstyle.api.DetailAST#getFirstChild() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CSUtility.java From kieker with Apache License 2.0 | 6 votes |
/** * This method extracts all method definitions from the given class (or interface). * * @param ast * The class (or interface). * * @return A collection of available methods. */ public static Collection<DetailAST> getMethodsFromClass(final DetailAST ast) { final Collection<DetailAST> result = new ArrayList<DetailAST>(); final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK); DetailAST child = objBlock.getFirstChild(); while (child != null) { if (child.getType() == TokenTypes.METHOD_DEF) { result.add(child); } child = child.getNextSibling(); } return result; }
Example 2
Source File: AnalysisComponentConstructorCheck.java From kieker with Apache License 2.0 | 6 votes |
/** * This method finds out whether the given class is an analysis component or * not (by searching for annotations with the names "Plugin" or * "Repository"). * * @param clazz * The class in question. * * @return true if and only if the class is an analysis component. */ private static boolean isAnalysisComponent(final DetailAST clazz) { // Get the list of modifiers as the annotations are within those final DetailAST modifiers = clazz.findFirstToken(TokenTypes.MODIFIERS); DetailAST modifier = modifiers.getFirstChild(); // Run through the modifiers while (modifier != null) { // If we find an annotation we check the name if ((modifier.getType() == TokenTypes.ANNOTATION) && (modifier.findFirstToken(TokenTypes.IDENT) != null)) { final String annotationName = modifier.findFirstToken(TokenTypes.IDENT).getText(); if (annotationName.equals(PLUGIN_ANNOTATION_NAME) || annotationName.equals(REPOSITORY_ANNOTATION_NAME)) { // We found an annotation with the correct name return true; } } modifier = modifier.getNextSibling(); } // Seems like there is no such annotation return false; }
Example 3
Source File: MonitoringRecordFactoryConventionCheck.java From kieker with Apache License 2.0 | 6 votes |
/** * This method finds out whether the given class implements the record factory * or not. * * @param clazz * The class in question. * * @return true if and only if the class implements the record factory. */ private static boolean implementsFactory(final DetailAST clazz) { final DetailAST implementsClause = clazz.findFirstToken(TokenTypes.IMPLEMENTS_CLAUSE); if (implementsClause != null) { DetailAST clause = implementsClause.getFirstChild(); // Run through the whole implements clause while (clause != null) { // It has to be a combination of two names if ((clause.getType() == TokenTypes.DOT) && (clause.getChildCount() == 2)) { final String fstClauseIdent = clause.getFirstChild().getText(); final String sndClauseIdent = clause.getLastChild().getText(); return (FACTORY_FST_NAME.equals(fstClauseIdent)) && (FACTORY_SND_NAME.equals(sndClauseIdent)); } clause = clause.getNextSibling(); } } return false; }
Example 4
Source File: SpringMethodOrderCheck.java From spring-javaformat with Apache License 2.0 | 6 votes |
@Override public void visitToken(DetailAST ast) { DetailAST block = ast.findFirstToken(TokenTypes.OBJBLOCK); DetailAST candidate = block.getFirstChild(); List<DetailAST> methods = new ArrayList<>(); while (candidate != null) { candidate = candidate.getNextSibling(); if (candidate != null && candidate.getType() == TokenTypes.METHOD_DEF) { DetailAST ident = candidate.findFirstToken(TokenTypes.IDENT); DetailAST parameters = candidate.findFirstToken(TokenTypes.PARAMETERS); if (isOrderedMethod(ident, parameters)) { methods.add(ident); } } } checkOrder(methods); }
Example 5
Source File: PluralEnumNames.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
private boolean hasPublicStaticField(DetailAST ast) { DetailAST classBody = ast.findFirstToken(TokenTypes.OBJBLOCK); DetailAST maybeVariableDefinition = classBody.getFirstChild(); String className = ast.findFirstToken(TokenTypes.IDENT).getText(); // Filter out util classes if (className.endsWith("Utils")) { return false; } while (maybeVariableDefinition != null) { if (maybeVariableDefinition.getType() == TokenTypes.VARIABLE_DEF) { DetailAST modifiers = maybeVariableDefinition.findFirstToken(TokenTypes.MODIFIERS); if (modifiers != null && isPublic(modifiers) && isStatic(modifiers)) { return true; } } maybeVariableDefinition = maybeVariableDefinition.getNextSibling(); } return false; }
Example 6
Source File: OverrideAnnotationOnTheSameLineCheck.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override public void visitToken(DetailAST ast) { DetailAST node = ast.findFirstToken(MODIFIERS); if (node == null) node = ast.findFirstToken(ANNOTATIONS); if (node == null) return; for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getType() == TokenTypes.ANNOTATION && Override.class.getSimpleName().equals(annotationName(node)) && node.getLineNo() != nextNode(node).getLineNo()) log(node.getLineNo(), DIFF_LINE_ERR_MSG, annotationName(node)); } }
Example 7
Source File: SpringLambdaCheck.java From spring-javaformat with Apache License 2.0 | 5 votes |
private int countDescendantsOfType(DetailAST ast, int... types) { int count = 0; for (int type : types) { count += ast.getChildCount(type); } DetailAST child = ast.getFirstChild(); while (child != null) { count += countDescendantsOfType(child, types); child = child.getNextSibling(); } return count; }
Example 8
Source File: MonitoringRecordFactoryConventionCheck.java From kieker with Apache License 2.0 | 5 votes |
/** * This method checks whether the given trees are more or less equal. This is * necessary as the available methods do not work as expected for this special * case. * * @param actual * The actual tree. * @param expected * The expected tree. * * @return true if and only if the trees are (more or less) equal. */ private static boolean treeCompare(final DetailAST actual, final DetailAST expected) { if ((actual != null) && (expected != null) && (actual.getType() == expected.getType())) { // If they are identifiers, we check the texts if ((actual.getType() == TokenTypes.IDENT) && (!actual.getText().equals(expected.getText()))) { return false; } // Everything fine so far. Check the equality of the children DetailAST actChild = actual.getFirstChild(); DetailAST expChild = expected.getFirstChild(); while (actChild != null) { // The treeCompare method checks whether one of the children is null (we // recognize therefore if the parents have different number of children). if (!MonitoringRecordFactoryConventionCheck.treeCompare(actChild, expChild)) { return false; } actChild = actChild.getNextSibling(); expChild = expChild.getNextSibling(); } // Everything seems to be fine - just make sure that expChild is null as well or // otherwise the parents had a different number of children return expChild == null; } return false; }
Example 9
Source File: SpringTernaryCheck.java From spring-javaformat with Apache License 2.0 | 5 votes |
private boolean isSimpleEqualsExpression(DetailAST expression) { if (expression == null || expression.getType() != TokenTypes.EQUAL) { return false; } DetailAST child = expression.getFirstChild(); while (child != null) { if (child.getChildCount() > 0) { return false; } child = child.getNextSibling(); } return true; }
Example 10
Source File: SpringTernaryCheck.java From spring-javaformat with Apache License 2.0 | 5 votes |
private void visitQuestion(DetailAST ast) { DetailAST expression = ast.getFirstChild(); if (!hasType(expression, TokenTypes.LPAREN)) { if (expression != null && requiresParens(expression)) { log(ast.getLineNo(), ast.getColumnNo(), "ternary.missingParen"); } } while (hasType(expression, TokenTypes.LPAREN)) { expression = expression.getNextSibling(); } if (isSimpleEqualsExpression(expression) && !isEqualsTestAllowed(ast)) { log(ast.getLineNo(), ast.getColumnNo(), "ternary.equalOperator"); } }
Example 11
Source File: SpringCatchCheck.java From spring-javaformat with Apache License 2.0 | 5 votes |
private void visitCatch(DetailAST ast) { DetailAST child = ast.getFirstChild(); while (child != null && child.getType() != TokenTypes.PARAMETER_DEF) { child = child.getNextSibling(); } if (child != null) { visitParameterDef(child); } }
Example 12
Source File: SpringMethodVisibilityCheck.java From spring-javaformat with Apache License 2.0 | 5 votes |
private boolean hasOverrideAnnotation(DetailAST modifiers) { DetailAST candidate = modifiers.getFirstChild(); while (candidate != null) { if (candidate.getType() == TokenTypes.ANNOTATION) { DetailAST dot = candidate.findFirstToken(TokenTypes.DOT); String name = (dot != null ? dot : candidate).findFirstToken(TokenTypes.IDENT).getText(); if ("Override".equals(name)) { return true; } } candidate = candidate.getNextSibling(); } return false; }
Example 13
Source File: UnusedPrivateMethodCheck.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
/** * Checks if a given method is writeObject(). * @param aAST method def to check * @return true if this is a writeObject() definition */ private boolean isWriteObject(DetailAST aAST) { // name is writeObject... final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); if (!"writeObject".equals(ident.getText())) { return false; } // returns void... final DetailAST typeAST = (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); if (typeAST.getType() != TokenTypes.LITERAL_VOID) { return false; } // should have one parameter... final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); if (params == null || params.getChildCount() != 1) { return false; } // and paramter's type should be java.io.ObjectOutputStream final DetailAST type = (DetailAST) ((DetailAST) params.getFirstChild()) .findFirstToken(TokenTypes.TYPE).getFirstChild(); final String typeName = FullIdent.createFullIdent(type).getText(); if (!"java.io.ObjectOutputStream".equals(typeName) && !"ObjectOutputStream".equals(typeName)) { return false; } // and, finally, it should throws java.io.IOException final DetailAST throwsAST = aAST.findFirstToken(TokenTypes.LITERAL_THROWS); if (throwsAST == null || throwsAST.getChildCount() != 1) { return false; } final DetailAST expt = (DetailAST) throwsAST.getFirstChild(); final String exceptionName = FullIdent.createFullIdent(expt).getText(); if (!"java.io.IOException".equals(exceptionName) && !"IOException".equals(exceptionName)) { return false; } return true; }
Example 14
Source File: UnusedPrivateMethodCheck.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
/** * Checks if a given method is readObject(). * @param aAST method def to check * @return true if this is a readObject() definition */ private boolean isReadObject(DetailAST aAST) { // name is readObject... final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); if (!"readObject".equals(ident.getText())) { return false; } // returns void... final DetailAST typeAST = (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); if (typeAST.getType() != TokenTypes.LITERAL_VOID) { return false; } // should have one parameter... final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); if (params == null || params.getChildCount() != 1) { return false; } // and paramter's type should be java.io.ObjectInputStream final DetailAST type = (DetailAST) ((DetailAST) params.getFirstChild()) .findFirstToken(TokenTypes.TYPE).getFirstChild(); final String typeName = FullIdent.createFullIdent(type).getText(); if (!"java.io.ObjectInputStream".equals(typeName) && !"ObjectInputStream".equals(typeName)) { return false; } // and, finally, it should throws java.io.IOException // and java.lang.ClassNotFoundException final DetailAST throwsAST = aAST.findFirstToken(TokenTypes.LITERAL_THROWS); if (throwsAST == null || throwsAST.getChildCount() != 3) { return false; } final DetailAST excpt1 = (DetailAST) throwsAST.getFirstChild(); final String exception1 = FullIdent.createFullIdent(excpt1).getText(); final String exception2 = FullIdent.createFullIdent(throwsAST.getLastChild()).getText(); if (!"java.io.IOException".equals(exception1) && !"IOException".equals(exception1) && !"java.io.IOException".equals(exception2) && !"IOException".equals(exception2) || !"java.lang.ClassNotFoundException".equals(exception1) && !"ClassNotFoundException".equals(exception1) && !"java.lang.ClassNotFoundException".equals(exception2) && !"ClassNotFoundException".equals(exception2)) { return false; } return true; }
Example 15
Source File: UnusedPrivateMethodCheck.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
/** * Checks if a given method is writeReplace() or readResolve(). * @param aAST method def to check * @return true if this is a writeReplace() definition */ private boolean isWriteReplaceOrReadResolve(DetailAST aAST) { // name is writeReplace or readResolve... final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); if (!"writeReplace".equals(ident.getText()) && !"readResolve".equals(ident.getText())) { return false; } // returns Object... final DetailAST typeAST = (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); if (typeAST.getType() != TokenTypes.DOT && typeAST.getType() != TokenTypes.IDENT) { return false; } // should have no parameters... final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); if (params != null && params.getChildCount() != 0) { return false; } // and, finally, it should throws java.io.ObjectStreamException final DetailAST throwsAST = aAST.findFirstToken(TokenTypes.LITERAL_THROWS); if (throwsAST == null || throwsAST.getChildCount() != 1) { return false; } final DetailAST excpt = (DetailAST) throwsAST.getFirstChild(); final String exception = FullIdent.createFullIdent(excpt).getText(); if (!"java.io.ObjectStreamException".equals(exception) && !"ObjectStreamException".equals(exception)) { return false; } return true; }
Example 16
Source File: UnusedPrivateMethodCheck.java From contribution with GNU Lesser General Public License v2.1 | 4 votes |
/** * Checks if a given method is writeObject(). * @param aAST method def to check * @return true if this is a writeObject() definition */ private boolean isWriteObject(DetailAST aAST) { // name is writeObject... final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); if (!"writeObject".equals(ident.getText())) { return false; } // returns void... final DetailAST typeAST = (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); if (typeAST.getType() != TokenTypes.LITERAL_VOID) { return false; } // should have one parameter... final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); if (params == null || params.getChildCount() != 1) { return false; } // and paramter's type should be java.io.ObjectOutputStream final DetailAST type = (DetailAST) ((DetailAST) params.getFirstChild()) .findFirstToken(TokenTypes.TYPE).getFirstChild(); final String typeName = FullIdent.createFullIdent(type).getText(); if (!"java.io.ObjectOutputStream".equals(typeName) && !"ObjectOutputStream".equals(typeName)) { return false; } // and, finally, it should throws java.io.IOException final DetailAST throwsAST = aAST.findFirstToken(TokenTypes.LITERAL_THROWS); if (throwsAST == null || throwsAST.getChildCount() != 1) { return false; } final DetailAST expt = (DetailAST) throwsAST.getFirstChild(); final String exceptionName = FullIdent.createFullIdent(expt).getText(); if (!"java.io.IOException".equals(exceptionName) && !"IOException".equals(exceptionName)) { return false; } return true; }
Example 17
Source File: UnusedPrivateMethodCheck.java From contribution with GNU Lesser General Public License v2.1 | 4 votes |
/** * Checks if a given method is readObject(). * @param aAST method def to check * @return true if this is a readObject() definition */ private boolean isReadObject(DetailAST aAST) { // name is readObject... final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); if (!"readObject".equals(ident.getText())) { return false; } // returns void... final DetailAST typeAST = (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); if (typeAST.getType() != TokenTypes.LITERAL_VOID) { return false; } // should have one parameter... final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); if (params == null || params.getChildCount() != 1) { return false; } // and paramter's type should be java.io.ObjectInputStream final DetailAST type = (DetailAST) ((DetailAST) params.getFirstChild()) .findFirstToken(TokenTypes.TYPE).getFirstChild(); final String typeName = FullIdent.createFullIdent(type).getText(); if (!"java.io.ObjectInputStream".equals(typeName) && !"ObjectInputStream".equals(typeName)) { return false; } // and, finally, it should throws java.io.IOException // and java.lang.ClassNotFoundException final DetailAST throwsAST = aAST.findFirstToken(TokenTypes.LITERAL_THROWS); if (throwsAST == null || throwsAST.getChildCount() != 3) { return false; } final DetailAST excpt1 = (DetailAST) throwsAST.getFirstChild(); final String exception1 = FullIdent.createFullIdent(excpt1).getText(); final String exception2 = FullIdent.createFullIdent(throwsAST.getLastChild()).getText(); if (!"java.io.IOException".equals(exception1) && !"IOException".equals(exception1) && !"java.io.IOException".equals(exception2) && !"IOException".equals(exception2) || !"java.lang.ClassNotFoundException".equals(exception1) && !"ClassNotFoundException".equals(exception1) && !"java.lang.ClassNotFoundException".equals(exception2) && !"ClassNotFoundException".equals(exception2)) { return false; } return true; }
Example 18
Source File: UnusedPrivateMethodCheck.java From contribution with GNU Lesser General Public License v2.1 | 4 votes |
/** * Checks if a given method is writeReplace() or readResolve(). * @param aAST method def to check * @return true if this is a writeReplace() definition */ private boolean isWriteReplaceOrReadResolve(DetailAST aAST) { // name is writeReplace or readResolve... final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); if (!"writeReplace".equals(ident.getText()) && !"readResolve".equals(ident.getText())) { return false; } // returns Object... final DetailAST typeAST = (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); if (typeAST.getType() != TokenTypes.DOT && typeAST.getType() != TokenTypes.IDENT) { return false; } // should have no parameters... final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); if (params != null && params.getChildCount() != 0) { return false; } // and, finally, it should throws java.io.ObjectStreamException final DetailAST throwsAST = aAST.findFirstToken(TokenTypes.LITERAL_THROWS); if (throwsAST == null || throwsAST.getChildCount() != 1) { return false; } final DetailAST excpt = (DetailAST) throwsAST.getFirstChild(); final String exception = FullIdent.createFullIdent(excpt).getText(); if (!"java.io.ObjectStreamException".equals(exception) && !"ObjectStreamException".equals(exception)) { return false; } return true; }
Example 19
Source File: NodeIterator.java From cacheonix-core with GNU Lesser General Public License v2.1 | 2 votes |
/** * Get the first child of a DetailAST. * @param aAST the DetailAST. * @return the first child of aAST. */ protected DetailAST getFirstChild(DetailAST aAST) { return (DetailAST) aAST.getFirstChild(); }
Example 20
Source File: NodeIterator.java From contribution with GNU Lesser General Public License v2.1 | 2 votes |
/** * Get the first child of a DetailAST. * @param aAST the DetailAST. * @return the first child of aAST. */ protected DetailAST getFirstChild(DetailAST aAST) { return (DetailAST) aAST.getFirstChild(); }