Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OuterTypeFilenameCheck |
|
| 2.0;2 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 Oliver Burn | |
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 | package com.puppycrawl.tools.checkstyle.checks; | |
20 | ||
21 | import com.puppycrawl.tools.checkstyle.api.Check; | |
22 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
24 | import java.io.File; | |
25 | ||
26 | /** | |
27 | * Checks that the outer type name and the file name match. | |
28 | * @author Oliver Burn | |
29 | */ | |
30 | 2 | public class OuterTypeFilenameCheck extends Check |
31 | { | |
32 | /** indicates whether the first token has been seen in the file. */ | |
33 | private boolean mSeenFirstToken; | |
34 | ||
35 | @Override | |
36 | public int[] getDefaultTokens() | |
37 | { | |
38 | 2 | return new int[] { |
39 | TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, | |
40 | TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, | |
41 | }; | |
42 | } | |
43 | ||
44 | @Override | |
45 | public void beginTree(DetailAST aAST) | |
46 | { | |
47 | 2 | mSeenFirstToken = false; |
48 | 2 | } |
49 | ||
50 | @Override | |
51 | public void visitToken(DetailAST aAST) | |
52 | { | |
53 | // Only check first declaration | |
54 | 6 | if (mSeenFirstToken) { |
55 | 4 | return; |
56 | } | |
57 | 2 | mSeenFirstToken = true; |
58 | ||
59 | 2 | final String outerTypeName = |
60 | aAST.findFirstToken(TokenTypes.IDENT).getText(); | |
61 | ||
62 | // Calculate the file name without the leading path or | |
63 | // the trailing .java suffix. Will be lax and just remove whatever | |
64 | // is after the '.' character. | |
65 | 2 | String fname = getFileContents().getFilename(); |
66 | 2 | fname = fname.substring(fname.lastIndexOf(File.separatorChar) + 1); |
67 | 2 | fname = fname.replaceAll("\\.[^\\.]*$", ""); |
68 | ||
69 | 2 | if (!(fname.equals(outerTypeName))) { |
70 | 1 | log(aAST.getLineNo(), "type.file.mismatch"); |
71 | } | |
72 | 2 | } |
73 | } |