001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 the original author or authors.
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////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.coding;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Set;
027import java.util.regex.Pattern;
028
029import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
030import com.puppycrawl.tools.checkstyle.api.DetailAST;
031import com.puppycrawl.tools.checkstyle.api.FullIdent;
032import com.puppycrawl.tools.checkstyle.api.TokenTypes;
033import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
034import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
035
036/**
037 * Checks that particular class are never used as types in variable
038 * declarations, return values or parameters.
039 *
040 * <p>Rationale:
041 * Helps reduce coupling on concrete classes.
042 *
043 * <p>Check has following properties:
044 *
045 * <p><b>format</b> - Pattern for illegal class names.
046 *
047 * <p><b>legalAbstractClassNames</b> - Abstract classes that may be used as types.
048 *
049 * <p><b>illegalClassNames</b> - Classes that should not be used as types in variable
050   declarations, return values or parameters.
051 * It is possible to set illegal class names via short or
052 * <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.7">
053 *  canonical</a> name.
054 *  Specifying illegal type invokes analyzing imports and Check puts violations at
055 *   corresponding declarations
056 *  (of variables, methods or parameters). This helps to avoid ambiguous cases, e.g.:
057 *
058 * <p>{@code java.awt.List} was set as illegal class name, then, code like:
059 *
060 * <p>{@code
061 * import java.util.List;<br>
062 * ...<br>
063 * List list; //No violation here
064 * }
065 *
066 * <p>will be ok.
067 *
068 * <p><b>validateAbstractClassNames</b> - controls whether to validate abstract class names.
069 * Default value is <b>false</b>
070 * </p>
071 *
072 * <p><b>ignoredMethodNames</b> - Methods that should not be checked.
073 *
074 * <p><b>memberModifiers</b> - To check only methods and fields with only specified modifiers.
075 *
076 * <p>In most cases it's justified to put following classes to <b>illegalClassNames</b>:
077 * <ul>
078 * <li>GregorianCalendar</li>
079 * <li>Hashtable</li>
080 * <li>ArrayList</li>
081 * <li>LinkedList</li>
082 * <li>Vector</li>
083 * </ul>
084 *
085 * <p>as methods that are differ from interface methods are rear used, so in most cases user will
086 *  benefit from checking for them.
087 * </p>
088 *
089 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
090 * @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a>
091 * @author <a href="mailto:andreyselkin@gmail.com">Andrei Selkin</a>
092 */
093public final class IllegalTypeCheck extends AbstractCheck {
094
095    /**
096     * A key is pointing to the warning message text in "messages.properties"
097     * file.
098     */
099    public static final String MSG_KEY = "illegal.type";
100
101    /** Abstract classes legal by default. */
102    private static final String[] DEFAULT_LEGAL_ABSTRACT_NAMES = {};
103    /** Types illegal by default. */
104    private static final String[] DEFAULT_ILLEGAL_TYPES = {
105        "HashSet",
106        "HashMap",
107        "LinkedHashMap",
108        "LinkedHashSet",
109        "TreeSet",
110        "TreeMap",
111        "java.util.HashSet",
112        "java.util.HashMap",
113        "java.util.LinkedHashMap",
114        "java.util.LinkedHashSet",
115        "java.util.TreeSet",
116        "java.util.TreeMap",
117    };
118
119    /** Default ignored method names. */
120    private static final String[] DEFAULT_IGNORED_METHOD_NAMES = {
121        "getInitialContext",
122        "getEnvironment",
123    };
124
125    /** Illegal classes. */
126    private final Set<String> illegalClassNames = new HashSet<>();
127    /** Legal abstract classes. */
128    private final Set<String> legalAbstractClassNames = new HashSet<>();
129    /** Methods which should be ignored. */
130    private final Set<String> ignoredMethodNames = new HashSet<>();
131    /** Check methods and fields with only corresponding modifiers. */
132    private List<Integer> memberModifiers;
133
134    /** The regexp to match against. */
135    private Pattern format = Pattern.compile("^(.*[.])?Abstract.*$");
136
137    /**
138     * Controls whether to validate abstract class names.
139     */
140    private boolean validateAbstractClassNames;
141
142    /** Creates new instance of the check. */
143    public IllegalTypeCheck() {
144        setIllegalClassNames(DEFAULT_ILLEGAL_TYPES);
145        setLegalAbstractClassNames(DEFAULT_LEGAL_ABSTRACT_NAMES);
146        setIgnoredMethodNames(DEFAULT_IGNORED_METHOD_NAMES);
147    }
148
149    /**
150     * Set the format for the specified regular expression.
151     * @param pattern a pattern.
152     */
153    public void setFormat(Pattern pattern) {
154        format = pattern;
155    }
156
157    /**
158     * Sets whether to validate abstract class names.
159     * @param validateAbstractClassNames whether abstract class names must be ignored.
160     */
161    public void setValidateAbstractClassNames(boolean validateAbstractClassNames) {
162        this.validateAbstractClassNames = validateAbstractClassNames;
163    }
164
165    @Override
166    public int[] getDefaultTokens() {
167        return getAcceptableTokens();
168    }
169
170    @Override
171    public int[] getAcceptableTokens() {
172        return new int[] {
173            TokenTypes.VARIABLE_DEF,
174            TokenTypes.PARAMETER_DEF,
175            TokenTypes.METHOD_DEF,
176            TokenTypes.IMPORT,
177        };
178    }
179
180    @Override
181    public int[] getRequiredTokens() {
182        return new int[] {TokenTypes.IMPORT};
183    }
184
185    @Override
186    public void visitToken(DetailAST ast) {
187        switch (ast.getType()) {
188            case TokenTypes.METHOD_DEF:
189                if (isVerifiable(ast)) {
190                    visitMethodDef(ast);
191                }
192                break;
193            case TokenTypes.VARIABLE_DEF:
194                if (isVerifiable(ast)) {
195                    visitVariableDef(ast);
196                }
197                break;
198            case TokenTypes.PARAMETER_DEF:
199                visitParameterDef(ast);
200                break;
201            case TokenTypes.IMPORT:
202                visitImport(ast);
203                break;
204            default:
205                throw new IllegalStateException(ast.toString());
206        }
207    }
208
209    /**
210     * Checks if current method's return type or variable's type is verifiable
211     * according to <b>memberModifiers</b> option.
212     * @param methodOrVariableDef METHOD_DEF or VARIABLE_DEF ast node.
213     * @return true if member is verifiable according to <b>memberModifiers</b> option.
214     */
215    private boolean isVerifiable(DetailAST methodOrVariableDef) {
216        boolean result = true;
217        if (memberModifiers != null) {
218            final DetailAST modifiersAst = methodOrVariableDef
219                    .findFirstToken(TokenTypes.MODIFIERS);
220            result = isContainVerifiableType(modifiersAst);
221        }
222        return result;
223    }
224
225    /**
226     * Checks is modifiers contain verifiable type.
227     *
228     * @param modifiers
229     *            parent node for all modifiers
230     * @return true if method or variable can be verified
231     */
232    private boolean isContainVerifiableType(DetailAST modifiers) {
233        boolean result = false;
234        if (modifiers.getFirstChild() != null) {
235            for (DetailAST modifier = modifiers.getFirstChild(); modifier != null;
236                     modifier = modifier.getNextSibling()) {
237                if (memberModifiers.contains(modifier.getType())) {
238                    result = true;
239                }
240            }
241        }
242        return result;
243    }
244
245    /**
246     * Checks return type of a given method.
247     * @param methodDef method for check.
248     */
249    private void visitMethodDef(DetailAST methodDef) {
250        if (isCheckedMethod(methodDef)) {
251            checkClassName(methodDef);
252        }
253    }
254
255    /**
256     * Checks type of parameters.
257     * @param parameterDef parameter list for check.
258     */
259    private void visitParameterDef(DetailAST parameterDef) {
260        final DetailAST grandParentAST = parameterDef.getParent().getParent();
261
262        if (grandParentAST.getType() == TokenTypes.METHOD_DEF
263            && isCheckedMethod(grandParentAST)) {
264            checkClassName(parameterDef);
265        }
266    }
267
268    /**
269     * Checks type of given variable.
270     * @param variableDef variable to check.
271     */
272    private void visitVariableDef(DetailAST variableDef) {
273        checkClassName(variableDef);
274    }
275
276    /**
277     * Checks imported type (as static and star imports are not supported by Check,
278     *  only type is in the consideration).<br>
279     * If this type is illegal due to Check's options - puts violation on it.
280     * @param importAst {@link TokenTypes#IMPORT Import}
281     */
282    private void visitImport(DetailAST importAst) {
283        if (!isStarImport(importAst)) {
284            final String canonicalName = getImportedTypeCanonicalName(importAst);
285            extendIllegalClassNamesWithShortName(canonicalName);
286        }
287    }
288
289    /**
290     * Checks if current import is star import. E.g.:
291     * <p>
292     * {@code
293     * import java.util.*;
294     * }
295     * </p>
296     * @param importAst {@link TokenTypes#IMPORT Import}
297     * @return true if it is star import
298     */
299    private static boolean isStarImport(DetailAST importAst) {
300        boolean result = false;
301        DetailAST toVisit = importAst;
302        while (toVisit != null) {
303            toVisit = getNextSubTreeNode(toVisit, importAst);
304            if (toVisit != null && toVisit.getType() == TokenTypes.STAR) {
305                result = true;
306                break;
307            }
308        }
309        return result;
310    }
311
312    /**
313     * Checks type of given method, parameter or variable.
314     * @param ast node to check.
315     */
316    private void checkClassName(DetailAST ast) {
317        final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
318        final FullIdent ident = CheckUtils.createFullType(type);
319
320        if (isMatchingClassName(ident.getText())) {
321            log(ident.getLineNo(), ident.getColumnNo(),
322                MSG_KEY, ident.getText());
323        }
324    }
325
326    /**
327     * @param className class name to check.
328     * @return true if given class name is one of illegal classes
329     *         or if it matches to abstract class names pattern.
330     */
331    private boolean isMatchingClassName(String className) {
332        final String shortName = className.substring(className.lastIndexOf('.') + 1);
333        return illegalClassNames.contains(className)
334                || illegalClassNames.contains(shortName)
335                || validateAbstractClassNames
336                    && !legalAbstractClassNames.contains(className)
337                    && format.matcher(className).find();
338    }
339
340    /**
341     * Extends illegal class names set via imported short type name.
342     * @param canonicalName
343     *  <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.7">
344     *  Canonical</a> name of imported type.
345     */
346    private void extendIllegalClassNamesWithShortName(String canonicalName) {
347        if (illegalClassNames.contains(canonicalName)) {
348            final String shortName = canonicalName
349                .substring(canonicalName.lastIndexOf('.') + 1);
350            illegalClassNames.add(shortName);
351        }
352    }
353
354    /**
355     * Gets imported type's
356     * <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.7">
357     *  canonical name</a>.
358     * @param importAst {@link TokenTypes#IMPORT Import}
359     * @return Imported canonical type's name.
360     */
361    private static String getImportedTypeCanonicalName(DetailAST importAst) {
362        final StringBuilder canonicalNameBuilder = new StringBuilder();
363        DetailAST toVisit = importAst;
364        while (toVisit != null) {
365            toVisit = getNextSubTreeNode(toVisit, importAst);
366            if (toVisit != null && toVisit.getType() == TokenTypes.IDENT) {
367                canonicalNameBuilder.append(toVisit.getText());
368                final DetailAST nextSubTreeNode = getNextSubTreeNode(toVisit, importAst);
369                if (nextSubTreeNode.getType() != TokenTypes.SEMI) {
370                    canonicalNameBuilder.append('.');
371                }
372            }
373        }
374        return canonicalNameBuilder.toString();
375    }
376
377    /**
378     * Gets the next node of a syntactical tree (child of a current node or
379     * sibling of a current node, or sibling of a parent of a current node).
380     * @param currentNodeAst Current node in considering
381     * @param subTreeRootAst SubTree root
382     * @return Current node after bypassing, if current node reached the root of a subtree
383     *        method returns null
384     */
385    private static DetailAST
386        getNextSubTreeNode(DetailAST currentNodeAst, DetailAST subTreeRootAst) {
387        DetailAST currentNode = currentNodeAst;
388        DetailAST toVisitAst = currentNode.getFirstChild();
389        while (toVisitAst == null) {
390            toVisitAst = currentNode.getNextSibling();
391            if (toVisitAst == null) {
392                if (currentNode.getParent().equals(subTreeRootAst)) {
393                    break;
394                }
395                currentNode = currentNode.getParent();
396            }
397        }
398        return toVisitAst;
399    }
400
401    /**
402     * @param ast method def to check.
403     * @return true if we should check this method.
404     */
405    private boolean isCheckedMethod(DetailAST ast) {
406        final String methodName =
407            ast.findFirstToken(TokenTypes.IDENT).getText();
408        return !ignoredMethodNames.contains(methodName);
409    }
410
411    /**
412     * Set the list of illegal variable types.
413     * @param classNames array of illegal variable types
414     */
415    public void setIllegalClassNames(String... classNames) {
416        illegalClassNames.clear();
417        Collections.addAll(illegalClassNames, classNames);
418    }
419
420    /**
421     * Set the list of ignore method names.
422     * @param methodNames array of ignored method names
423     */
424    public void setIgnoredMethodNames(String... methodNames) {
425        ignoredMethodNames.clear();
426        Collections.addAll(ignoredMethodNames, methodNames);
427    }
428
429    /**
430     * Set the list of legal abstract class names.
431     * @param classNames array of legal abstract class names
432     */
433    public void setLegalAbstractClassNames(String... classNames) {
434        legalAbstractClassNames.clear();
435        Collections.addAll(legalAbstractClassNames, classNames);
436    }
437
438    /**
439     * Set the list of member modifiers (of methods and fields) which should be checked.
440     * @param modifiers String contains modifiers.
441     */
442    public void setMemberModifiers(String modifiers) {
443        final List<Integer> modifiersList = new ArrayList<>();
444        for (String modifier : modifiers.split(",")) {
445            modifiersList.add(TokenUtils.getTokenId(modifier.trim()));
446        }
447        memberModifiers = modifiersList;
448    }
449}