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