Search in sources :

Example 56 with DetailAST

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

the class SuppressWarningsHolderTest method testGetAnnotationValuesWrongArg.

@Test
public void testGetAnnotationValuesWrongArg() throws ReflectiveOperationException {
    final SuppressWarningsHolder holder = new SuppressWarningsHolder();
    final Method getAllAnnotationValues = holder.getClass().getDeclaredMethod("getAnnotationValues", DetailAST.class);
    getAllAnnotationValues.setAccessible(true);
    final DetailAST methodDef = new DetailAST();
    methodDef.setType(TokenTypes.METHOD_DEF);
    methodDef.setText("Method Def");
    methodDef.setLineNo(0);
    methodDef.setColumnNo(0);
    try {
        getAllAnnotationValues.invoke(holder, methodDef);
        fail("Exception expected");
    } catch (InvocationTargetException ex) {
        assertTrue(ex.getCause() instanceof IllegalArgumentException);
        assertEquals("Expression or annotation array" + " initializer AST expected: Method Def[0x0]", ex.getCause().getMessage());
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 57 with DetailAST

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

the class VisibilityModifierCheck method findMatchingAnnotation.

/**
     * Checks whether the AST is annotated with
     * an annotation containing the passed in regular
     * expression and return the AST representing that
     * annotation.
     *
     * <p>
     * This method will not look for imports or package
     * statements to detect the passed in annotation.
     * </p>
     *
     * <p>
     * To check if an AST contains a passed in annotation
     * taking into account fully-qualified names
     * (ex: java.lang.Override, Override)
     * this method will need to be called twice. Once for each
     * name given.
     * </p>
     *
     * @param variableDef {@link TokenTypes#VARIABLE_DEF variable def node}.
     * @return the AST representing the first such annotation or null if
     *         no such annotation was found
     */
private DetailAST findMatchingAnnotation(DetailAST variableDef) {
    DetailAST matchingAnnotation = null;
    final DetailAST holder = AnnotationUtility.getAnnotationHolder(variableDef);
    for (DetailAST child = holder.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getType() == TokenTypes.ANNOTATION) {
            final DetailAST ast = child.getFirstChild();
            final String name = FullIdent.createFullIdent(ast.getNextSibling()).getText();
            if (ignoreAnnotationCanonicalNames.contains(name) || ignoreAnnotationShortNames.contains(name)) {
                matchingAnnotation = child;
                break;
            }
        }
    }
    return matchingAnnotation;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 58 with DetailAST

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

the class VisibilityModifierCheck method getNextSubTreeNode.

/**
     * Gets the next node of a syntactical tree (child of a current node or
     * sibling of a current node, or sibling of a parent of a current node).
     * @param currentNodeAst Current node in considering
     * @param subTreeRootAst SubTree root
     * @return Current node after bypassing, if current node reached the root of a subtree
     *        method returns null
     */
private static DetailAST getNextSubTreeNode(DetailAST currentNodeAst, DetailAST subTreeRootAst) {
    DetailAST currentNode = currentNodeAst;
    DetailAST toVisitAst = currentNode.getFirstChild();
    while (toVisitAst == null) {
        toVisitAst = currentNode.getNextSibling();
        if (toVisitAst == null) {
            if (currentNode.getParent().equals(subTreeRootAst) && currentNode.getParent().getColumnNo() == subTreeRootAst.getColumnNo()) {
                break;
            }
            currentNode = currentNode.getParent();
        }
    }
    return toVisitAst;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 59 with DetailAST

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

the class VisibilityModifierCheck method getModifiers.

/**
     * Returns the set of modifier Strings for a VARIABLE_DEF or CLASS_DEF AST.
     * @param defAST AST for a variable or class definition.
     * @return the set of modifier Strings for defAST.
     */
private static Set<String> getModifiers(DetailAST defAST) {
    final AST modifiersAST = defAST.findFirstToken(TokenTypes.MODIFIERS);
    final Set<String> modifiersSet = new HashSet<>();
    if (modifiersAST != null) {
        AST modifier = modifiersAST.getFirstChild();
        while (modifier != null) {
            modifiersSet.add(modifier.getText());
            modifier = modifier.getNextSibling();
        }
    }
    return modifiersSet;
}
Also used : AST(antlr.collections.AST) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) HashSet(java.util.HashSet)

Example 60 with DetailAST

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

the class AvoidStaticImportCheck method visitToken.

@Override
public void visitToken(final DetailAST ast) {
    final DetailAST startingDot = ast.getFirstChild().getNextSibling();
    final FullIdent name = FullIdent.createFullIdent(startingDot);
    if (!isExempt(name.getText())) {
        log(startingDot.getLineNo(), MSG_KEY, name.getText());
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FullIdent(com.puppycrawl.tools.checkstyle.api.FullIdent)

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