/
PackageDeclarationCheck.java
188 lines (168 loc) · 5.78 KB
/
PackageDeclarationCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.coding;
import java.io.File;
import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* <p>
* Ensures that a class has a package declaration, and (optionally) whether
* the package name matches the directory name for the source file.
* </p>
* <p>
* Rationale: Classes that live in the null package cannot be imported.
* Many novice developers are not aware of this.
* </p>
* <p>
* Packages provide logical namespace to classes and should be stored in
* the form of directory levels to provide physical grouping to your classes.
* These directories are added to the classpath so that your classes
* are visible to JVM when it runs the code.
* </p>
* <ul>
* <li>
* Property {@code matchDirectoryStructure} - Control whether to check for
* directory and package name match.
* Type is {@code boolean}.
* Default value is {@code true}.
* </li>
* </ul>
* <p>
* To configure the check:
* </p>
* <pre>
* <module name="PackageDeclaration"/>
* </pre>
* <p>
* Let us consider the class AnnotationLocationCheck which is in the directory
* /com/puppycrawl/tools/checkstyle/checks/annotations/
* </p>
* <pre>
* package com.puppycrawl.tools.checkstyle.checks; //Violation
* public class AnnotationLocationCheck extends AbstractCheck {
* //...
* }
* </pre>
* <p>
* Example of how the check works when matchDirectoryStructure option is set to false.
* Let us again consider the AnnotationLocationCheck class located at directory
* /com/puppycrawl/tools/checkstyle/checks/annotations/ along with the following setup,
* </p>
* <pre>
* <module name="PackageDeclaration">
* <property name="matchDirectoryStructure" value="false"/>
* </module>
* </pre>
* <pre>
* package com.puppycrawl.tools.checkstyle.checks; //No Violation
*
* public class AnnotationLocationCheck extends AbstractCheck {
* //...
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
* Violation Message Keys:
* </p>
* <ul>
* <li>
* {@code mismatch.package.directory}
* </li>
* <li>
* {@code missing.package.declaration}
* </li>
* </ul>
*
* @since 3.2
*/
@FileStatefulCheck
public final class PackageDeclarationCheck extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_MISSING = "missing.package.declaration";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_MISMATCH = "mismatch.package.directory";
/** Is package defined. */
private boolean defined;
/** Control whether to check for directory and package name match. */
private boolean matchDirectoryStructure = true;
/**
* Setter to control whether to check for directory and package name match.
*
* @param matchDirectoryStructure the new value.
*/
public void setMatchDirectoryStructure(boolean matchDirectoryStructure) {
this.matchDirectoryStructure = matchDirectoryStructure;
}
@Override
public int[] getDefaultTokens() {
return getRequiredTokens();
}
@Override
public int[] getRequiredTokens() {
return new int[] {TokenTypes.PACKAGE_DEF};
}
@Override
public int[] getAcceptableTokens() {
return getRequiredTokens();
}
@Override
public void beginTree(DetailAST ast) {
defined = false;
}
@Override
public void finishTree(DetailAST ast) {
if (!defined && ast != null) {
log(ast, MSG_KEY_MISSING);
}
}
@Override
public void visitToken(DetailAST ast) {
defined = true;
if (matchDirectoryStructure) {
final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling();
final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst);
final String packageName = fullIdent.getText().replace('.', File.separatorChar);
final String directoryName = getDirectoryName();
if (!directoryName.endsWith(packageName)) {
log(ast, MSG_KEY_MISMATCH, packageName);
}
}
}
/**
* Returns the directory name this file is in.
*
* @return Directory name.
*/
private String getDirectoryName() {
final String fileName = getFilePath();
final int lastSeparatorPos = fileName.lastIndexOf(File.separatorChar);
return fileName.substring(0, lastSeparatorPos);
}
}