1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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  
30  /**
31   * Checks that the outer type name and the file name match.
32   */
33  @FileStatefulCheck
34  public class OuterTypeFilenameCheck extends AbstractCheck {
35  
36      /**
37       * A key is pointing to the warning message text in "messages.properties"
38       * file.
39       */
40      public static final String MSG_KEY = "type.file.mismatch";
41  
42      /** Pattern matching any file extension with dot included. */
43      private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("\\.[^.]*$");
44  
45      /** Indicates whether the first token has been seen in the file. */
46      private boolean seenFirstToken;
47  
48      /** Current file name. */
49      private String fileName;
50  
51      /** If file has public type. */
52      private boolean hasPublic;
53  
54      /** Outer type with mismatched file name. */
55      private DetailAST wrongType;
56  
57      @Override
58      public int[] getDefaultTokens() {
59          return getRequiredTokens();
60      }
61  
62      @Override
63      public int[] getAcceptableTokens() {
64          return getRequiredTokens();
65      }
66  
67      @Override
68      public int[] getRequiredTokens() {
69          return new int[] {
70              TokenTypes.CLASS_DEF,
71              TokenTypes.INTERFACE_DEF,
72              TokenTypes.ENUM_DEF,
73              TokenTypes.ANNOTATION_DEF,
74          };
75      }
76  
77      @Override
78      public void beginTree(DetailAST rootAST) {
79          fileName = getFileName();
80          seenFirstToken = false;
81          hasPublic = false;
82          wrongType = null;
83      }
84  
85      @Override
86      public void visitToken(DetailAST ast) {
87          if (seenFirstToken) {
88              final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
89              if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
90                      && ast.getParent() == null) {
91                  hasPublic = true;
92              }
93          }
94          else {
95              final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();
96  
97              if (!fileName.equals(outerTypeName)) {
98                  wrongType = ast;
99              }
100         }
101         seenFirstToken = true;
102     }
103 
104     @Override
105     public void finishTree(DetailAST rootAST) {
106         if (!hasPublic && wrongType != null) {
107             log(wrongType.getLineNo(), MSG_KEY);
108         }
109     }
110 
111     /**
112      * Get source file name.
113      * @return source file name.
114      */
115     private String getFileName() {
116         String name = getFileContents().getFileName();
117         name = name.substring(name.lastIndexOf(File.separatorChar) + 1);
118         name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll("");
119         return name;
120     }
121 
122 }