-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrules.xml
More file actions
535 lines (509 loc) · 17.6 KB
/
rules.xml
File metadata and controls
535 lines (509 loc) · 17.6 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?xml version="1.0"?>
<ruleset name="Custom MyCoRe rules" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
This ruleset contains custom MyCoRe rules
</description>
<rule name="MCR.Configuration.PublicFieldsAndMethods"
language="java"
message="A field or method annotated with a configuration annotation (like @MCRProperty) needs to be public"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/(FieldDeclaration|MethodDeclaration)
[
ModifierList/Annotation/ClassType[pmd-java:typeIs('org.mycore.common.config.annotation.MCRFactory')]
or
ModifierList/Annotation/ClassType[pmd-java:typeIs('org.mycore.common.config.annotation.MCRInstance')]
or
ModifierList/Annotation/ClassType[pmd-java:typeIs('org.mycore.common.config.annotation.MCRInstanceList')]
or
ModifierList/Annotation/ClassType[pmd-java:typeIs('org.mycore.common.config.annotation.MCRInstanceMap')]
or
ModifierList/Annotation/ClassType[pmd-java:typeIs('org.mycore.common.config.annotation.MCRPostConstruction')]
or
ModifierList/Annotation/ClassType[pmd-java:typeIs('org.mycore.common.config.annotation.MCRProperty')]
or
ModifierList/Annotation/ClassType[pmd-java:typeIs('org.mycore.common.config.annotation.MCRRawProperties')]
]
[
not(@Visibility='public')
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Logger.MaxOneField"
language="java"
message="There should only be one field with name 'logger' or 'LOGGER' and type 'Logger'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/FieldDeclaration
[
VariableDeclarator/VariableId/@Name=('logger','LOGGER')
or
ClassType[@SimpleName='Logger']
]
[
position()>1
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Logger.MatchingLoggerType"
language="java"
message="A field with name 'logger' or 'LOGGER' should have type 'Logger'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/FieldDeclaration
[
VariableDeclarator/VariableId/@Name=('logger','LOGGER')
and
not(ClassType[@SimpleName='Logger'])
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Logger.MatchingFieldName"
language="java"
message="A field with type 'Logger' should have name 'logger' or 'LOGGER'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/FieldDeclaration
[
ClassType[@SimpleName='Logger']
and
not(VariableDeclarator/VariableId/@Name=('logger','LOGGER'))
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Logger.CorrectLoggerType"
language="java"
message="A field with type 'Logger' should have name actual type 'org.apache.logging.log4j.Logger'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/FieldDeclaration
[
ClassType[@SimpleName='Logger']
and
not(ClassType[pmd-java:typeIs('org.apache.logging.log4j.Logger')])
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Logger.CorrectStaticInitialisation"
language="java"
message="A field with type 'Logger' and name 'LOGGER' should be initialized as 'private static final Logger LOGGER = LogManager.getLogger();'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/FieldDeclaration
[
ClassType[@SimpleName='Logger']
and
VariableDeclarator/VariableId/@Name='LOGGER'
]
[
not(
deep-equal(pmd-java:modifiers(), ('private','static','final'))
and
VariableDeclarator/MethodCall[
TypeExpression/ClassType[@SimpleName='LogManager']
and
@MethodName='getLogger'
and
count(*)=2
and
count(ArgumentList/*)=0
]
)
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Logger.CorrectDynamicInitialisation"
language="java"
message="A field with type 'Logger' and name 'logger' should be initialized as 'protected final Logger logger = LogManager.getLogger(getClass());'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/FieldDeclaration
[
ClassType[@SimpleName='Logger']
and
VariableDeclarator/VariableId/@Name='logger'
]
[
not(
deep-equal(pmd-java:modifiers(), ('protected','final'))
and
VariableDeclarator/MethodCall[
TypeExpression/ClassType[@SimpleName='LogManager']
and
@MethodName='getLogger'
and
count(*)=2
and
count(ArgumentList/*)=1
and
ArgumentList[1][
MethodCall[
@MethodName='getClass'
and
count(*)=1
and
count(ArgumentList/*)=0
]
]
]
)
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Logger.IndirectAccess"
language="java"
message="A field with name 'Logger' should be accessed directly (either logger.info(...) or this.logger.info(...))"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//FieldAccess[@Name='logger'][VariableAccess|ConstructorCall]
</value>
</property>
</properties>
</rule>
<!--rule name="MCR.Logger.AbstractClassesShouldHaveDynamic"
language="java"
message="An abstract class should have a Logger initialized as 'protected final Logger logger = LogManager.getLogger(getClass());' "
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>5</priority>
<properties>
<property name="xpath">
<value>
//ClassBody/FieldDeclaration
[
../..[pmd-java:modifiers()='abstract']
and
ClassType[@SimpleName='Logger']
and
VariableDeclarator/VariableId/@Name!='logger'
]
</value>
</property>
</properties>
</rule-->
<rule name="MCR.Singleton.MethodModifiers"
language="java"
message="A method named 'getInstance' should be static"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//MethodDeclaration
[
@Name='getInstance'
and
not(ModifierList/Annotation/ClassType[pmd-java:typeIs('java.lang.Deprecated')])
]
[
not(pmd-java:modifiers()='static')
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Singleton.MethodReturnType"
language="java"
message="A method named 'getInstance' should return an instance of the enclosing class"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//MethodDeclaration
[
@Name='getInstance'
and
not(ModifierList/Annotation/ClassType[pmd-java:typeIs('java.lang.Deprecated')])
]
[
not(ClassType[pmd-java:typeIs(../../../@CanonicalName)])
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Singleton.ClassModifiers"
language="java"
message="A singleton class (with method named 'getInstance') should be final"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//MethodDeclaration
[
@Name='getInstance'
and
not(ModifierList/Annotation/ClassType[pmd-java:typeIs('java.lang.Deprecated')])
]
[
not(../..[pmd-java:modifiers()='final'])
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Singleton.PrivateConstructor"
language="java"
message="A singleton class with (method named 'getInstance') should have a private constructor"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//MethodDeclaration
[
@Name='getInstance'
and
not(ModifierList/Annotation/ClassType[pmd-java:typeIs('java.lang.Deprecated')])
]
[
not(../ConstructorDeclaration[pmd-java:modifiers()='private'])
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.Singleton.NonPrivateConstructors"
language="java"
message="A singleton class with (method named 'getInstance') should only have a private constructors"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//MethodDeclaration
[
@Name='getInstance'
and
not(ModifierList/Annotation/ClassType[pmd-java:typeIs('java.lang.Deprecated')])
]
[
../ConstructorDeclaration[not(deep-equal(pmd-java:modifiers(),('private')))]
]
</value>
</property>
</properties>
</rule>
<rule name="MCR.SerialVersionUid.ModifiersAndType"
language="java"
message="The serialVersionUID should have modifiers private static final and type long"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassBody/FieldDeclaration
[
VariableDeclarator/VariableId[@Name='serialVersionUID']
]
[
not(
deep-equal(pmd-java:modifiers(), ('private','static','final'))
and
PrimitiveType[@Kind='long']
)
]
]]>
</value>
</property>
</properties>
</rule>
<rule name="MCR.SerialVersionUid.Annotation"
language="java"
message="The serialVersionUID should be annotated with @java.io.Serial"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<description>
Field should look like this:
@Serial
private static final long serialVersionUID = 1L;
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassBody/FieldDeclaration
[
VariableDeclarator/VariableId[@Name='serialVersionUID']
]
[
not(ModifierList/Annotation/ClassType[pmd-java:typeIs('java.io.Serial')])
]
]]>
</value>
</property>
</properties>
</rule>
<rule name="MCR.SerialVersionUid.Value"
language="java"
message="The serialVersionUID should have literal value '1L'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<description>
Field should look like this:
@Serial
private static final long serialVersionUID = 1L;
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassBody/FieldDeclaration
[
VariableDeclarator/VariableId[@Name='serialVersionUID']
]
[
not(VariableDeclarator/NumericLiteral[@Image='1L'])
]
]]>
</value>
</property>
</properties>
</rule>
<rule name="MCR.ResourceResolver"
language="java"
message="Resources should be accessed using MCRResourceResolver or MCRResourceHelper"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<description>
Accessing resources directly via Class, ClassLoader or ServletContext should only be done
- in classes that are part of the setup of the MyCoRe application (e.g. MCRConfigurationInputStream, MCRComponent),
- in classes that are used by MCRResourceResolver (e.g. MCRResourceProvider, MCRResourceLocator) or
- in rare cases where module specific files need to be accessed (e.g. MCRCoreVersion accessing git.properties).
Otherwise, MCRResourceResolver or MCRResourceHelper should be used.
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//(MethodCall|MethodReference)
[*[position() = 1 and pmd-java:typeIs('java.lang.Class')]]
[@MethodName=(
'getResource',
'getResourceAsStream'
)]
|
//(MethodCall|MethodReference)
[*[position() = 1 and pmd-java:typeIs('java.lang.ClassLoader')]]
[@MethodName=(
'resources',
'getResource',
'getResources',
'getResourceAsStream'
)]
|
//(MethodCall|MethodReference)
[*[position() = 1 and pmd-java:typeIs('jakarta.servlet.ServletContext')]]
[@MethodName=(
'getResource',
'getResourcePaths',
'getResourceAsStream'
)]
]]>
</value>
</property>
</properties>
</rule>
<rule name="MCR.ClassTools.Clone"
language="java"
message="MCRClassTools#clone should be called as MCRClassTools.clone(getClass(), super::clone) exactly"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<description>
MCRClassTools#clone is a helper function for top most classes in class hierarchies that implement Cloneable
and expect every subclass to implement a proper clone method, i.e. to never throw a CloneNotSupportedException.
It is meant to simplify and harmonize suppression of this exception in the method signatures of the clone-Method
of such classes and needs to be called as MCRClassTools.clone(getClass(), super::clone) exactly
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//(MethodCall|MethodReference)
[
*[position() = 1 and pmd-java:typeIs('org.mycore.common.MCRClassTools')]
and
@MethodName='clone'
]
[
not(ArgumentList/*[1][name()='MethodCall' and @MethodName='getClass' and ArgumentList and count(*)=1])
or
not(ArgumentList/*[2][name()='MethodReference' and @MethodName='clone' and SuperExpression and count(*)=1])
]
]]>
</value>
</property>
</properties>
</rule>
<!-- rule name="MCR.XSLT.ExcludeAllResultPrefixes"
language="xml"
message="XSLT 2.0 and 3.0 stylesheets should set exclude-result-prefixes='#all' to prevent unused namespaces output."
class="net.sourceforge.pmd.lang.xml.rule.DomXPathRule">
<description>
When using XSLT 2.0 or 3.0, it is a best practice to use exclude-result-prefixes="#all"
to ensure that only explicitly required namespaces are included in the result document.
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
/*
[
namespace-uri()='http://www.w3.org/1999/XSL/Transform'
and
local-name()='stylesheet'
and
(@version='2.0' or @version='3.0')
]
[
not(@exclude-result-prefixes='#all')
]
]]>
</value>
</property>
</properties>
</rule -->
</ruleset>