001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.coding;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.FastStack;
024import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * <p>
031 * Ensures that local variables that never get their values changed,
032 * must be declared final.
033 * </p>
034 * <p>
035 * An example of how to configure the check is:
036 * </p>
037 * <pre>
038 * &lt;module name="FinalLocalVariable"&gt;
039 *     &lt;property name="token" value="VARIABLE_DEF"/&gt;
040 * &lt;/module&gt;
041 * </pre>
042 * @author k_gibbs, r_auckenthaler
043 */
044public class FinalLocalVariableCheck extends Check
045{
046    /** Scope Stack */
047    private final FastStack<Map<String, DetailAST>> scopeStack =
048        FastStack.newInstance();
049
050    @Override
051    public int[] getDefaultTokens()
052    {
053        return new int[] {
054            TokenTypes.IDENT,
055            TokenTypes.CTOR_DEF,
056            TokenTypes.METHOD_DEF,
057            TokenTypes.VARIABLE_DEF,
058            TokenTypes.INSTANCE_INIT,
059            TokenTypes.STATIC_INIT,
060            TokenTypes.LITERAL_FOR,
061            TokenTypes.SLIST,
062            TokenTypes.OBJBLOCK,
063        };
064    }
065
066    @Override
067    public int[] getAcceptableTokens()
068    {
069        return new int[] {
070            TokenTypes.VARIABLE_DEF,
071            TokenTypes.PARAMETER_DEF,
072        };
073    }
074
075    @Override
076    public int[] getRequiredTokens()
077    {
078        return new int[] {
079            TokenTypes.IDENT,
080            TokenTypes.CTOR_DEF,
081            TokenTypes.METHOD_DEF,
082            TokenTypes.INSTANCE_INIT,
083            TokenTypes.STATIC_INIT,
084            TokenTypes.LITERAL_FOR,
085            TokenTypes.SLIST,
086            TokenTypes.OBJBLOCK,
087        };
088    }
089
090    @Override
091    public void visitToken(DetailAST ast)
092    {
093        switch (ast.getType()) {
094            case TokenTypes.OBJBLOCK:
095            case TokenTypes.SLIST:
096            case TokenTypes.LITERAL_FOR:
097            case TokenTypes.METHOD_DEF:
098            case TokenTypes.CTOR_DEF:
099            case TokenTypes.STATIC_INIT:
100            case TokenTypes.INSTANCE_INIT:
101                scopeStack.push(new HashMap<String, DetailAST>());
102                break;
103
104            case TokenTypes.PARAMETER_DEF:
105                if (ScopeUtils.inInterfaceBlock(ast)
106                    || inAbstractOrNativeMethod(ast))
107                {
108                    break;
109                }
110            case TokenTypes.VARIABLE_DEF:
111                if ((ast.getParent().getType() != TokenTypes.OBJBLOCK)
112                    && (ast.getParent().getType() != TokenTypes.FOR_EACH_CLAUSE)
113                    && isFirstVariableInForInit(ast))
114                {
115                    insertVariable(ast);
116                }
117                break;
118
119            case TokenTypes.IDENT:
120                final int parentType = ast.getParent().getType();
121                if ((TokenTypes.POST_DEC        == parentType)
122                    || (TokenTypes.DEC          == parentType)
123                    || (TokenTypes.POST_INC     == parentType)
124                    || (TokenTypes.INC          == parentType)
125                    || (TokenTypes.ASSIGN       == parentType)
126                    || (TokenTypes.PLUS_ASSIGN  == parentType)
127                    || (TokenTypes.MINUS_ASSIGN == parentType)
128                    || (TokenTypes.DIV_ASSIGN   == parentType)
129                    || (TokenTypes.STAR_ASSIGN  == parentType)
130                    || (TokenTypes.MOD_ASSIGN   == parentType)
131                    || (TokenTypes.SR_ASSIGN    == parentType)
132                    || (TokenTypes.BSR_ASSIGN   == parentType)
133                    || (TokenTypes.SL_ASSIGN    == parentType)
134                    || (TokenTypes.BXOR_ASSIGN  == parentType)
135                    || (TokenTypes.BOR_ASSIGN   == parentType)
136                    || (TokenTypes.BAND_ASSIGN  == parentType))
137                {
138                    // TODO: is there better way to check is ast
139                    // in left part of assignment?
140                    if (ast.getParent().getFirstChild() == ast) {
141                        removeVariable(ast);
142                    }
143                }
144                break;
145
146            default:
147        }
148    }
149
150    /**
151     * Checks if current variable is defined first in
152     *  {@link TokenTypes#FOR_INIT for-loop init}, e.g.:
153     * <p>
154     * <code>
155     * for (int i = 0, j = 0; i < j; i++) { . . . }
156     * </code>
157     * </p>
158     * <code>i</code> is first variable in {@link TokenTypes#FOR_INIT for-loop init}
159     * @param variableDef variable definition node.
160     * @return true if variableDef is first variable in {@link TokenTypes#FOR_INIT for-loop init}
161     */
162    private static boolean isFirstVariableInForInit(DetailAST variableDef)
163    {
164        return variableDef.getParent().getType() != TokenTypes.FOR_INIT
165                 || variableDef.getPreviousSibling() == null
166                 || variableDef.getPreviousSibling().getType() != TokenTypes.COMMA;
167    }
168
169    /**
170     * Determines whether an AST is a descendant of an abstract or native method.
171     * @param ast the AST to check.
172     * @return true if ast is a descendant of an abstract or native method.
173     */
174    private static boolean inAbstractOrNativeMethod(DetailAST ast)
175    {
176        DetailAST parent = ast.getParent();
177        while (parent != null) {
178            if (parent.getType() == TokenTypes.METHOD_DEF) {
179                final DetailAST modifiers =
180                    parent.findFirstToken(TokenTypes.MODIFIERS);
181                return modifiers.branchContains(TokenTypes.ABSTRACT)
182                        || modifiers.branchContains(TokenTypes.LITERAL_NATIVE);
183            }
184            parent = parent.getParent();
185        }
186        return false;
187    }
188
189    /**
190     * Inserts a variable at the topmost scope stack
191     * @param ast the variable to insert
192     */
193    private void insertVariable(DetailAST ast)
194    {
195        if (!ast.branchContains(TokenTypes.FINAL)) {
196            final Map<String, DetailAST> state = scopeStack.peek();
197            final DetailAST astNode = ast.findFirstToken(TokenTypes.IDENT);
198            state.put(astNode.getText(), astNode);
199        }
200    }
201
202    /**
203     * Removes the variable from the Stacks
204     * @param ast Variable to remove
205     */
206    private void removeVariable(DetailAST ast)
207    {
208        for (int i = scopeStack.size() - 1; i >= 0; i--) {
209            final Map<String, DetailAST> state = scopeStack.peek(i);
210            final Object obj = state.remove(ast.getText());
211            if (obj != null) {
212                break;
213            }
214        }
215    }
216
217    @Override
218    public void leaveToken(DetailAST ast)
219    {
220        super.leaveToken(ast);
221
222        switch (ast.getType()) {
223            case TokenTypes.OBJBLOCK:
224            case TokenTypes.SLIST:
225            case TokenTypes.LITERAL_FOR:
226            case TokenTypes.CTOR_DEF:
227            case TokenTypes.STATIC_INIT:
228            case TokenTypes.INSTANCE_INIT:
229            case TokenTypes.METHOD_DEF:
230                final Map<String, DetailAST> state = scopeStack.pop();
231                for (DetailAST var : state.values()) {
232                    log(var.getLineNo(), var.getColumnNo(), "final.variable", var
233                        .getText());
234                }
235                break;
236
237            default:
238        }
239    }
240}