From 69a24126f7ae12c153bda1ce30fb169114a0fa91 Mon Sep 17 00:00:00 2001 From: mitchspano Date: Thu, 14 Nov 2024 23:29:44 +0000 Subject: [PATCH] Fix formatting and static code analysis findings. --- .../QueueableWithoutFinalizerRule.java | 116 +++++++++--------- .../QueueableWithoutFinalizerTest.java | 2 +- 2 files changed, 62 insertions(+), 56 deletions(-) diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerRule.java index bae5f2ef85..9acb39e07d 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerRule.java @@ -5,82 +5,88 @@ package net.sourceforge.pmd.lang.apex.rule.bestpractices; import java.util.List; + +import org.checkerframework.checker.nullness.qual.NonNull; + import net.sourceforge.pmd.lang.apex.ast.ASTMethod; import net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression; import net.sourceforge.pmd.lang.apex.ast.ASTParameter; import net.sourceforge.pmd.lang.apex.ast.ASTUserClass; 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 - * execute(QueueableContext context)` method does not call the `System.attachFinalizer(Finalizer f)` - * method, then a violation will be added to the `execute` method. + * execute(QueueableContext context)` method does not call the + * `System.attachFinalizer(Finalizer f)` method, then a violation will be added + * to the `execute` method. * * @author mitchspano */ public class QueueableWithoutFinalizerRule extends AbstractApexRule { - private static final String EXECUTE = "execute"; - private static final String QUEUEABLE = "queueable"; - private static final String QUEUEABLE_CONTEXT = "queueablecontext"; - private static final String SYSTEM_ATTACH_FINALIZER = "system.attachfinalizer"; + private static final String EXECUTE = "execute"; + private static final String QUEUEABLE = "queueable"; + private static final String QUEUEABLE_CONTEXT = "queueablecontext"; + private static final String SYSTEM_ATTACH_FINALIZER = "system.attachfinalizer"; - @Override - protected @NonNull RuleTargetSelector buildTargetSelector() { - return RuleTargetSelector.forTypes(ASTUserClass.class); - } + @Override + protected @NonNull RuleTargetSelector buildTargetSelector() { + return RuleTargetSelector.forTypes(ASTUserClass.class); + } - /** - * 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. - */ - @Override - public Object visit(ASTUserClass theClass, Object data) { - if (!implementsTheQueueableInterface(theClass)) { - return data; + /** + * 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. + */ + @Override + public Object visit(ASTUserClass theClass, Object data) { + if (!implementsTheQueueableInterface(theClass)) { + return data; + } + for (ASTMethod theMethod : theClass.descendants(ASTMethod.class).toList()) { + if (isTheExecuteMethodOfTheQueueableInterface(theMethod) + && !callsTheSystemAttachFinalizerMethod(theMethod)) { + asCtx(data).addViolation(theMethod); + } + } + return data; } - for (ASTMethod theMethod : theClass.descendants(ASTMethod.class).toList()) { - if (isTheExecuteMethodOfTheQueueableInterface(theMethod) - && !callsTheSystemAttachFinalizerMethod(theMethod)) { - asCtx(data).addViolation(theMethod); - } - } - return data; - } - /** Determines if the class implements the Queueable interface. */ - private boolean implementsTheQueueableInterface(ASTUserClass theClass) { - for (String interfaceName : theClass.getInterfaceNames()) { - if (interfaceName.equalsIgnoreCase(QUEUEABLE)) { - return true; - } + /** Determines if the class implements the Queueable interface. */ + private boolean implementsTheQueueableInterface(ASTUserClass theClass) { + for (String interfaceName : theClass.getInterfaceNames()) { + if (QUEUEABLE.equalsIgnoreCase(interfaceName)) { + return true; + } + } + return false; } - return false; - } - /** - * Determines if the method is the `execute(QueueableContext context)` method. Parameter count is - * checked to account for method overloading. - */ - private boolean isTheExecuteMethodOfTheQueueableInterface(ASTMethod theMethod) { - if (!theMethod.getCanonicalName().equalsIgnoreCase(EXECUTE)) { - return false; + /** + * Determines if the method is the `execute(QueueableContext context)` + * method. Parameter count is checked to account for method overloading. + */ + private boolean isTheExecuteMethodOfTheQueueableInterface(ASTMethod theMethod) { + if (!EXECUTE.equalsIgnoreCase(theMethod.getCanonicalName())) { + return false; + } + List parameters = theMethod.descendants(ASTParameter.class).toList(); + return parameters.size() == 1 && QUEUEABLE_CONTEXT.equalsIgnoreCase(parameters.get(0).getType()); } - List parameters = theMethod.descendants(ASTParameter.class).toList(); - return parameters.size() == 1 - && parameters.get(0).getType().equalsIgnoreCase(QUEUEABLE_CONTEXT); - } - /** Determines if the method calls the `System.attachFinalizer(Finalizer f)` method. */ - private boolean callsTheSystemAttachFinalizerMethod(ASTMethod theMethod) { - for (ASTMethodCallExpression methodCallExpression : - theMethod.descendants(ASTMethodCallExpression.class).toList()) { - if (methodCallExpression.getFullMethodName().equalsIgnoreCase(SYSTEM_ATTACH_FINALIZER)) { - return true; - } + /** + * Determines if the method calls the `System.attachFinalizer(Finalizer f)` + * method. + */ + private boolean callsTheSystemAttachFinalizerMethod(ASTMethod theMethod) { + for (ASTMethodCallExpression methodCallExpression : theMethod.descendants(ASTMethodCallExpression.class) + .toList()) { + if (SYSTEM_ATTACH_FINALIZER.equalsIgnoreCase(methodCallExpression.getFullMethodName())) { + return true; + } + } + return false; } - return false; - } } diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerTest.java index 35d20e8d9c..7daa72c169 100644 --- a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerTest.java +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/QueueableWithoutFinalizerTest.java @@ -7,5 +7,5 @@ package net.sourceforge.pmd.lang.apex.rule.bestpractices; import net.sourceforge.pmd.test.PmdRuleTst; class QueueableWithoutFinalizerTest extends PmdRuleTst { - // no additional unit tests + // no additional unit tests }