Java Code Examples for com.puppycrawl.tools.checkstyle.api.TokenTypes#METHOD_DEF

The following examples show how to use com.puppycrawl.tools.checkstyle.api.TokenTypes#METHOD_DEF . 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 vote down vote up
/**
 * 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: SpringMethodOrderCheck.java    From spring-javaformat with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: UnusedParameterCheck.java    From cacheonix-core 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 4
Source File: UnusedPrivateMethodCheck.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 int[] getDefaultTokens()
{
    return new int[] {
        TokenTypes.METHOD_DEF,
    };
}
 
Example 5
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 6
Source File: UnusedParameterCheck.java    From contribution 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 7
Source File: UnusedPrivateMethodCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
    return new int[] {
        TokenTypes.METHOD_DEF,
    };
}
 
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: SpringJUnit5Check.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Override
public void visitToken(DetailAST ast) {
	switch (ast.getType()) {
	case TokenTypes.METHOD_DEF:
		visitMethodDef(ast);
	case TokenTypes.IMPORT:
		visitImport(ast);
		break;
	}
}
 
Example 10
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 11
Source File: SpringMethodVisibilityCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getAcceptableTokens() {
	return new int[] { TokenTypes.METHOD_DEF };
}
 
Example 12
Source File: SdkPublicMethodNameCheck.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getRequiredTokens() {
    return new int[] {
        TokenTypes.METHOD_DEF
    };
}
 
Example 13
Source File: OneMethodPrivateFieldCheck.java    From contribution 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 14
Source File: SpringJUnit5Check.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getAcceptableTokens() {
	return new int[] { TokenTypes.METHOD_DEF, TokenTypes.IMPORT };
}
 
Example 15
Source File: SpringJavadocCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getAcceptableTokens() {
	return new int[] { TokenTypes.INTERFACE_DEF, TokenTypes.CLASS_DEF, TokenTypes.ENUM_DEF,
			TokenTypes.ANNOTATION_DEF, TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF, TokenTypes.ENUM_CONSTANT_DEF,
			TokenTypes.ANNOTATION_FIELD_DEF };
}
 
Example 16
Source File: NotAllowedIgnoreAnnotationCheck.java    From kieker with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getDefaultTokens() {
	// This here makes sure that we just get the correct components
	return new int[] { TokenTypes.METHOD_DEF };
}
 
Example 17
Source File: NotAllowedSinceTagCheck.java    From kieker with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getDefaultTokens() {
	// This here makes sure that we just get the correct components
	return new int[] { TokenTypes.METHOD_DEF, TokenTypes.VARIABLE_DEF };
}
 
Example 18
Source File: SpringJavadocCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getDefaultTokens() {
	return new int[] { TokenTypes.INTERFACE_DEF, TokenTypes.CLASS_DEF, TokenTypes.ENUM_DEF,
			TokenTypes.ANNOTATION_DEF, TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF };
}