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   * <p>
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   * </p>
36   * <p>
37   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
38   * </p>
39   * <p>
40   * Violation Message Keys:
41   * </p>
42   * <ul>
43   * <li>
44   * {@code type.file.mismatch}
45   * </li>
46   * </ul>
47   *
48   * @since 5.3
49   */
50  @FileStatefulCheck
51  public class OuterTypeFilenameCheck extends AbstractCheck {
52  
53      /**
54       * A key is pointing to the warning message text in "messages.properties"
55       * file.
56       */
57      public static final String MSG_KEY = "type.file.mismatch";
58  
59      /** Pattern matching any file extension with dot included. */
60      private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$");
61  
62      /** Indicates whether the first token has been seen in the file. */
63      private boolean seenFirstToken;
64  
65      /** Current file name. */
66      private String fileName;
67  
68      /** If file has public type. */
69      private boolean hasPublic;
70  
71      /** Outer type with mismatched file name. */
72      private DetailAST wrongType;
73  
74      @Override
75      public int[] getDefaultTokens() {
76          return getRequiredTokens();
77      }
78  
79      @Override
80      public int[] getAcceptableTokens() {
81          return getRequiredTokens();
82      }
83  
84      @Override
85      public int[] getRequiredTokens() {
86          return new int[] {
87              TokenTypes.CLASS_DEF,
88              TokenTypes.INTERFACE_DEF,
89              TokenTypes.ENUM_DEF,
90              TokenTypes.ANNOTATION_DEF,
91              TokenTypes.RECORD_DEF,
92          };
93      }
94  
95      @Override
96      public void beginTree(DetailAST rootAST) {
97          fileName = getSourceFileName();
98          seenFirstToken = false;
99          hasPublic = false;
100         wrongType = null;
101     }
102 
103     @Override
104     public void visitToken(DetailAST ast) {
105         if (seenFirstToken) {
106             final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
107             if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
108                     && TokenUtil.isRootNode(ast.getParent())) {
109                 hasPublic = true;
110             }
111         }
112         else {
113             final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
114 
115             if (!fileName.equals(outerTypeName)) {
116                 wrongType = ast;
117             }
118         }
119         seenFirstToken = true;
120     }
121 
122     @Override
123     public void finishTree(DetailAST rootAST) {
124         if (!hasPublic && wrongType != null) {
125             log(wrongType, MSG_KEY);
126         }
127     }
128 
129     /**
130      * Get source file name.
131      *
132      * @return source file name.
133      */
134     private String getSourceFileName() {
135         String name = getFilePath();
136         name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
137         return FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
138     }
139 
140 }