forked from phoedos/pmd
Implements the RuleChain
within the QueueableWithoutFinalizerRule
.
Implements the [`RuleChain`](https://docs.pmd-code.org/latest/pmd_userdocs_extending_writing_java_rules.html#economic-traversal-the-rulechain) to traverse all classes within the file.
This commit is contained in:
parent
4e4ca6bb70
commit
1a9e721fb6
@ -10,6 +10,8 @@ import net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression;
|
|||||||
import net.sourceforge.pmd.lang.apex.ast.ASTParameter;
|
import net.sourceforge.pmd.lang.apex.ast.ASTParameter;
|
||||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
|
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
|
||||||
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
|
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
|
||||||
|
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scans classes which implement the `Queueable` interface. If the `public void
|
* Scans classes which implement the `Queueable` interface. If the `public void
|
||||||
@ -25,23 +27,19 @@ public class QueueableWithoutFinalizerRule extends AbstractApexRule {
|
|||||||
private static final String QUEUEABLE_CONTEXT = "queueablecontext";
|
private static final String QUEUEABLE_CONTEXT = "queueablecontext";
|
||||||
private static final String SYSTEM_ATTACH_FINALIZER = "system.attachfinalizer";
|
private static final String SYSTEM_ATTACH_FINALIZER = "system.attachfinalizer";
|
||||||
|
|
||||||
/** Scans the top level class and all inner classes. */
|
|
||||||
@Override
|
@Override
|
||||||
public Object visit(ASTUserClass topLevelClass, Object data) {
|
protected @NonNull RuleTargetSelector buildTargetSelector() {
|
||||||
scanClassForViolation(topLevelClass, data);
|
return RuleTargetSelector.forTypes(ASTUserClass.class);
|
||||||
for (ASTUserClass innerClass : topLevelClass.descendants(ASTUserClass.class).toList()) {
|
|
||||||
scanClassForViolation(innerClass, data);
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the class implements the `Queueable` interface and the `execute(QueueableContext context)`
|
* If the class implements the `Queueable` interface and the `execute(QueueableContext context)`
|
||||||
* does not call the `System.attachFinalizer(Finalizer f)` method, then add a violation.
|
* does not call the `System.attachFinalizer(Finalizer f)` method, then add a violation.
|
||||||
*/
|
*/
|
||||||
private void scanClassForViolation(ASTUserClass theClass, Object data) {
|
@Override
|
||||||
|
public Object visit(ASTUserClass theClass, Object data) {
|
||||||
if (!implementsTheQueueableInterface(theClass)) {
|
if (!implementsTheQueueableInterface(theClass)) {
|
||||||
return;
|
return data;
|
||||||
}
|
}
|
||||||
for (ASTMethod theMethod : theClass.descendants(ASTMethod.class).toList()) {
|
for (ASTMethod theMethod : theClass.descendants(ASTMethod.class).toList()) {
|
||||||
if (isTheExecuteMethodOfTheQueueableInterface(theMethod)
|
if (isTheExecuteMethodOfTheQueueableInterface(theMethod)
|
||||||
@ -49,6 +47,7 @@ public class QueueableWithoutFinalizerRule extends AbstractApexRule {
|
|||||||
asCtx(data).addViolation(theMethod);
|
asCtx(data).addViolation(theMethod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Determines if the class implements the Queueable interface. */
|
/** Determines if the class implements the Queueable interface. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user