Incorporate code review feedback.

- Renames the rule to `QueueableWithoutFinalizer` to be more neutral.
- Provides a more robust description.
- Provides a more succinct error message.
- Provides a positive sample for the documentation .
This commit is contained in:
mitchspano
2024-11-12 19:46:58 +00:00
parent 83d8ca0169
commit 4e4ca6bb70
4 changed files with 40 additions and 15 deletions

View File

@ -18,7 +18,7 @@ import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
*
* @author mitchspano
*/
public class QueueableShouldAttachFinalizerRule extends AbstractApexRule {
public class QueueableWithoutFinalizerRule extends AbstractApexRule {
private static final String EXECUTE = "execute";
private static final String QUEUEABLE = "queueable";

View File

@ -285,29 +285,54 @@ Detects when a local variable is declared and/or assigned but not used.
</example>
</rule>
<rule name="QueueableShouldAttachFinalizer"
<rule name="QueueableWithoutFinalizer"
since="7.8.0"
language="apex"
message="It is best practice to call the `System.attachFinalizer(Finalizer f)` method within the `execute` method of a class which implements the `Queueable` interface."
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.QueueableShouldAttachFinalizerRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_bestpractices.html#queueableshouldattachfinalizer">
message="This Queueable doesn't attach a Finalizer"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.QueueableWithoutFinalizerRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_bestpractices.html#queueablewithoutfinalizer">
<description>
Detects when the Queueable interface is used but a Finalizer is not attached.
It is best practice to call the `System.attachFinalizer(Finalizer f)` method within the `execute` method of a class which implements the `Queueable` interface.
Without attaching a Finalizer, there is no way of designing error recovery actions should the Queueable action fail.
</description>
<priority>5</priority>
<example>
<![CDATA[
public class UserUpdater implements Queueable {
public List<User> usersToUpdate;
// Incorrect code, does not attach a finalizer.
public class UserUpdater implements Queueable {
public List<User> usersToUpdate;
public UserUpdater(List<User> usersToUpdate) {
this.usersToUpdate = usersToUpdate;
}
public UserUpdater(List<User> usersToUpdate) {
this.usersToUpdate = usersToUpdate;
}
public void execute(QueueableContext context) { // no Finalizer is attached
update usersToUpdate;
public void execute(QueueableContext context) { // no Finalizer is attached
update usersToUpdate;
}
}
// Proper code, attaches a finalizer.
public class UserUpdater implements Queueable, Finalizer {
public List<User> usersToUpdate;
public UserUpdater(List<User> usersToUpdate) {
this.usersToUpdate = usersToUpdate;
}
public void execute(QueueableContext context) {
System.attachFinalizer(this);
update usersToUpdate;
}
public void execute(FinalizerContext ctx) {
if (ctx.getResult() == ParentJobResult.SUCCESS) {
// Handle success
} else {
// Handle failure
}
}
}
]]>
</example>
</rule>

View File

@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.apex.rule.bestpractices;
import net.sourceforge.pmd.test.PmdRuleTst;
class QueueableShouldAttachFinalizerTest extends PmdRuleTst {
class QueueableWithoutFinalizerTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>[apex] Queueable Should Attach Finalizer - positive test case #5302</description>
<description>[apex] Queueable Without Finalizer - positive test case #5302</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>8</expected-linenumbers>
<code><![CDATA[
@ -23,7 +23,7 @@ public class UserUpdater implements Queueable {
]]></code>
</test-code>
<test-code>
<description>[apex] Queueable Should Attach Finalizer - negative test case #5302</description>
<description>[apex] Queueable Without Finalizer - negative test case #5302</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class UserUpdater implements Queueable, Finalizer {