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;
}
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);
}
}
}
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));
}
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));
}
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
}
}
Aggregations