forked from pmd/pmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # pmd-apex/src/main/resources/rulesets/apex/ruleset.xml
- Loading branch information
Showing
6 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ex/src/main/java/net/sourceforge/pmd/lang/apex/rule/performance/AvoidSoslInLoopsRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.apex.rule.performance; | ||
|
||
import net.sourceforge.pmd.lang.apex.ast.ASTDoLoopStatement; | ||
import net.sourceforge.pmd.lang.apex.ast.ASTForEachStatement; | ||
import net.sourceforge.pmd.lang.apex.ast.ASTForLoopStatement; | ||
import net.sourceforge.pmd.lang.apex.ast.ASTReturnStatement; | ||
import net.sourceforge.pmd.lang.apex.ast.ASTSoslExpression; | ||
import net.sourceforge.pmd.lang.apex.ast.ASTWhileLoopStatement; | ||
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule; | ||
import net.sourceforge.pmd.lang.ast.Node; | ||
|
||
public class AvoidSoslInLoopsRule extends AbstractApexRule { | ||
|
||
public AvoidSoslInLoopsRule() { | ||
setProperty(CODECLIMATE_CATEGORIES, "Performance"); | ||
// Note: Often more complicated as just moving the SOSL a few lines. | ||
// Involves Maps... | ||
setProperty(CODECLIMATE_REMEDIATION_MULTIPLIER, 150); | ||
setProperty(CODECLIMATE_BLOCK_HIGHLIGHTING, false); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTSoslExpression node, Object data) { | ||
if (insideLoop(node) && parentNotReturn(node) && parentNotForEach(node)) { | ||
addViolation(data, node); | ||
} | ||
return data; | ||
} | ||
|
||
private boolean parentNotReturn(ASTSoslExpression node) { | ||
return !(node.jjtGetParent() instanceof ASTReturnStatement); | ||
} | ||
|
||
private boolean parentNotForEach(ASTSoslExpression node) { | ||
return !(node.jjtGetParent() instanceof ASTForEachStatement); | ||
} | ||
|
||
private boolean insideLoop(ASTSoslExpression node) { | ||
Node n = node.jjtGetParent(); | ||
|
||
while (n != null) { | ||
if (n instanceof ASTDoLoopStatement || n instanceof ASTWhileLoopStatement | ||
|| n instanceof ASTForLoopStatement || n instanceof ASTForEachStatement) { | ||
return true; | ||
} | ||
n = n.jjtGetParent(); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...rc/test/resources/net/sourceforge/pmd/lang/apex/rule/performance/xml/AvoidSoslInLoops.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<test-data | ||
xmlns="http://pmd.sourceforge.net/rule-tests" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> | ||
|
||
<test-code> | ||
<description>Problematic Sosl in for each</description> | ||
<expected-problems>1</expected-problems> | ||
<code><![CDATA[ | ||
public class Foo { | ||
public void test1() { | ||
for(Integer i : new List<Integer>{1,2}) { | ||
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Problematic Sosl in for loop</description> | ||
<expected-problems>1</expected-problems> | ||
<code><![CDATA[ | ||
public class Foo { | ||
public void test1() { | ||
for(;;) { | ||
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Problematic Sosl in While loop</description> | ||
<expected-problems>1</expected-problems> | ||
<code><![CDATA[ | ||
public class Foo { | ||
public void test1() { | ||
while(true) { | ||
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Problematic Sosl in do loop</description> | ||
<expected-problems>1</expected-problems> | ||
<code><![CDATA[ | ||
public class Foo { | ||
public void test1() { | ||
do{ | ||
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; | ||
}while(true) ; | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Multiple problematic Sosl expressions</description> | ||
<expected-problems>2</expected-problems> | ||
<code><![CDATA[ | ||
public class Foo { | ||
public void test1() { | ||
do{ | ||
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; | ||
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name)]; | ||
}while(true) ; | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Return Sosl is even ok in loop</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
public class Foo { | ||
public List<Account> test1() { | ||
for(;;) { | ||
return [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>#29 SOSL For Loops should not throw an Avoid Sosl queries inside loops issue</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
public class Foo { | ||
public void test1() { | ||
for(List<sObject> a : [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity]) { | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
</test-data> |