Search in sources :

Example 71 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class JavadocMethodCheck method getThrows.

/**
     * Computes the exception nodes for a method.
     *
     * @param ast the method node.
     * @return the list of exception nodes for ast.
     */
private List<ExceptionInfo> getThrows(DetailAST ast) {
    final List<ExceptionInfo> returnValue = new ArrayList<>();
    final DetailAST throwsAST = ast.findFirstToken(TokenTypes.LITERAL_THROWS);
    if (throwsAST != null) {
        DetailAST child = throwsAST.getFirstChild();
        while (child != null) {
            if (child.getType() == TokenTypes.IDENT || child.getType() == TokenTypes.DOT) {
                final FullIdent ident = FullIdent.createFullIdent(child);
                final ExceptionInfo exceptionInfo = new ExceptionInfo(createClassInfo(new Token(ident), getCurrentClassName()));
                returnValue.add(exceptionInfo);
            }
            child = child.getNextSibling();
        }
    }
    return returnValue;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) ArrayList(java.util.ArrayList) FullIdent(com.puppycrawl.tools.checkstyle.api.FullIdent)

Example 72 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class JavadocMethodCheck method searchMatchingTypeParameter.

/**
     * Returns true if required type found in type parameters.
     * @param typeParams
     *            list of type parameters
     * @param requiredTypeName
     *            name of required type
     * @return true if required type found in type parameters.
     */
private static boolean searchMatchingTypeParameter(List<DetailAST> typeParams, String requiredTypeName) {
    // Loop looking for matching type param
    final Iterator<DetailAST> typeParamsIt = typeParams.iterator();
    boolean found = false;
    while (typeParamsIt.hasNext()) {
        final DetailAST typeParam = typeParamsIt.next();
        if (typeParam.findFirstToken(TokenTypes.IDENT).getText().equals(requiredTypeName)) {
            found = true;
            typeParamsIt.remove();
            break;
        }
    }
    return found;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 73 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class JavadocTypeCheck method shouldCheck.

/**
     * Whether we should check this node.
     * @param ast a given node.
     * @return whether we should check a given node.
     */
private boolean shouldCheck(final DetailAST ast) {
    final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS);
    final Scope declaredScope = ScopeUtils.getScopeFromMods(mods);
    final Scope customScope;
    if (ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) {
        customScope = Scope.PUBLIC;
    } else {
        customScope = declaredScope;
    }
    final Scope surroundingScope = ScopeUtils.getSurroundingScope(ast);
    return customScope.isIn(scope) && (surroundingScope == null || surroundingScope.isIn(scope)) && (excludeScope == null || !customScope.isIn(excludeScope) || surroundingScope != null && !surroundingScope.isIn(excludeScope));
}
Also used : Scope(com.puppycrawl.tools.checkstyle.api.Scope) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 74 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class JavaNCSSCheck method isVariableDefCountable.

/**
     * Checks if a variable definition is countable.
     *
     * @param ast the AST
     * @return true if the variable definition is countable, false otherwise
     */
private static boolean isVariableDefCountable(DetailAST ast) {
    boolean countable = false;
    //count variable definitions only if they are direct child to a slist or
    // object block
    final int parentType = ast.getParent().getType();
    if (parentType == TokenTypes.SLIST || parentType == TokenTypes.OBJBLOCK) {
        final DetailAST prevSibling = ast.getPreviousSibling();
        //is countable if no previous sibling is found or
        //the sibling is no COMMA.
        //This is done because multiple assignment on one line are counted
        // as 1
        countable = prevSibling == null || prevSibling.getType() != TokenTypes.COMMA;
    }
    return countable;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 75 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class ModifierOrderCheck method isAnnotationOnType.

/**
     * Checks whether annotation on type takes place.
     * @param modifier modifier token.
     * @return true if annotation on type takes place.
     */
private static boolean isAnnotationOnType(DetailAST modifier) {
    boolean annotationOnType = false;
    final DetailAST modifiers = modifier.getParent();
    final DetailAST definition = modifiers.getParent();
    final int definitionType = definition.getType();
    if (definitionType == TokenTypes.VARIABLE_DEF || definitionType == TokenTypes.PARAMETER_DEF || definitionType == TokenTypes.CTOR_DEF) {
        annotationOnType = true;
    } else if (definitionType == TokenTypes.METHOD_DEF) {
        final DetailAST typeToken = definition.findFirstToken(TokenTypes.TYPE);
        final int methodReturnType = typeToken.getLastChild().getType();
        if (methodReturnType != TokenTypes.LITERAL_VOID) {
            annotationOnType = true;
        }
    }
    return annotationOnType;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Aggregations

DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)397 Test (org.junit.Test)74 CommonHiddenStreamToken (antlr.CommonHiddenStreamToken)14 FullIdent (com.puppycrawl.tools.checkstyle.api.FullIdent)14 ArrayList (java.util.ArrayList)13 AST (antlr.collections.AST)8 Method (java.lang.reflect.Method)7 LinkedList (java.util.LinkedList)7 Scope (com.puppycrawl.tools.checkstyle.api.Scope)6 HashSet (java.util.HashSet)6 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)5 ArrayDeque (java.util.ArrayDeque)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)3 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 HashMap (java.util.HashMap)3