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>> mScopeStack =
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 aAST)
092    {
093        switch (aAST.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            mScopeStack.push(new HashMap<String, DetailAST>());
102            break;
103
104        case TokenTypes.PARAMETER_DEF:
105            if (ScopeUtils.inInterfaceBlock(aAST)
106                || inAbstractMethod(aAST))
107            {
108                break;
109            }
110        case TokenTypes.VARIABLE_DEF:
111            if ((aAST.getParent().getType() != TokenTypes.OBJBLOCK)
112                && (aAST.getParent().getType() != TokenTypes.FOR_EACH_CLAUSE))
113            {
114                insertVariable(aAST);
115            }
116            break;
117
118        case TokenTypes.IDENT:
119            final int parentType = aAST.getParent().getType();
120            if ((TokenTypes.POST_DEC        == parentType)
121                || (TokenTypes.DEC          == parentType)
122                || (TokenTypes.POST_INC     == parentType)
123                || (TokenTypes.INC          == parentType)
124                || (TokenTypes.ASSIGN       == parentType)
125                || (TokenTypes.PLUS_ASSIGN  == parentType)
126                || (TokenTypes.MINUS_ASSIGN == parentType)
127                || (TokenTypes.DIV_ASSIGN   == parentType)
128                || (TokenTypes.STAR_ASSIGN  == parentType)
129                || (TokenTypes.MOD_ASSIGN   == parentType)
130                || (TokenTypes.SR_ASSIGN    == parentType)
131                || (TokenTypes.BSR_ASSIGN   == parentType)
132                || (TokenTypes.SL_ASSIGN    == parentType)
133                || (TokenTypes.BXOR_ASSIGN  == parentType)
134                || (TokenTypes.BOR_ASSIGN   == parentType)
135                || (TokenTypes.BAND_ASSIGN  == parentType))
136            {
137                // TODO: is there better way to check is aAST
138                // in left part of assignment?
139                if (aAST.getParent().getFirstChild() == aAST) {
140                    removeVariable(aAST);
141                }
142            }
143            break;
144
145        default:
146        }
147    }
148
149    /**
150     * Determines whether an AST is a descentant of an abstract method.
151     * @param aAST the AST to check.
152     * @return true if aAST is a descentant of an abstract method.
153     */
154    private boolean inAbstractMethod(DetailAST aAST)
155    {
156        DetailAST parent = aAST.getParent();
157        while (parent != null) {
158            if (parent.getType() == TokenTypes.METHOD_DEF) {
159                final DetailAST modifiers =
160                    parent.findFirstToken(TokenTypes.MODIFIERS);
161                return modifiers.branchContains(TokenTypes.ABSTRACT);
162            }
163            parent = parent.getParent();
164        }
165        return false;
166    }
167
168    /**
169     * Inserts a variable at the topmost scope stack
170     * @param aAST the variable to insert
171     */
172    private void insertVariable(DetailAST aAST)
173    {
174        if (!aAST.branchContains(TokenTypes.FINAL)) {
175            final Map<String, DetailAST> state = mScopeStack.peek();
176            final DetailAST ast = aAST.findFirstToken(TokenTypes.IDENT);
177            state.put(ast.getText(), ast);
178        }
179    }
180
181    /**
182     * Removes the variable from the Stacks
183     * @param aAST Variable to remove
184     */
185    private void removeVariable(DetailAST aAST)
186    {
187        for (int i = mScopeStack.size() - 1; i >= 0; i--) {
188            final Map<String, DetailAST> state = mScopeStack.peek(i);
189            final Object obj = state.remove(aAST.getText());
190            if (obj != null) {
191                break;
192            }
193        }
194    }
195
196    @Override
197    public void leaveToken(DetailAST aAST)
198    {
199        super.leaveToken(aAST);
200
201        switch (aAST.getType()) {
202        case TokenTypes.OBJBLOCK:
203        case TokenTypes.SLIST:
204        case TokenTypes.LITERAL_FOR:
205        case TokenTypes.CTOR_DEF:
206        case TokenTypes.STATIC_INIT:
207        case TokenTypes.INSTANCE_INIT:
208        case TokenTypes.METHOD_DEF:
209            final Map<String, DetailAST> state = mScopeStack.pop();
210            for (DetailAST var : state.values()) {
211                log(var.getLineNo(), var.getColumnNo(), "final.variable", var
212                        .getText());
213            }
214            break;
215
216        default:
217        }
218    }
219}