|| ast.getParent().getParent().findFirstToken(TokenTypes.CASE_GROUP) == ast.getParent()) { storePrevScopeUninitializedVariableData(); scopeStack.push(new ScopeData()); if (!isInLambda(ast) && ast.findFirstToken(TokenTypes.MODIFIERS) .findFirstToken(TokenTypes.FINAL) == null && !isInAbstractOrNativeMethod(ast) && !ScopeUtil.isInInterfaceBlock(ast) && !isMultipleTypeCatch(ast) && !CheckUtil.isReceiverParameter(ast)) { insertParameter(ast); && ast.findFirstToken(TokenTypes.MODIFIERS) .findFirstToken(TokenTypes.FINAL) == null && !isVariableInForInit(ast) && shouldCheckEnhancedForLoopVariable(ast)) { insertVariable(ast); if (isAssignOperator(parentType) && isFirstChild(ast)) { final Optional<FinalVariableCandidate> candidate = getFinalCandidate(ast); if (candidate.isPresent()) { determineAssignmentConditions(ast, candidate.get()); currentScopeAssignedVariables.peek().add(ast); removeFinalVariableCandidateFromStack(ast);
/** * Check if both the Variables are same. * @param ast1 Variable to compare * @param ast2 Variable to compare * @return true if both the variables are same, otherwise false */ private static boolean isSameVariables(DetailAST ast1, DetailAST ast2) { final DetailAST classOrMethodOfAst1 = findFirstUpperNamedBlock(ast1); final DetailAST classOrMethodOfAst2 = findFirstUpperNamedBlock(ast2); return classOrMethodOfAst1 == classOrMethodOfAst2 && ast1.getText().equals(ast2.getText()); }
boolean containsBreak = false; if (ast.getParent().getType() != TokenTypes.CASE_GROUP || findLastChildWhichContainsSpecifiedToken(ast.getParent().getParent(), TokenTypes.CASE_GROUP, TokenTypes.SLIST) == ast.getParent()) { containsBreak = scopeStack.peek().containsBreak; if (containsBreak || shouldUpdateUninitializedVariables(parent)) { updateAllUninitializedVariables(prevScopeUninitializedVariableData); updateCurrentScopeAssignedVariables(); break; default: for (FinalVariableCandidate candidate : scope.values()) { final DetailAST ident = candidate.variableIdent; log(ident, MSG_KEY, ident.getText());
/** * If token is LITERAL_IF and there is an {@code else} following or token is CASE_GROUP and * there is another {@code case} following, then update the uninitialized variables. * @param ast token to be checked * @return true if should be updated, else false */ private static boolean shouldUpdateUninitializedVariables(DetailAST ast) { return isIfTokenWithAnElseFollowing(ast) || isCaseTokenWithAnotherCaseFollowing(ast); }
@Test public void testImproperToken() { final FinalLocalVariableCheck check = new FinalLocalVariableCheck(); final DetailAST lambdaAst = new DetailAST(); lambdaAst.setType(TokenTypes.LAMBDA); try { check.visitToken(lambdaAst); Assert.fail("IllegalStateException is expected"); } catch (IllegalStateException ex) { // it is OK } }
/** * If token is CASE_GROUP and there is another {@code case} following. * @param ast token to be checked * @return true if token is CASE_GROUP and there is another {@code case} following, else false */ private static boolean isCaseTokenWithAnotherCaseFollowing(DetailAST ast) { return ast.getType() == TokenTypes.CASE_GROUP && findLastChildWhichContainsSpecifiedToken( ast.getParent(), TokenTypes.CASE_GROUP, TokenTypes.SLIST) != ast; }
/** * If token is LITERAL_IF and there is an {@code else} following or token is CASE_GROUP and * there is another {@code case} following, then update the uninitialized variables. * @param ast token to be checked * @return true if should be updated, else false */ private static boolean shouldUpdateUninitializedVariables(DetailAST ast) { return isIfTokenWithAnElseFollowing(ast) || isCaseTokenWithAnotherCaseFollowing(ast); }
/** * If token is CASE_GROUP and there is another {@code case} following. * @param ast token to be checked * @return true if token is CASE_GROUP and there is another {@code case} following, else false */ private static boolean isCaseTokenWithAnotherCaseFollowing(DetailAST ast) { return ast.getType() == TokenTypes.CASE_GROUP && findLastChildWhichContainsSpecifiedToken( ast.getParent(), TokenTypes.CASE_GROUP, TokenTypes.SLIST) != ast; }
|| ast.getParent().getParent().findFirstToken(TokenTypes.CASE_GROUP) == ast.getParent()) { storePrevScopeUninitializedVariableData(); scopeStack.push(new ScopeData()); if (!isInLambda(ast) && ast.findFirstToken(TokenTypes.MODIFIERS) .findFirstToken(TokenTypes.FINAL) == null && !isInAbstractOrNativeMethod(ast) && !ScopeUtil.isInInterfaceBlock(ast) && !isMultipleTypeCatch(ast) && !CheckUtil.isReceiverParameter(ast)) { insertParameter(ast); && ast.findFirstToken(TokenTypes.MODIFIERS) .findFirstToken(TokenTypes.FINAL) == null && !isVariableInForInit(ast) && shouldCheckEnhancedForLoopVariable(ast)) { insertVariable(ast); if (isAssignOperator(parentType) && isFirstChild(ast)) { final Optional<FinalVariableCandidate> candidate = getFinalCandidate(ast); if (candidate.isPresent()) { determineAssignmentConditions(ast, candidate.get()); currentScopeAssignedVariables.peek().add(ast); removeFinalVariableCandidateFromStack(ast);
boolean containsBreak = false; if (ast.getParent().getType() != TokenTypes.CASE_GROUP || findLastChildWhichContainsSpecifiedToken(ast.getParent().getParent(), TokenTypes.CASE_GROUP, TokenTypes.SLIST) == ast.getParent()) { containsBreak = scopeStack.peek().containsBreak; if (containsBreak || shouldUpdateUninitializedVariables(parent)) { updateAllUninitializedVariables(prevScopeUninitializedVariableData); updateCurrentScopeAssignedVariables(); break; default: for (FinalVariableCandidate candidate : scope.values()) { final DetailAST ident = candidate.variableIdent; log(ident, MSG_KEY, ident.getText());
/** * Check if both the Variables are same. * @param ast1 Variable to compare * @param ast2 Variable to compare * @return true if both the variables are same, otherwise false */ private static boolean isSameVariables(DetailAST ast1, DetailAST ast2) { final DetailAST classOrMethodOfAst1 = findFirstUpperNamedBlock(ast1); final DetailAST classOrMethodOfAst2 = findFirstUpperNamedBlock(ast2); return classOrMethodOfAst1 == classOrMethodOfAst2 && ast1.getText().equals(ast2.getText()); }