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

The following examples show how to use com.puppycrawl.tools.checkstyle.api.DetailAST#findFirstToken() . 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: 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 2
Source File: MonitoringRecordFactoryConventionCheck.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given field is valid or not.
 *
 * @param field
 *            The field to check.
 *
 * @return true if and only if the field is a valid one.
 */
private static boolean isValidTypeField(final DetailAST field) {
	final String ident = field.findFirstToken(TokenTypes.IDENT).getText();

	// Is the name correct?
	if (FIELD_NAME.equals(ident)) {
		final DetailAST modifiers = field.findFirstToken(TokenTypes.MODIFIERS);

		// Check whether the field is static and final
		if (modifiers.branchContains(TokenTypes.LITERAL_STATIC) && modifiers.branchContains(TokenTypes.FINAL)) {
			return MonitoringRecordFactoryConventionCheck.treeCompare(field.findFirstToken(TokenTypes.TYPE),
					TYPE_AST);
		}
	}

	return false;
}
 
Example 3
Source File: OverrideAnnotationOnTheSameLineCheck.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@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 4
Source File: PluralEnumNames.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
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 5
Source File: SpringMethodVisibilityCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private void visitPublicMethod(DetailAST modifiers, DetailAST method) {
	if (hasOverrideAnnotation(modifiers)) {
		return;
	}
	DetailAST classDef = getClassDef(method.getParent());
	if (classDef == null || isPublicOrProtected(classDef)) {
		return;
	}
	DetailAST interfaceDef = getInterfaceDef(classDef.getParent());
	if (interfaceDef != null && isPublicOrProtected(interfaceDef)) {
		return;
	}
	DetailAST ident = method.findFirstToken(TokenTypes.IDENT);
	log(ident.getLineNo(), ident.getColumnNo(), "methodvisibility.publicMethod", ident.getText());
}
 
Example 6
Source File: OverrideAnnotationOnTheSameLineCheck.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the name of the given annotation.
 *
 * @param annotation Annotation node.
 * @return Annotation name.
 */
private String annotationName(DetailAST annotation) {
    DetailAST identNode = annotation.findFirstToken(IDENT);

    if (identNode == null)
        identNode = annotation.findFirstToken(DOT).getLastChild();

    return identNode.getText();
}
 
Example 7
Source File: UnusedPrivateFieldCheck.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
public boolean mustCheckReferenceCount(DetailAST aAST)
{
    final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
    return ((mods != null)
        && (ScopeUtils.getScopeFromMods(mods) == Scope.PRIVATE));
}
 
Example 8
Source File: SpringJUnit5Check.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private void checkMethodVisibility(List<DetailAST> methods, String publicMessageKey, String privateMessageKey) {
	for (DetailAST method : methods) {
		DetailAST modifiers = method.findFirstToken(TokenTypes.MODIFIERS);
		if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null) {
			log(method, publicMessageKey);
		}
		if (modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null) {
			log(method, privateMessageKey);
		}
	}
}
 
Example 9
Source File: UnusedParameterCheck.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Checks if a given method is local, i.e. either static or private.
 * @param aAST method def for check
 * @return true if a given method is iether static or private.
 */
private boolean isLocal(DetailAST aAST)
{
    if (aAST.getType() == TokenTypes.METHOD_DEF) {
        final DetailAST modifiers =
            aAST.findFirstToken(TokenTypes.MODIFIERS);
        return (modifiers == null)
            || modifiers.branchContains(TokenTypes.LITERAL_STATIC)
            || modifiers.branchContains(TokenTypes.LITERAL_PRIVATE);
    }
    return true;
}
 
Example 10
Source File: SpringLambdaCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private boolean isUsingParametersToDefineType(DetailAST lambda) {
	DetailAST ast = lambda.findFirstToken(TokenTypes.PARAMETERS);
	ast = (ast != null ? ast.findFirstToken(TokenTypes.PARAMETER_DEF) : null);
	ast = (ast != null ? ast.findFirstToken(TokenTypes.TYPE) : null);
	ast = (ast != null ? ast.findFirstToken(TokenTypes.IDENT) : null);
	return ast != null;
}
 
Example 11
Source File: OneMethodPrivateFieldCheck.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
public boolean mustCheckReferenceCount(DetailAST aAST)
{
    final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
    return ((mods != null)
        && (ScopeUtils.getScopeFromMods(mods) == Scope.PRIVATE));
}
 
Example 12
Source File: SpringMethodVisibilityCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
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: AbstractUsageCheck.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
    if (mustCheckReferenceCount(aAST)) {
        final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
        Pattern regexp = getRegexp();
        if ((regexp == null)
            || !regexp.matcher(nameAST.getText()).find())
        {
            getASTManager().registerCheckNode(this, nameAST);
        }
    }
}
 
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: SpringLambdaCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
private boolean hasToken(DetailAST ast, int type) {
	return ast.findFirstToken(type) != null;
}
 
Example 16
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 17
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 18
Source File: PluralEnumNames.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private boolean isStatic(DetailAST modifiers) {
    return modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
}
 
Example 19
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;
}
 
Example 20
Source File: CSUtility.java    From kieker with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether the given class is marked as private or not.
 *
 * @param clazz
 *            The class to check.
 *
 * @return true if and only if the class contains a private modifier.
 */
public static boolean isPrivate(final DetailAST clazz) {
	final DetailAST modifiers = clazz.findFirstToken(TokenTypes.MODIFIERS);

	return modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
}