Search in sources :

Example 41 with DetailAST

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

the class FinalLocalVariableCheck method shouldRemoveFinalVariableCandidate.

/**
     * Whether the final variable candidate should be removed from the list of final local variable
     * candidates.
     * @param scopeData the scope data of the variable.
     * @param ast the variable ast.
     * @return true, if the variable should be removed.
     */
private static boolean shouldRemoveFinalVariableCandidate(ScopeData scopeData, DetailAST ast) {
    boolean shouldRemove = true;
    for (DetailAST variable : scopeData.uninitializedVariables) {
        if (variable.getText().equals(ast.getText())) {
            // more than once in this case
            if (isInTheSameLoop(variable, ast) || !isUseOfExternalVariableInsideLoop(ast)) {
                final FinalVariableCandidate candidate = scopeData.scope.get(ast.getText());
                shouldRemove = candidate.alreadyAssigned;
            }
            scopeData.uninitializedVariables.remove(variable);
            break;
        }
    }
    return shouldRemove;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 42 with DetailAST

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

the class HiddenFieldCheck method processVariable.

/**
     * Process a variable token.
     * Check whether a local variable or parameter shadows a field.
     * Store a field for later comparison with local variables and parameters.
     * @param ast the variable token.
     */
private void processVariable(DetailAST ast) {
    if (!ScopeUtils.isInInterfaceOrAnnotationBlock(ast) && !CheckUtils.isReceiverParameter(ast) && (ScopeUtils.isLocalVariableDef(ast) || ast.getType() == TokenTypes.PARAMETER_DEF)) {
        // local variable or parameter. Does it shadow a field?
        final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
        final String name = nameAST.getText();
        if ((isStaticFieldHiddenFromAnonymousClass(ast, name) || isStaticOrInstanceField(ast, name)) && !isMatchingRegexp(name) && !isIgnoredParam(ast, name)) {
            log(nameAST, MSG_KEY, name);
        }
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 43 with DetailAST

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

the class JavadocTagInfoTest method testSerialField.

@Test
public void testSerialField() {
    final DetailAST ast = new DetailAST();
    final DetailAST astChild = new DetailAST();
    astChild.setType(TokenTypes.TYPE);
    ast.setFirstChild(astChild);
    final DetailAST astChild2 = new DetailAST();
    astChild2.setType(TokenTypes.ARRAY_DECLARATOR);
    astChild2.setText("ObjectStreafield");
    astChild.setFirstChild(astChild2);
    final int[] validTypes = { TokenTypes.VARIABLE_DEF };
    for (int type : validTypes) {
        ast.setType(type);
        assertTrue(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast));
    }
    astChild2.setText("1111");
    assertFalse(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast));
    astChild2.setType(TokenTypes.LITERAL_VOID);
    assertFalse(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast));
    ast.setType(TokenTypes.LAMBDA);
    assertFalse(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast));
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

Example 44 with DetailAST

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

the class JavadocTagInfoTest method testVersions.

@Test
public void testVersions() {
    final DetailAST ast = new DetailAST();
    final int[] validTypes = { TokenTypes.PACKAGE_DEF, TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF };
    for (int type : validTypes) {
        ast.setType(type);
        assertTrue(JavadocTagInfo.VERSION.isValidOn(ast));
    }
    ast.setType(TokenTypes.LAMBDA);
    assertFalse(JavadocTagInfo.VERSION.isValidOn(ast));
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

Example 45 with DetailAST

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

the class ModifiedControlVariableCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final ModifiedControlVariableCheck check = new ModifiedControlVariableCheck();
    final DetailAST classDefAst = new DetailAST();
    classDefAst.setType(TokenTypes.CLASS_DEF);
    try {
        check.visitToken(classDefAst);
        Assert.fail("IllegalStateException is expected");
    } catch (IllegalStateException ex) {
    // it is OK
    }
    try {
        check.leaveToken(classDefAst);
        Assert.fail("IllegalStateException is expected");
    } catch (IllegalStateException ex) {
    // it is OK
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

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