new Android rule DoNotHardCodeSDCard taken from my blog article on how to use PMD in Android projects

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6960 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch 2009-06-13 20:34:03 +00:00
parent 6243e25400
commit 0e1bf46e07
4 changed files with 44 additions and 0 deletions

View File

@ -13,6 +13,7 @@ Dependencies updates: asm updated to 3.2
Android ruleset: CallSuperLast rule now also checks for finish() redefinitions
New rule:
Android: DoNotHardCodeSDCard
Controversial : AvoidLiteralsInIfCondition (patch 2591627)
StrictExceptions : AvoidCatchingGenericException

View File

@ -10,6 +10,7 @@ public class AndroidRulesTest extends SimpleAggregatorTst {
public void setUp() {
addRule("android", "CallSuperFirst");
addRule("android", "CallSuperLast");
addRule("android", "DoNotHardCodeSDCard");
addRule("android", "ProtectLogD");
addRule("android", "ProtectLogV");
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description>basic /sdcard test</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class MyActivity extends Activity {
protected void bad() {
String storageLocation = "/sdcard/mypackage";
}
protected void good() {
String storageLocation = Environment.getExternalStorageDirectory() + "/mypackage";
}
}
]]></code>
</test-code>
</test-data>

View File

@ -169,5 +169,28 @@ To get better results, make sure that the auxclasspath is defined for type resol
</example>
</rule>
<rule name="DoNotHardCodeSDCard" since="4.2.6"
message="Do not hardcode /sdcard."
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/android.html#DoNotHardCodeSDCard">
<description>Use Environment.getExternalStorageDirectory() instead of "/sdcard"</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>//Literal[starts-with(@Image,'"/sdcard')]</value>
</property>
</properties>
<example>
<![CDATA[
public class MyActivity extends Activity {
protected void foo() {
String storageLocation = "/sdcard/mypackage"; // BAD
storageLocation = Environment.getExternalStorageDirectory() + "/mypackage"; // GOOD
}
}
]]>
</example>
</rule>
</ruleset>