From bd479e4cc44612a58b56dd56a2cada86c11590fc Mon Sep 17 00:00:00 2001 From: Jonathan Wiesel Date: Mon, 4 Oct 2021 00:01:42 +0200 Subject: [PATCH] Add GetDescribeShouldUseSObjectDescribeOptions Apex rule --- .../resources/category/apex/performance.xml | 36 +++++++++++ ...beShouldUseSObjectDescribeOptionsTest.java | 11 ++++ ...escribeShouldUseSObjectDescribeOptions.xml | 64 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/performance/GetDescribeShouldUseSObjectDescribeOptionsTest.java create mode 100644 pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/performance/xml/GetDescribeShouldUseSObjectDescribeOptions.xml diff --git a/pmd-apex/src/main/resources/category/apex/performance.xml b/pmd-apex/src/main/resources/category/apex/performance.xml index e1f05fb21a..fb29264fdb 100644 --- a/pmd-apex/src/main/resources/category/apex/performance.xml +++ b/pmd-apex/src/main/resources/category/apex/performance.xml @@ -137,6 +137,42 @@ public class Something { + + +Explicitly supplying appropriate SObjectDescribeOptions to getDescribe calls to avoid default eager-loading possibly unnecesary +describe information on API version earlier than v43 when may not be really required + + 3 + + + + + + + + + + + accounts) { + if (Account.SObjectType.getDescribe(SObjectDescribeOptions.DEFERRED).isCreateable()) { + insert accounts; + } + } +} +]]> + + + + + + + No describer options + 1 + accounts) { + if (Account.SObjectType.getDescribe().isCreateable()) { + insert accounts; + } + } +} + ]]> + + + + Correct describer option check + 0 + accounts) { + if (Account.SObjectType.getDescribe(SObjectDescribeOptions.DEFERRED).isCreateable()) { + insert accounts; + } + } +} + ]]> + + + + Default describer option check allowing default + 0 + accounts) { + if (Account.SObjectType.getDescribe(SObjectDescribeOptions.DEFAULT).isCreateable()) { + insert accounts; + } + } +} + ]]> + + + + Default describer option check restricting default option + true + 1 + accounts) { + if (Account.SObjectType.getDescribe(SObjectDescribeOptions.DEFAULT).isCreateable()) { + insert accounts; + } + } +} + ]]> + + +