aboutsummaryrefslogtreecommitdiff
path: root/src/xdocs/config_modifier.xml
blob: 7911772bebe846f7458c02589ca9e5e2345203ca (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?xml version="1.0" encoding="UTF-8"?>

<document xmlns="http://maven.apache.org/XDOC/2.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">

  <head>
    <title>Modifiers</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"/>
    <script type="text/javascript" src="js/anchors.js"/>
    <script type="text/javascript" src="js/google-analytics.js"/>
    <link rel="icon" href="images/favicon.png" type="image/x-icon" />
    <link rel="shortcut icon" href="images/favicon.ico" type="image/ico" />
  </head>

  <body>
    <section name="Content">
      <macro name="toc">
        <param name="fromDepth" value="1"/>
        <param name="toDepth" value="1"/>
      </macro>
    </section>

    <section name="ModifierOrder">
      <subsection name="Description">
        <p>
          Checks that the order of modifiers conforms to the suggestions in
          the <a
          href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html">Java
          Language specification, sections 8.1.1, 8.3.1 and 8.4.3</a>. The
          correct order is:
        </p>

        <ol>
          <li>
            <code>public</code>
          </li>
          <li>
            <code>protected</code>
          </li>
          <li>
            <code>private</code>
          </li>
          <li>
            <code>abstract</code>
          </li>
          <li>
            <code>static</code>
          </li>
          <li>
            <code>final</code>
          </li>
          <li>
            <code>transient</code>
          </li>
          <li>
            <code>volatile</code>
          </li>
          <li>
            <code>synchronized</code>
          </li>
          <li>
            <code>native</code>
          </li>
          <li>
            <code>strictfp</code>
          </li>
        </ol>

        <p>
          ATTENTION: We skip <a href="https://blogs.oracle.com/java-platform-group/entry/java_8_s_new_type">
          type annotations</a> from validation.
        </p>

      </subsection>

      <subsection name="Examples">
        <p> To configure the check: </p>
        <source>
&lt;module name=&quot;ModifierOrder&quot;/&gt;
        </source>
      </subsection>

      <subsection name="Example of Usage">
        <ul>
          <li>
            <a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+ModifierOrder">
            Google Style</a>
          </li>
          <li>
            <a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+ModifierOrder">
            Sun Style</a>
          </li>
          <li>
            <a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+ModifierOrder">
            Checkstyle Style</a>
          </li>
        </ul>
      </subsection>

      <subsection name="Error Messages">
        <ul>
          <li>
            <a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fmodifier+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22annotation.order%22">
            annotation.order</a>
          </li>
          <li>
            <a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fmodifier+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22mod.order%22">
            mod.order</a>
          </li>
        </ul>
        <p>
          All messages can be customized if the default message doesn't suite you.
          Please <a href="config.html#Custom_messages">see the documentation</a> to learn how to.
        </p>
      </subsection>

      <subsection name="Package">
        <p> com.puppycrawl.tools.checkstyle.checks.modifier </p>
      </subsection>

      <subsection name="Parent Module">
        <p>
          <a href="config.html#TreeWalker">TreeWalker</a>
        </p>
      </subsection>
    </section>

    <section name="RedundantModifier">

      <subsection name="Description">
        <p>
          Checks for redundant modifiers in:
        </p>
        <ol>
          <li>Interface and annotation definitions.</li>
          <li>Final modifier on methods of final and anonymous classes.</li>
          <li>
            Inner <code>interface</code> declarations that are declared
            as <code>static</code>.
          </li>
          <li>Class constructors.</li>
          <li>
            Nested <code>enum</code> definitions that are declared
            as <code>static</code>.
          </li>
        </ol>
        <p>
          Rationale: The Java Language Specification strongly
          discourages the usage of <code>public</code> and <code>abstract</code> for method
          declarations in interface definitions as a matter of style.
        </p>

        <p>
          Interfaces by definition are abstract so the <code>abstract</code>
          modifier on the interface is redundant.
        </p>

        <p>
          Classes inside of interfaces by definition are public and static,
          so the <code>public</code> and <code>static</code> modifiers
          on the inner classes are redundant. On the other hand, classes
          inside of interfaces can be abstract or non abstract.
          So, <code>abstract</code> modifier is allowed.
        </p>

        <p>
          Fields in interfaces and annotations are automatically
          public, static and final, so these modifiers are redundant as
          well.
        </p>

        <p>
          As annotations are a form of interface, their fields are also
          automatically public, static and final just as their
          annotation fields are automatically public and abstract.
        </p>

        <p>
          Enums by definition are static implicit subclasses of java.lang.Enum&#60;E&#62;.
          So, the <code>static</code> modifier on the enums is redundant. In addition,
          if enum is inside of interface, <code>public</code> modifier is also redundant.
        </p>

        <p>
          Nested <code>enum</code> types are always static by default.
        </p>

        <p>
          Final classes by definition cannot be extended so the <code>final</code>
          modifier on the method of a final class is redundant.
        </p>

        <p>
          Public modifier for constructors in non-public non-protected classes
          is always obsolete:
        </p>

        <source>
          public class PublicClass {
            public PublicClass() {} // OK
          }

          class PackagePrivateClass {
            public PackagePrivateClass() {} // violation expected
          }
        </source>

        <p>There is no violation in the following example,
          because removing public modifier from ProtectedInnerClass
          constructor will make this code not compiling:
        </p>

        <source>
          package a;
          public class ClassExample {
            protected class ProtectedInnerClass {
              public ProtectedInnerClass () {}
            }
          }

          package b;
          import a.ClassExample;
          public class ClassExtending extends ClassExample {
            ProtectedInnerClass pc = new ProtectedInnerClass();
          }
        </source>
      </subsection>

      <subsection name="Properties">
        <table>
          <tr>
            <th>name</th>
            <th>description</th>
            <th>type</th>
            <th>default value</th>
          </tr>
          <tr>
            <td>tokens</td>
            <td>tokens to check</td>
            <td>
              subset of tokens
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">METHOD_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF">ANNOTATION_FIELD_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">INTERFACE_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">CTOR_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">CLASS_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_DEF</a>.
            </td>
            <td>
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">METHOD_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF">ANNOTATION_FIELD_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">INTERFACE_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">CTOR_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">CLASS_DEF</a>,
              <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_DEF</a>.
            </td>
          </tr>
        </table>
      </subsection>

      <subsection name="Examples">
        <p> To configure the check: </p>
        <source>
&lt;module name=&quot;RedundantModifier&quot;/&gt;
        </source>

        <p>
          To configure the check to check only methods and not variables:
        </p>
        <source>
&lt;module name=&quot;RedundantModifier&quot;&gt;
  &lt;property name=&quot;tokens&quot; value=&quot;METHOD_DEF&quot;/&gt;
&lt;/module&gt;
        </source>
      </subsection>

      <subsection name="Example of Usage">
        <ul>
          <li>
            <a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+RedundantModifier">
            Sun Style</a>
          </li>
          <li>
            <a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+RedundantModifier">
            Checkstyle Style</a>
          </li>
        </ul>
      </subsection>

      <subsection name="Error Messages">
        <ul>
          <li>
            <a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fmodifier+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22redundantModifier%22">
            redundantModifier</a>
          </li>
        </ul>
        <p>
          All messages can be customized if the default message doesn't suite you.
          Please <a href="config.html#Custom_messages">see the documentation</a> to learn how to.
        </p>
      </subsection>

      <subsection name="Package">
        <p> com.puppycrawl.tools.checkstyle.checks.modifier </p>
      </subsection>

      <subsection name="Parent Module">
        <p>
          <a href="config.html#TreeWalker">TreeWalker</a>
        </p>
      </subsection>
    </section>
  </body>
</document>