forked from checkstyle/checkstyle
/
OneTopLevelClassCheck.java
154 lines (141 loc) · 4.5 KB
/
OneTopLevelClassCheck.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
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014 Oliver Burn
//
// 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.design;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
*
* Checks that each top-level class, interface
* or enum resides in a source file of its own.
* <p>
* Official description of a 'top-level' term:<a
* href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.6">
* 7.6. Top Level Type Declarations</a>. If file doesn't contains
* public class, enum or interface, top-level type is the first type in file.
* </p>
*
* An example of code with violations:
* </p>
* <pre><code>
* public class Foo{
* //methods
* }
*
* class Foo2{
* //methods
* }
* </code></pre>
* <p>
* An example of code without top-level public type:
* </p>
* <pre><code>
* class Foo{ //top-level class
* //methods
* }
*
* class Foo2{
* //methods
* }
* </code></pre>
* <p>
* An example of check's configuration:
* </p>
* <pre>
* <module name="OneTopLevelClass"/>
* </pre>
*
* <p>
* An example of code without violations:
* </p>
* <pre><code>
* public class Foo{
* //methods
* }
* </code></pre>
*
* @author maxvetrenko
*/
public class OneTopLevelClassCheck extends Check
{
/**
* true if a java source file contains a type
* with a public access level modifier.
*/
private boolean mPublicTypeFound;
/** Mapping between type names and line numbers of the type declarations.*/
private Map<Integer, String> mLineNumberTypeMap =
new TreeMap<Integer, String>();
@Override
public int[] getDefaultTokens()
{
return new int[] {};
}
@Override
public void beginTree(DetailAST aRootAST)
{
DetailAST currentNode = aRootAST;
while (currentNode != null) {
if (currentNode.getType() == TokenTypes.CLASS_DEF
|| currentNode.getType() == TokenTypes.ENUM_DEF
|| currentNode.getType() == TokenTypes.INTERFACE_DEF)
{
if (isPublic(currentNode)) {
mPublicTypeFound = true;
}
else {
final String typeName = currentNode.
findFirstToken(TokenTypes.IDENT).getText();
mLineNumberTypeMap.put(currentNode.getLineNo(), typeName);
}
}
currentNode = currentNode.getNextSibling();
}
}
@Override
public void finishTree(DetailAST aRootAST)
{
final Iterator<Map.Entry<Integer, String>> iterator =
mLineNumberTypeMap.entrySet().iterator();
if (!mPublicTypeFound && iterator.hasNext()) {
// skip first top-level type.
iterator.next();
}
while (iterator.hasNext()) {
final Map.Entry<Integer, String> entry = iterator.next();
log(entry.getKey(), "one.top.level.class", entry.getValue());
}
mLineNumberTypeMap.clear();
mPublicTypeFound = false;
}
/**
* Checks if a type is public.
* @param aTypeDef type definition node.
* @return true if a type has a public access level modifier.
*/
private boolean isPublic(DetailAST aTypeDef)
{
final DetailAST modifiers =
aTypeDef.findFirstToken(TokenTypes.MODIFIERS);
return modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null;
}
}