View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks;
21  
22  import java.io.File;
23  import java.util.regex.Pattern;
24  
25  import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
26  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27  import com.puppycrawl.tools.checkstyle.api.DetailAST;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
30  
31  /**
32   * <div>
33   * Checks that the outer type name and the file name match.
34   * For example, the class {@code Foo} must be in a file named {@code Foo.java}.
35   * </div>
36   *
37   * <p>
38   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
39   * </p>
40   *
41   * <p>
42   * Violation Message Keys:
43   * </p>
44   * <ul>
45   * <li>
46   * {@code type.file.mismatch}
47   * </li>
48   * </ul>
49   *
50   * @since 5.3
51   */
52  @FileStatefulCheck
53  public class OuterTypeFilenameCheck extends AbstractCheck {
54  
55      /**
56       * A key is pointing to the warning message text in "messages.properties"
57       * file.
58       */
59      public static final String MSG_KEY = "type.file.mismatch";
60  
61      /** Pattern matching any file extension with dot included. */
62      private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$");
63  
64      /** Indicates whether the first token has been seen in the file. */
65      private boolean seenFirstToken;
66  
67      /** Current file name. */
68      private String fileName;
69  
70      /** If file has public type. */
71      private boolean hasPublic;
72  
73      /** Outer type with mismatched file name. */
74      private DetailAST wrongType;
75  
76      @Override
77      public int[] getDefaultTokens() {
78          return getRequiredTokens();
79      }
80  
81      @Override
82      public int[] getAcceptableTokens() {
83          return getRequiredTokens();
84      }
85  
86      @Override
87      public int[] getRequiredTokens() {
88          return new int[] {
89              TokenTypes.CLASS_DEF,
90              TokenTypes.INTERFACE_DEF,
91              TokenTypes.ENUM_DEF,
92              TokenTypes.ANNOTATION_DEF,
93              TokenTypes.RECORD_DEF,
94          };
95      }
96  
97      @Override
98      public void beginTree(DetailAST rootAST) {
99          fileName = getSourceFileName();
100         seenFirstToken = false;
101         hasPublic = false;
102         wrongType = null;
103     }
104 
105     @Override
106     public void visitToken(DetailAST ast) {
107         if (seenFirstToken) {
108             final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
109             if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
110                     && TokenUtil.isRootNode(ast.getParent())) {
111                 hasPublic = true;
112             }
113         }
114         else {
115             final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
116 
117             if (!fileName.equals(outerTypeName)) {
118                 wrongType = ast;
119             }
120         }
121         seenFirstToken = true;
122     }
123 
124     @Override
125     public void finishTree(DetailAST rootAST) {
126         if (!hasPublic && wrongType != null) {
127             log(wrongType, MSG_KEY);
128         }
129     }
130 
131     /**
132      * Get source file name.
133      *
134      * @return source file name.
135      */
136     private String getSourceFileName() {
137         String name = getFilePath();
138         name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
139         return FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
140     }
141 
142 }