001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.modifier;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.lang3.ArrayUtils;
026
027import com.puppycrawl.tools.checkstyle.api.Check;
028import com.puppycrawl.tools.checkstyle.api.DetailAST;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030
031/**
032 * Checks for redundant modifiers in interface and annotation definitions,
033 * final modifier on methods of final classes, inner <code>interface</code>
034 * declarations that are declared as <code>static</code>, non public class
035 * constructors and enum constructors, nested enum definitions that are declared
036 * as <code>static</code>.
037 *
038 * <p>Interfaces by definition are abstract so the <code>abstract</code>
039 * modifier on the interface is redundant.
040 *
041 * <p>Classes inside of interfaces by definition are public and static,
042 * so the <code>public</code> and <code>static</code> modifiers
043 * on the inner classes are redundant. On the other hand, classes
044 * inside of interfaces can be abstract or non abstract.
045 * So, <code>abstract</code> modifier is allowed.
046 *
047 * <p>Fields in interfaces and annotations are automatically
048 * public, static and final, so these modifiers are redundant as
049 * well.</p>
050 *
051 * <p>As annotations are a form of interface, their fields are also
052 * automatically public, static and final just as their
053 * annotation fields are automatically public and abstract.</p>
054 *
055 * <p>Enums by definition are static implicit subclasses of java.lang.Enum&#60;E&#62;.
056 * So, the <code>static</code> modifier on the enums is redundant. In addition,
057 * if enum is inside of interface, <code>public</code> modifier is also redundant.
058 *
059 * <p>Final classes by definition cannot be extended so the <code>final</code>
060 * modifier on the method of a final class is redundant.
061 *
062 * <p>Public modifier for constructors in non-public non-protected classes
063 * is always obsolete: </p>
064 *
065 * <pre>
066 * public class PublicClass {
067 *     public PublicClass() {} // OK
068 * }
069 *
070 * class PackagePrivateClass {
071 *     public PackagePrivateClass() {} // violation expected
072 * }
073 * </pre>
074 *
075 * <p>There is no violation in the following example,
076 * because removing public modifier from ProtectedInnerClass
077 * constructor will make this code not compiling: </p>
078 *
079 * <pre>
080 * package a;
081 * public class ClassExample {
082 *     protected class ProtectedInnerClass {
083 *         public ProtectedInnerClass () {}
084 *     }
085 * }
086 *
087 * package b;
088 * import a.ClassExample;
089 * public class ClassExtending extends ClassExample {
090 *     ProtectedInnerClass pc = new ProtectedInnerClass();
091 * }
092 * </pre>
093 *
094 * @author lkuehne
095 * @author <a href="mailto:piotr.listkiewicz@gmail.com">liscju</a>
096 * @author <a href="mailto:andreyselkin@gmail.com">Andrei Selkin</a>
097 * @author Vladislav Lisetskiy
098 */
099public class RedundantModifierCheck
100    extends Check {
101
102    /**
103     * A key is pointing to the warning message text in "messages.properties"
104     * file.
105     */
106    public static final String MSG_KEY = "redundantModifier";
107
108    /**
109     * An array of tokens for interface modifiers.
110     */
111    private static final int[] TOKENS_FOR_INTERFACE_MODIFIERS = {
112        TokenTypes.LITERAL_STATIC,
113        TokenTypes.ABSTRACT,
114    };
115
116    @Override
117    public int[] getDefaultTokens() {
118        return getAcceptableTokens();
119    }
120
121    @Override
122    public int[] getRequiredTokens() {
123        return ArrayUtils.EMPTY_INT_ARRAY;
124    }
125
126    @Override
127    public int[] getAcceptableTokens() {
128        return new int[] {
129            TokenTypes.METHOD_DEF,
130            TokenTypes.VARIABLE_DEF,
131            TokenTypes.ANNOTATION_FIELD_DEF,
132            TokenTypes.INTERFACE_DEF,
133            TokenTypes.CTOR_DEF,
134            TokenTypes.CLASS_DEF,
135            TokenTypes.ENUM_DEF,
136        };
137    }
138
139    @Override
140    public void visitToken(DetailAST ast) {
141        if (ast.getType() == TokenTypes.INTERFACE_DEF) {
142            checkInterfaceModifiers(ast);
143        }
144        else if (ast.getType() == TokenTypes.CTOR_DEF) {
145            if (isEnumMember(ast)) {
146                checkEnumConstructorModifiers(ast);
147            }
148            else {
149                checkClassConstructorModifiers(ast);
150            }
151        }
152        else if (ast.getType() == TokenTypes.ENUM_DEF) {
153            checkEnumDef(ast);
154        }
155        else if (isInterfaceOrAnnotationMember(ast)) {
156            processInterfaceOrAnnotation(ast);
157        }
158        else if (ast.getType() == TokenTypes.METHOD_DEF) {
159            processMethods(ast);
160        }
161    }
162
163    /**
164     * Checks if interface has proper modifiers.
165     * @param ast interface to check
166     */
167    private void checkInterfaceModifiers(DetailAST ast) {
168        final DetailAST modifiers =
169            ast.findFirstToken(TokenTypes.MODIFIERS);
170
171        for (final int tokenType : TOKENS_FOR_INTERFACE_MODIFIERS) {
172            final DetailAST modifier =
173                    modifiers.findFirstToken(tokenType);
174            if (modifier != null) {
175                log(modifier.getLineNo(), modifier.getColumnNo(),
176                        MSG_KEY, modifier.getText());
177            }
178        }
179    }
180
181    /**
182     * Check if enum constructor has proper modifiers.
183     * @param ast constructor of enum
184     */
185    private void checkEnumConstructorModifiers(DetailAST ast) {
186        final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
187        final DetailAST modifier = modifiers.getFirstChild();
188        if (modifier != null) {
189            log(modifier.getLineNo(), modifier.getColumnNo(),
190                    MSG_KEY, modifier.getText());
191        }
192    }
193
194    /**
195     * Checks whether enum has proper modifiers.
196     * @param ast enum definition.
197     */
198    private void checkEnumDef(DetailAST ast) {
199        if (isInterfaceOrAnnotationMember(ast)) {
200            processInterfaceOrAnnotation(ast);
201        }
202        else if (ast.getParent() != null) {
203            final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
204            final DetailAST staticModifier = modifiers.findFirstToken(TokenTypes.LITERAL_STATIC);
205            if (staticModifier != null) {
206                log(staticModifier.getLineNo(), staticModifier.getColumnNo(),
207                        MSG_KEY, staticModifier.getText());
208            }
209        }
210    }
211
212    /**
213     * Do validation of interface of annotation.
214     * @param ast token AST
215     */
216    private void processInterfaceOrAnnotation(DetailAST ast) {
217        final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
218        DetailAST modifier = modifiers.getFirstChild();
219        while (modifier != null) {
220
221            // javac does not allow final or static in interface methods
222            // order annotation fields hence no need to check that this
223            // is not a method or annotation field
224
225            final int type = modifier.getType();
226            if (type == TokenTypes.LITERAL_PUBLIC
227                || type == TokenTypes.LITERAL_STATIC
228                        && ast.getType() != TokenTypes.METHOD_DEF
229                || type == TokenTypes.ABSTRACT
230                        && ast.getType() != TokenTypes.CLASS_DEF
231                || type == TokenTypes.FINAL
232                        && ast.getType() != TokenTypes.CLASS_DEF) {
233                log(modifier.getLineNo(), modifier.getColumnNo(),
234                        MSG_KEY, modifier.getText());
235                break;
236            }
237
238            modifier = modifier.getNextSibling();
239        }
240    }
241
242    /**
243     * Process validation ofMethods.
244     * @param ast method AST
245     */
246    private void processMethods(DetailAST ast) {
247        final DetailAST modifiers =
248                        ast.findFirstToken(TokenTypes.MODIFIERS);
249        // private method?
250        boolean checkFinal =
251            modifiers.branchContains(TokenTypes.LITERAL_PRIVATE);
252        // declared in a final class?
253        DetailAST parent = ast.getParent();
254        while (parent != null) {
255            if (parent.getType() == TokenTypes.CLASS_DEF) {
256                final DetailAST classModifiers =
257                    parent.findFirstToken(TokenTypes.MODIFIERS);
258                checkFinal |=
259                    classModifiers.branchContains(TokenTypes.FINAL);
260                break;
261            }
262            if (parent.getType() == TokenTypes.LITERAL_NEW) {
263                checkFinal = true;
264                break;
265            }
266            parent = parent.getParent();
267        }
268        if (checkFinal && !isAnnotatedWithSafeVarargs(ast)) {
269            DetailAST modifier = modifiers.getFirstChild();
270            while (modifier != null) {
271                final int type = modifier.getType();
272                if (type == TokenTypes.FINAL) {
273                    log(modifier.getLineNo(), modifier.getColumnNo(),
274                            MSG_KEY, modifier.getText());
275                    break;
276                }
277                modifier = modifier.getNextSibling();
278            }
279        }
280    }
281
282    /**
283     * Check if class constructor has proper modifiers.
284     * @param classCtorAst class constructor ast
285     */
286    private void checkClassConstructorModifiers(DetailAST classCtorAst) {
287        final DetailAST classDef = classCtorAst.getParent().getParent();
288        if (!isClassPublic(classDef) && !isClassProtected(classDef)) {
289            checkForRedundantPublicModifier(classCtorAst);
290        }
291    }
292
293    /**
294     * Checks if given ast has redundant public modifier.
295     * @param ast ast
296     */
297    private void checkForRedundantPublicModifier(DetailAST ast) {
298        final DetailAST astModifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
299        DetailAST astModifier = astModifiers.getFirstChild();
300        while (astModifier != null) {
301            if (astModifier.getType() == TokenTypes.LITERAL_PUBLIC) {
302                log(astModifier.getLineNo(), astModifier.getColumnNo(),
303                        MSG_KEY, astModifier.getText());
304            }
305
306            astModifier = astModifier.getNextSibling();
307        }
308    }
309
310    /**
311     * Checks if given class ast has protected modifier.
312     * @param classDef class ast
313     * @return true if class is protected, false otherwise
314     */
315    private static boolean isClassProtected(DetailAST classDef) {
316        final DetailAST classModifiers =
317                classDef.findFirstToken(TokenTypes.MODIFIERS);
318        return classModifiers.branchContains(TokenTypes.LITERAL_PROTECTED);
319    }
320
321    /**
322     * Checks if given class is accessible from "public" scope.
323     * @param ast class def to check
324     * @return true if class is accessible from public scope,false otherwise
325     */
326    private static boolean isClassPublic(DetailAST ast) {
327        boolean isAccessibleFromPublic = false;
328        final boolean isMostOuterScope = ast.getParent() == null;
329        final DetailAST modifiersAst = ast.findFirstToken(TokenTypes.MODIFIERS);
330        final boolean hasPublicModifier = modifiersAst.branchContains(TokenTypes.LITERAL_PUBLIC);
331
332        if (isMostOuterScope) {
333            isAccessibleFromPublic = hasPublicModifier;
334        }
335        else {
336            final DetailAST parentClassAst = ast.getParent().getParent();
337
338            if (parentClassAst.getType() == TokenTypes.INTERFACE_DEF || hasPublicModifier) {
339                isAccessibleFromPublic = isClassPublic(parentClassAst);
340            }
341        }
342
343        return isAccessibleFromPublic;
344    }
345
346    /**
347     * Checks if current AST node is member of Enum.
348     * @param ast AST node
349     * @return true if it is an enum member
350     */
351    private static boolean isEnumMember(DetailAST ast) {
352        final DetailAST parentTypeDef = ast.getParent().getParent();
353        return parentTypeDef.getType() == TokenTypes.ENUM_DEF;
354    }
355
356    /**
357     * Checks if current AST node is member of Interface or Annotation, not of their subnodes.
358     * @param ast AST node
359     * @return true or false
360     */
361    private static boolean isInterfaceOrAnnotationMember(DetailAST ast) {
362        DetailAST parentTypeDef = ast.getParent();
363
364        if (parentTypeDef != null) {
365            parentTypeDef = parentTypeDef.getParent();
366        }
367        return parentTypeDef != null
368                && (parentTypeDef.getType() == TokenTypes.INTERFACE_DEF
369                    || parentTypeDef.getType() == TokenTypes.ANNOTATION_DEF);
370    }
371
372    /**
373     * Checks if method definition is annotated with
374     * <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/SafeVarargs.html">
375     * SafeVarargs</a> annotation
376     * @param methodDef method definition node
377     * @return true or false
378     */
379    private static boolean isAnnotatedWithSafeVarargs(DetailAST methodDef) {
380        boolean result = false;
381        final List<DetailAST> methodAnnotationsList = getMethodAnnotationsList(methodDef);
382        for (DetailAST annotationNode : methodAnnotationsList) {
383            if ("SafeVarargs".equals(annotationNode.getLastChild().getText())) {
384                result = true;
385                break;
386            }
387        }
388        return result;
389    }
390
391    /**
392     * Gets the list of annotations on method definition.
393     * @param methodDef method definition node
394     * @return List of annotations
395     */
396    private static List<DetailAST> getMethodAnnotationsList(DetailAST methodDef) {
397        final List<DetailAST> annotationsList = new ArrayList<>();
398        final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS);
399        DetailAST modifier = modifiers.getFirstChild();
400        while (modifier != null) {
401            if (modifier.getType() == TokenTypes.ANNOTATION) {
402                annotationsList.add(modifier);
403            }
404            modifier = modifier.getNextSibling();
405        }
406        return annotationsList;
407    }
408}