Skip to content

Commit ebe579f

Browse files
committed
PMD: Add PostConstruct and PreDestroy
1 parent 3b31f0b commit ebe579f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

.config/pmd/java/ruleset.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,49 @@
218218
</properties>
219219
</rule>
220220

221+
<rule name="AvoidPostConstruct"
222+
language="java"
223+
message="Avoid @PostConstruct"
224+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
225+
<description>
226+
Using a `@PostConstruct` method is usually only done when field injection is used and initialization needs to be performed after that.
227+
228+
It's better to do this directly in the constructor with constructor injection, so that all logic will be encapsulated there.
229+
This also makes using the bean in environments where JavaEE is not present - for example in tests - a lot easier, as forgetting to call the `@PostConstruct` method is no longer possible.
230+
</description>
231+
<priority>3</priority>
232+
<properties>
233+
<property name="xpath">
234+
<value>
235+
<![CDATA[
236+
//MethodDeclaration[pmd-java:hasAnnotation('jakarta.annotation.PostConstruct')]
237+
]]>
238+
</value>
239+
</property>
240+
</properties>
241+
</rule>
242+
243+
<rule name="AvoidPreDestroy"
244+
language="java"
245+
message="Avoid @PreDestroy"
246+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
247+
<description>
248+
`@PreDestroy` should be replaced by implementing `AutoCloseable` and overwriting the `close` method instead.
249+
250+
This also makes using the bean in environments where JavaEE is not present - for example in tests - a lot easier, as forgetting to call the `@PreDestroy` method is no much more difficult.
251+
</description>
252+
<priority>3</priority>
253+
<properties>
254+
<property name="xpath">
255+
<value>
256+
<![CDATA[
257+
//MethodDeclaration[pmd-java:hasAnnotation('jakarta.annotation.PreDestroy')]
258+
]]>
259+
</value>
260+
</property>
261+
</properties>
262+
</rule>
263+
221264
<rule name="AvoidUnmanagedThreads"
222265
language="java"
223266
message="Avoid unmanaged threads"

0 commit comments

Comments
 (0)