001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
027
028/**
029 * <div>
030 * Checks for implicit modifiers on interface members and nested types.
031 * </div>
032 *
033 * <p>
034 * This check is effectively the opposite of
035 * <a href="https://checkstyle.org/checks/modifier/redundantmodifier.html">
036 * RedundantModifier</a>.
037 * It checks the modifiers on interface members, ensuring that certain modifiers are explicitly
038 * specified even though they are actually redundant.
039 * </p>
040 *
041 * <p>
042 * Methods in interfaces are {@code public} by default, however from Java 9 they can also be
043 * {@code private}. This check provides the ability to enforce that {@code public} is explicitly
044 * coded and not implicitly added by the compiler.
045 * </p>
046 *
047 * <p>
048 * From Java 8, there are three types of methods in interfaces - static methods marked with
049 * {@code static}, default methods marked with {@code default} and abstract methods which do not
050 * have to be marked with anything. From Java 9, there are also private methods marked with
051 * {@code private}. This check provides the ability to enforce that {@code abstract} is
052 * explicitly coded and not implicitly added by the compiler.
053 * </p>
054 *
055 * <p>
056 * Fields in interfaces are always {@code public static final} and as such the compiler does not
057 * require these modifiers. This check provides the ability to enforce that these modifiers are
058 * explicitly coded and not implicitly added by the compiler.
059 * </p>
060 *
061 * <p>
062 * Nested types within an interface are always {@code public static} and as such the compiler
063 * does not require the {@code public static} modifiers. This check provides the ability to
064 * enforce that the {@code public} and {@code static} modifiers are explicitly coded and not
065 * implicitly added by the compiler.
066 * </p>
067 * {@snippet lang="text" :
068 * public interface AddressFactory {
069 *   // check enforces code contains "public static final"
070 *   public static final String UNKNOWN = "Unknown";
071 *
072 *   String OTHER = "Other";  // violation
073 *
074 *   // check enforces code contains "public" or "private"
075 *   public static AddressFactory instance();
076 *
077 *   // check enforces code contains "public abstract"
078 *   public abstract Address createAddress(String addressLine, String city);
079 *
080 *   List&lt;Address&gt; findAddresses(String city);  // violation
081 *
082 *   // check enforces default methods are explicitly declared "public"
083 *   public default Address createAddress(String city) {
084 *     return createAddress(UNKNOWN, city);
085 *   }
086 *
087 *   default Address createOtherAddress() {  // violation
088 *     return createAddress(OTHER, OTHER);
089 *   }
090 * }
091 * }
092 *
093 * <p>
094 * Rationale for this check: Methods, fields and nested types are treated differently
095 * depending on whether they are part of an interface or part of a class. For example, by
096 * default methods are package-scoped on classes, but public in interfaces. However, from
097 * Java 8 onwards, interfaces have changed to be much more like abstract classes.
098 * Interfaces now have static and instance methods with code. Developers should not have to
099 * remember which modifiers are required and which are implied. This check allows the simpler
100 * alternative approach to be adopted where the implied modifiers must always be coded explicitly.
101 * </p>
102 *
103 * @since 8.12
104 */
105@StatelessCheck
106public class InterfaceMemberImpliedModifierCheck extends AbstractCheck {
107
108    /**
109     * A key is pointing to the warning message text in "messages.properties" file.
110     */
111    public static final String MSG_KEY = "interface.implied.modifier";
112
113    /** Name for 'public' access modifier. */
114    private static final String PUBLIC_ACCESS_MODIFIER = "public";
115
116    /** Name for 'abstract' keyword. */
117    private static final String ABSTRACT_KEYWORD = "abstract";
118
119    /** Name for 'static' keyword. */
120    private static final String STATIC_KEYWORD = "static";
121
122    /** Name for 'final' keyword. */
123    private static final String FINAL_KEYWORD = "final";
124
125    /**
126     * Control whether to enforce that {@code public} is explicitly coded on interface fields.
127     */
128    private boolean violateImpliedPublicField = true;
129
130    /**
131     * Control whether to enforce that {@code static} is explicitly coded on interface fields.
132     */
133    private boolean violateImpliedStaticField = true;
134
135    /**
136     * Control whether to enforce that {@code final} is explicitly coded on interface fields.
137     */
138    private boolean violateImpliedFinalField = true;
139
140    /**
141     * Control whether to enforce that {@code public} is explicitly coded on interface methods.
142     */
143    private boolean violateImpliedPublicMethod = true;
144
145    /**
146     * Control whether to enforce that {@code abstract} is explicitly coded on interface methods.
147     */
148    private boolean violateImpliedAbstractMethod = true;
149
150    /**
151     * Control whether to enforce that {@code public} is explicitly coded on interface nested types.
152     */
153    private boolean violateImpliedPublicNested = true;
154
155    /**
156     * Control whether to enforce that {@code static} is explicitly coded on interface nested types.
157     */
158    private boolean violateImpliedStaticNested = true;
159
160    /**
161     * Creates a new {@code InterfaceMemberImpliedModifierCheck} instance.
162     */
163    public InterfaceMemberImpliedModifierCheck() {
164        // no code by default
165    }
166
167    /**
168     * Setter to control whether to enforce that {@code public} is explicitly coded
169     * on interface fields.
170     *
171     * @param violateImpliedPublicField
172     *        True to perform the check, false to turn the check off.
173     * @since 8.12
174     */
175    public void setViolateImpliedPublicField(boolean violateImpliedPublicField) {
176        this.violateImpliedPublicField = violateImpliedPublicField;
177    }
178
179    /**
180     * Setter to control whether to enforce that {@code static} is explicitly coded
181     * on interface fields.
182     *
183     * @param violateImpliedStaticField
184     *        True to perform the check, false to turn the check off.
185     * @since 8.12
186     */
187    public void setViolateImpliedStaticField(boolean violateImpliedStaticField) {
188        this.violateImpliedStaticField = violateImpliedStaticField;
189    }
190
191    /**
192     * Setter to control whether to enforce that {@code final} is explicitly coded
193     * on interface fields.
194     *
195     * @param violateImpliedFinalField
196     *        True to perform the check, false to turn the check off.
197     * @since 8.12
198     */
199    public void setViolateImpliedFinalField(boolean violateImpliedFinalField) {
200        this.violateImpliedFinalField = violateImpliedFinalField;
201    }
202
203    /**
204     * Setter to control whether to enforce that {@code public} is explicitly coded
205     * on interface methods.
206     *
207     * @param violateImpliedPublicMethod
208     *        True to perform the check, false to turn the check off.
209     * @since 8.12
210     */
211    public void setViolateImpliedPublicMethod(boolean violateImpliedPublicMethod) {
212        this.violateImpliedPublicMethod = violateImpliedPublicMethod;
213    }
214
215    /**
216     * Setter to control whether to enforce that {@code abstract} is explicitly coded
217     * on interface methods.
218     *
219     * @param violateImpliedAbstractMethod
220     *        True to perform the check, false to turn the check off.
221     * @since 8.12
222     */
223    public void setViolateImpliedAbstractMethod(boolean violateImpliedAbstractMethod) {
224        this.violateImpliedAbstractMethod = violateImpliedAbstractMethod;
225    }
226
227    /**
228     * Setter to control whether to enforce that {@code public} is explicitly coded
229     * on interface nested types.
230     *
231     * @param violateImpliedPublicNested
232     *        True to perform the check, false to turn the check off.
233     * @since 8.12
234     */
235    public void setViolateImpliedPublicNested(boolean violateImpliedPublicNested) {
236        this.violateImpliedPublicNested = violateImpliedPublicNested;
237    }
238
239    /**
240     * Setter to control whether to enforce that {@code static} is explicitly coded
241     * on interface nested types.
242     *
243     * @param violateImpliedStaticNested
244     *        True to perform the check, false to turn the check off.
245     * @since 8.12
246     */
247    public void setViolateImpliedStaticNested(boolean violateImpliedStaticNested) {
248        this.violateImpliedStaticNested = violateImpliedStaticNested;
249    }
250
251    @Override
252    public int[] getDefaultTokens() {
253        return getAcceptableTokens();
254    }
255
256    @Override
257    public int[] getRequiredTokens() {
258        return getAcceptableTokens();
259    }
260
261    @Override
262    public int[] getAcceptableTokens() {
263        return new int[] {
264            TokenTypes.METHOD_DEF,
265            TokenTypes.VARIABLE_DEF,
266            TokenTypes.INTERFACE_DEF,
267            TokenTypes.CLASS_DEF,
268            TokenTypes.ENUM_DEF,
269        };
270    }
271
272    @Override
273    public void visitToken(DetailAST ast) {
274        if (ScopeUtil.isInInterfaceBlock(ast) && !ScopeUtil.isInCodeBlock(ast)) {
275            switch (ast.getType()) {
276                case TokenTypes.METHOD_DEF -> processMethod(ast);
277
278                case TokenTypes.VARIABLE_DEF -> processField(ast);
279
280                case TokenTypes.CLASS_DEF,
281                     TokenTypes.INTERFACE_DEF,
282                     TokenTypes.ENUM_DEF -> processNestedType(ast);
283
284                default -> throw new IllegalStateException(ast.toString());
285            }
286        }
287    }
288
289    /**
290     * Check method in interface.
291     *
292     * @param ast the method AST
293     */
294    private void processMethod(DetailAST ast) {
295        final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
296        if (violateImpliedPublicMethod
297                && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null
298                && modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) == null) {
299            log(ast, MSG_KEY, PUBLIC_ACCESS_MODIFIER);
300        }
301        if (violateImpliedAbstractMethod
302                && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null
303                && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null
304                && modifiers.findFirstToken(TokenTypes.LITERAL_DEFAULT) == null
305                && modifiers.findFirstToken(TokenTypes.ABSTRACT) == null) {
306            log(ast, MSG_KEY, ABSTRACT_KEYWORD);
307        }
308    }
309
310    /**
311     * Check field in interface.
312     *
313     * @param ast the field AST
314     */
315    private void processField(DetailAST ast) {
316        final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
317        if (violateImpliedPublicField
318                && modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) == null) {
319            log(ast, MSG_KEY, PUBLIC_ACCESS_MODIFIER);
320        }
321        if (violateImpliedStaticField
322                && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null) {
323            log(ast, MSG_KEY, STATIC_KEYWORD);
324        }
325        if (violateImpliedFinalField
326                && modifiers.findFirstToken(TokenTypes.FINAL) == null) {
327            log(ast, MSG_KEY, FINAL_KEYWORD);
328        }
329    }
330
331    /**
332     * Check nested types in interface.
333     *
334     * @param ast the nested type AST
335     */
336    private void processNestedType(DetailAST ast) {
337        final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
338        if (violateImpliedPublicNested
339                && modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) == null) {
340            log(ast, MSG_KEY, PUBLIC_ACCESS_MODIFIER);
341        }
342        if (violateImpliedStaticNested
343                && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null) {
344            log(ast, MSG_KEY, STATIC_KEYWORD);
345        }
346    }
347
348}