Skip to content

Commit 93fa4f6

Browse files
committed
MIR-1434 ensure usage of MCRResourceResolver
1 parent fd520af commit 93fa4f6

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

mir-module/src/main/java/org/mycore/mir/common/MIRCoreVersion.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @author Thomas Scheffler (yagee)
3737
*
3838
*/
39+
@SuppressWarnings("PMD.MCR.ResourceResolver")
3940
public class MIRCoreVersion {
4041
private static Properties prop = loadVersionProperties();
4142

mir-module/src/main/java/org/mycore/mir/migration/MIRMigration03Utils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
@MCRCommandGroup(
2222
name = "MIR migration 0.3")
23+
@SuppressWarnings("PMD.MCR.ResourceResolver")
2324
public class MIRMigration03Utils {
2425

2526
private static final Logger LOGGER = LogManager.getLogger();

mir-module/src/main/java/org/mycore/mir/migration/MIRMigration04Utils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
@MCRCommandGroup(
1818
name = "MIR migration 0.4")
19+
@SuppressWarnings("PMD.MCR.ResourceResolver")
1920
public class MIRMigration04Utils {
2021

2122
private static final Logger LOGGER = LogManager.getLogger();

mir-module/src/main/java/org/mycore/mir/migration/MIRMigration201603Utils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
@MCRCommandGroup(
2121
name = "MIR migration 2016.03")
22+
@SuppressWarnings("PMD.MCR.ResourceResolver")
2223
public class MIRMigration201603Utils {
2324

2425
private static final Logger LOGGER = LogManager.getLogger();

mir-module/src/main/java/org/mycore/mir/migration/MIRMigration201706Utils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
@MCRCommandGroup(
2525
name = "MIR migration 2017.06")
26+
@SuppressWarnings("PMD.MCR.ResourceResolver")
2627
public class MIRMigration201706Utils {
2728

2829
private static final Logger LOGGER = LogManager.getLogger();

rules.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
message="The serialVersionUID should be annotated with @java.io.Serial"
1313
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
1414
<priority>4</priority>
15+
<description>
16+
Field should look like this:
17+
18+
@Serial
19+
private static final long serialVersionUID = 1L;
20+
</description>
1521
<properties>
1622
<property name="xpath">
1723
<value>
@@ -42,6 +48,12 @@
4248
message="The serialVersionUID should have literal value '1L'"
4349
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
4450
<priority>4</priority>
51+
<description>
52+
Field should look like this:
53+
54+
@Serial
55+
private static final long serialVersionUID = 1L;
56+
</description>
4557
<properties>
4658
<property name="xpath">
4759
<value>
@@ -67,4 +79,49 @@
6779
</properties>
6880
</rule>
6981

82+
<rule name="MCR.ResourceResolver"
83+
language="java"
84+
message="Resources should be accessed using MCRResourceResolver or MCRResourceHelper"
85+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
86+
<description>
87+
Accessing resources directly via Class, ClassLoader or ServletContext should only be done
88+
- in classes that are part of the setup of the MyCoRe application (e.g. MCRConfigurationInputStream, MCRComponent),
89+
- in classes that are used by MCRResourceResolver (e.g. MCRResourceProvider, MCRResourceLocator) or
90+
- in rare cases where module specific files need to be accessed (e.g. MCRCoreVersion accessing git.properties).
91+
Otherwise, MCRResourceResolver or MCRResourceHelper should be used.
92+
</description>
93+
<priority>1</priority>
94+
<properties>
95+
<property name="xpath">
96+
<value>
97+
<![CDATA[
98+
//(MethodCall|MethodReference)
99+
[*[position() = 1 and pmd-java:typeIs('java.lang.Class')]]
100+
[@MethodName=(
101+
'getResource',
102+
'getResourceAsStream'
103+
)]
104+
|
105+
//(MethodCall|MethodReference)
106+
[*[position() = 1 and pmd-java:typeIs('java.lang.ClassLoader')]]
107+
[@MethodName=(
108+
'resources',
109+
'getResource',
110+
'getResources',
111+
'getResourceAsStream'
112+
)]
113+
|
114+
//(MethodCall|MethodReference)
115+
[*[position() = 1 and pmd-java:typeIs('jakarta.servlet.ServletContext')]]
116+
[@MethodName=(
117+
'getResource',
118+
'getResourcePaths',
119+
'getResourceAsStream'
120+
)]
121+
]]>
122+
</value>
123+
</property>
124+
</properties>
125+
</rule>
126+
70127
</ruleset>

0 commit comments

Comments
 (0)