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

The following examples show how to use com.puppycrawl.tools.checkstyle.api.DetailAST#getType() . 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: UnusedParameterCheck.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
public boolean mustCheckReferenceCount(DetailAST aAST)
{
    boolean result = false;
    final DetailAST parent = aAST.getParent();
    if (parent != null) {
        if (parent.getType() == TokenTypes.PARAMETERS) {
            final DetailAST grandparent = parent.getParent();
            if (grandparent != null) {
                result = hasBody(grandparent)
                    && (!mIgnoreNonLocal || isLocal(grandparent));
            }
        }
        else if (parent.getType() == TokenTypes.LITERAL_CATCH) {
            result = !mIgnoreCatch;
        }
    }
    return result;
}
 
Example 2
Source File: MonitoringRecordFactoryConventionCheck.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * This method checks the constructors of the record.
 *
 * @param ast
 *            The record class.
 */
private void checkConstructors(final DetailAST ast) {
	DetailAST child = ast.findFirstToken(TokenTypes.OBJBLOCK).findFirstToken(TokenTypes.CTOR_DEF);
	while (child != null) {
		if ((child.getType() == TokenTypes.CTOR_DEF)
				&& MonitoringRecordFactoryConventionCheck.isValidConstructor(child)) {
			// We found a valid constructor
			return;
		}

		child = child.getNextSibling();
	}

	// Seems like there is no valid constructor
	this.log(ast.getLineNo(), "invalid factory constructor");
}
 
Example 3
Source File: MonitoringRecordFactoryConventionCheck.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * This method checks the fields of the record.
 *
 * @param ast
 *            The record class.
 */
private void checkFields(final DetailAST ast) {
	DetailAST child = ast.findFirstToken(TokenTypes.OBJBLOCK).findFirstToken(TokenTypes.VARIABLE_DEF);
	while (child != null) {
		if ((child.getType() == TokenTypes.VARIABLE_DEF)
				&& MonitoringRecordFactoryConventionCheck.isValidTypeField(child)) {
			// We found a valid field
			return;
		}

		child = child.getNextSibling();
	}

	// Seems like there is no valid field
	this.log(ast.getLineNo(), "invalid factory field");
}
 
Example 4
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 5
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 6
Source File: DocumentNavigator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @see org.jaxen.DefaultNavigator#isDocument(java.lang.Object)
 */
public boolean isDocument(Object aObject)
{
    if (aObject instanceof DetailAST) {
        final DetailAST node = (DetailAST) aObject;
        return (node.getType() == TokenTypes.EOF);
    }
    else {
        return false;
    }
}
 
Example 7
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 8
Source File: UnusedParameterCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Determines whether an AST is a method definition with a body, or is
 * a constructor definition.
 * @param aAST the AST to check.
 * @return if AST has a body.
 */
private boolean hasBody(DetailAST aAST)
{
    if (aAST.getType() == TokenTypes.METHOD_DEF) {
        return aAST.branchContains(TokenTypes.SLIST);
    }
    else if (aAST.getType() == TokenTypes.CTOR_DEF) {
        return true;
    }
    return false;
}
 
Example 9
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 10
Source File: SpringCatchCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
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 11
Source File: SpringNoThisCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private DetailAST getFirstNonDotParent(DetailAST ast) {
	DetailAST result = (ast != null ? ast.getParent() : null);
	while (result != null && result.getType() == TokenTypes.DOT) {
		result = result.getParent();
	}
	return result;
}
 
Example 12
Source File: NotAllowedSinceTagCheck.java    From kieker with Apache License 2.0 5 votes vote down vote up
@Override
public void visitToken(final DetailAST ast) {
	if (CSUtility.sinceTagAvailable(this, ast)
			&& ((ast.getType() == TokenTypes.VARIABLE_DEF) || !(CSUtility.parentIsInterface(ast)))) {
		this.log(ast.getLineNo(), "@since tag not allowed");
	}
}
 
Example 13
Source File: SpringJavadocCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private DetailAST findParent(DetailAST ast, int classDef) {
	while (ast != null) {
		if (ast.getType() == classDef) {
			return ast;
		}
		ast = ast.getParent();
	}
	return null;
}
 
Example 14
Source File: SpringMethodVisibilityCheck.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private DetailAST findParent(DetailAST ast, int classDef) {
	while (ast != null) {
		if (ast.getType() == classDef) {
			return ast;
		}
		ast = ast.getParent();
	}
	return null;
}
 
Example 15
Source File: SpringCatchCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
private void visitParameterDef(DetailAST ast) {
	DetailAST lastChild = ast.getLastChild();
	if (lastChild != null && lastChild.getType() == TokenTypes.IDENT) {
		checkIdent(lastChild);
	}
}
 
Example 16
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 17
Source File: IfBracesCheck.java    From requestor with Apache License 2.0 4 votes vote down vote up
@Override
public void visitToken(DetailAST aAST) {
    final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST);

    if (aAST.getType() == TokenTypes.LITERAL_ELSE) {
        // If we have an else, it must have braces, except it is an "else if" (then the if must have braces).
        DetailAST ifToken = aAST.findFirstToken(TokenTypes.LITERAL_IF);

        if (ifToken == null) {
            // This is an simple else, it must have brace.
            if (slistAST == null) {
                log(aAST.getLineNo(), "ifBracesElse", aAST.getText());
            }
        } else {
            // This is an "else if", the if must have braces.
            if (ifToken.findFirstToken(TokenTypes.SLIST) == null) {
                log(aAST.getLineNo(), "ifBracesConditional", ifToken.getText(), aAST.getText() + " " + ifToken
                        .getText());
            }
        }
    } else if (aAST.getType() == TokenTypes.LITERAL_IF) {
        // If the if uses braces, nothing as to be checked.
        if (slistAST != null) {
            return;
        }

        // We have an if, we need to check if it has no conditionnal structure as direct child.
        final int[] conditionals = {
                TokenTypes.LITERAL_DO,
                TokenTypes.LITERAL_ELSE,
                TokenTypes.LITERAL_FOR,
                TokenTypes.LITERAL_IF,
                TokenTypes.LITERAL_WHILE,
                TokenTypes.LITERAL_SWITCH,
        };

        for (int conditional : conditionals) {
            DetailAST conditionalAST = aAST.findFirstToken(conditional);

            if (conditionalAST != null) {
                log(aAST.getLineNo(), "ifBracesConditional", aAST.getText(), conditionalAST.getText());

                // Let's trigger this only once.
                return;
            }
        }
    }
}
 
Example 18
Source File: OneMethodPrivateFieldCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
public void applyTo(Set aNodes)
{
    // apply the check to each private field
    final Set methods = new HashSet();
    final Iterator it = aNodes.iterator();
    while (it.hasNext()) {
        methods.clear();
        final DetailAST nameAST = (DetailAST) it.next();
        // find methods using the field
        final Iterator refIt = getReferences(nameAST);
        while (refIt.hasNext()) {
            final Reference ref = (Reference) refIt.next();
            final SymTabAST refNode = ref.getTreeNode();
            final DetailAST refDetail = refNode.getDetailNode();
            // don't need to check a self-reference
            if (refDetail == nameAST) {
                continue;
            }
            DetailAST parent = refDetail.getParent();
            while (parent != null) {
                final int type = parent.getType();
                if ((type == TokenTypes.METHOD_DEF)
                    || (type == TokenTypes.CTOR_DEF)
                    || (type == TokenTypes.INSTANCE_INIT)
                    || (type == TokenTypes.STATIC_INIT))
                {
                    methods.add(parent);
                    break;
                }
                // initializer for inner class?
                else if (type == TokenTypes.CLASS_DEF) {
                    break;
                }
                parent = parent.getParent();
            }
        }
        if (methods.size() == 1) {
            log(
                nameAST.getLineNo(),
                nameAST.getColumnNo(),
                getErrorKey(),
                nameAST.getText());
        }
    }
}
 
Example 19
Source File: PluralEnumNames.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private boolean isEnum(DetailAST ast) {
    return ast.getType() == TokenTypes.ENUM_DEF;
}
 
Example 20
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;
}