Java Code Examples for com.puppycrawl.tools.checkstyle.api.DetailAST#getChildCount()

The following examples show how to use com.puppycrawl.tools.checkstyle.api.DetailAST#getChildCount() . 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: MonitoringRecordFactoryConventionCheck.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given constructor seems to be a valid one or not.
 *
 * @param constructor
 *            The constructor in question.
 *
 * @return true if and only if the constructor seems to be valid.
 */
private static boolean isValidConstructor(final DetailAST constructor) {
	// Find the available parameters of the constructor
	final DetailAST parameters = constructor.findFirstToken(TokenTypes.PARAMETERS);

	// Make sure that there is exactly ONE parameter
	if (parameters.getChildCount() == 1) {
		final DetailAST fstParType = parameters.getFirstChild().findFirstToken(TokenTypes.TYPE);
		final DetailAST fstParArr = fstParType.findFirstToken(TokenTypes.ARRAY_DECLARATOR);
		if (fstParArr != null) {
			final DetailAST fstParIdent = fstParArr.findFirstToken(TokenTypes.IDENT);
			return fstParIdent.getText().equals(CONSTRUCTOR_PARAMETER);
		}
	}

	return false;
}
 
Example 2
Source File: MonitoringRecordFactoryConventionCheck.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source File: AnalysisComponentConstructorCheck.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given constructor seems to be a valid one or not.
 *
 * @param constructor
 *            The constructor in question.
 *
 * @return true if and only if the constructor seems to be valid.
 */
private static boolean isValidConstructor(final DetailAST constructor) {
	// Find the available parameters of the constructor
	final DetailAST parameters = constructor.findFirstToken(TokenTypes.PARAMETERS);

	// Make sure that there are exactly TWO parameters (two parameters plus
	// a comma in the middle)
	if (parameters.getChildCount() == 3) {
		final DetailAST fstParType = parameters.getFirstChild().findFirstToken(TokenTypes.TYPE);
		final DetailAST sndParType = parameters.getLastChild().findFirstToken(TokenTypes.TYPE);

		final DetailAST fstParIdent = fstParType.findFirstToken(TokenTypes.IDENT);
		final DetailAST sndParIdent = sndParType.findFirstToken(TokenTypes.IDENT);

		return fstParIdent.getText().equals(CONSTRUCTOR_FST_PARAMETER)
				&& sndParIdent.getText().equals(CONSTRUCTOR_SND_PARAMETER);
	}

	return false;
}
 
Example 4
Source File: SpringLambdaCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
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 5
Source File: SpringTernaryCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private boolean requiresParens(DetailAST expression) {
	if (expression != null && expression.getChildCount() > 1) {
		switch (expression.getType()) {
		case TokenTypes.METHOD_CALL:
		case TokenTypes.DOT:
			return false;
		}
		return true;
	}
	return false;
}
 
Example 6
Source File: SpringTernaryCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
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 7
Source File: SpringMethodOrderCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private boolean isOrderedMethod(DetailAST ident, DetailAST parameters) {
	if ("equals".equals(ident.getText()) && parameters.getChildCount() == 1) {
		return true;
	}
	if ("hashCode".equals(ident.getText()) && parameters.getChildCount() == 0) {
		return true;
	}
	if ("toString".equals(ident.getText()) && parameters.getChildCount() == 0) {
		return true;
	}
	return false;
}
 
Example 8
Source File: MethodLimitCheck.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitToken(DetailAST ast) {
  // find the OBJBLOCK node below the CLASS_DEF/INTERFACE_DEF
  DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
  // count the number of direct children of the OBJBLOCK
  // that are METHOD_DEFS
  int methodDefs = objBlock.getChildCount(TokenTypes.METHOD_DEF);
  // report error if limit is reached
  if (methodDefs > max) {
    log(ast.getLineNo(), "methodlimit", max);
  }
}
 
Example 9
Source File: SpringLambdaCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
private boolean hasSingleParameter(DetailAST lambda) {
	DetailAST parameters = lambda.findFirstToken(TokenTypes.PARAMETERS);
	return (parameters == null) || (parameters.getChildCount(TokenTypes.PARAMETER_DEF) == 1);
}
 
Example 10
Source File: UnusedPrivateMethodCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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 11
Source File: UnusedPrivateMethodCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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 12
Source File: UnusedPrivateMethodCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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 13
Source File: UnusedPrivateMethodCheck.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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 contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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 contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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;
}