[java] Rename new rule to UseIOStreamsWithApacheCommonsFileItem

This commit is contained in:
Andreas Dangel
2020-06-13 18:44:07 +02:00
parent bb3223d92c
commit 48583f9f58
3 changed files with 74 additions and 63 deletions

View File

@ -58,58 +58,6 @@ sb.append('a'); // use this instead
</example>
</rule>
<rule name="AvoidApacheCommonsFileItemNonStreaming"
since="6.17.0"
language="java"
message="Avoid memory intensive FileItem.get and FileItem.getString"
class="net.sourceforge.pmd.lang.rule.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidapachecommonsfileitemnonstreaming">
<description>
Problem: Use of FileItem.get and FileItem.getString could exhaust memory since they load the entire file into memory&#13;
Solution: Use streaming methods and buffering.
</description>
<priority>2</priority>
<properties>
<property name="version" value="1.0"/>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix/Name
[ends-with(@Image, '.get') or ends-with(@Image, '.getString')]
[
starts-with(@Image, concat(
ancestor::MethodDeclaration//FormalParameter/Type/ReferenceType/ClassOrInterfaceType[typeIs('org.apache.commons.fileupload.FileItem')]/../../..//VariableDeclaratorId/@Image,
'.')
) or
starts-with(@Image, concat(
ancestor::MethodDeclaration//LocalVariableDeclaration/Type/ReferenceType/ClassOrInterfaceType[typeIs('org.apache.commons.fileupload.FileItem')]/../../..//VariableDeclaratorId/@Image,
'.')
) or
starts-with(@Image, concat(
ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration/Type/ReferenceType/ClassOrInterfaceType[typeIs('org.apache.commons.fileupload.FileItem')]/../../..//VariableDeclaratorId/@Image,
'.')
)
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class FileStuff {
private String bad(FileItem fileItem) {
return fileItem.getString();
}
private InputStream good(FileItem fileItem) {
return fileItem.getInputStream();
}
}
]]>
</example>
</rule>
<rule name="AvoidArrayLoops"
language="java"
since="3.5"
@ -985,6 +933,63 @@ if (s.indexOf('d') {}
</example>
</rule>
<rule name="UseIOStreamsWithApacheCommonsFileItem"
since="6.25.0"
language="java"
message="Avoid memory intensive FileItem.get() or FileItem.getString()"
class="net.sourceforge.pmd.lang.rule.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#useiostreamswithapachecommonsfileitem">
<description>
Problem: Use of [FileItem.get()](https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html#get--)
and [FileItem.getString()](https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html#getString--)
could exhaust memory since they load the entire file into memory.
Solution: Use IO streams and buffering.
</description>
<priority>2</priority>
<properties>
<property name="version" value="1.0"/>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix/Name
[ends-with(@Image, '.get') or ends-with(@Image, '.getString')]
[
starts-with(@Image, concat(
ancestor::MethodDeclaration//FormalParameter/Type/ReferenceType/ClassOrInterfaceType[typeIs('org.apache.commons.fileupload.FileItem')]/../../..//VariableDeclaratorId/@Image,
'.')
) or
starts-with(@Image, concat(
ancestor::MethodDeclaration//LocalVariableDeclaration/Type/ReferenceType/ClassOrInterfaceType[typeIs('org.apache.commons.fileupload.FileItem')]/../../..//VariableDeclaratorId/@Image,
'.')
) or
starts-with(@Image, concat(
ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration/Type/ReferenceType/ClassOrInterfaceType[typeIs('org.apache.commons.fileupload.FileItem')]/../../..//VariableDeclaratorId/@Image,
'.')
)
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
import org.apache.commons.fileupload.FileItem;
public class FileStuff {
private String bad(FileItem fileItem) {
return fileItem.getString();
}
private InputStream good(FileItem fileItem) {
return fileItem.getInputStream();
}
}
]]>
</example>
</rule>
<rule name="UselessStringValueOf"
since="3.8"
message="No need to call String.valueOf to append to a string."

View File

@ -1,4 +1,4 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
@ -6,5 +6,5 @@ package net.sourceforge.pmd.lang.java.rule.performance;
import net.sourceforge.pmd.testframework.PmdRuleTst;
public class AvoidApacheCommonsFileItemNonStreamingTest extends PmdRuleTst {
public class UseIOStreamsWithApacheCommonsFileItemTest extends PmdRuleTst {
}

View File

@ -3,6 +3,7 @@
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>violation: FileItem get() used</description>
<expected-problems>3</expected-problems>
@ -28,6 +29,7 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>violation: FileItem get() used with fully qualified class reference</description>
<expected-problems>3</expected-problems>
@ -52,6 +54,7 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>violation: FileItem getString() used</description>
<expected-problems>4</expected-problems>
@ -77,6 +80,7 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>violation: FileItem getString() used with fully qualified class reference</description>
<expected-problems>4</expected-problems>
@ -101,6 +105,7 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>no violation: harmless get methods</description>
<expected-problems>0</expected-problems>
@ -118,6 +123,7 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>Harmless getString methods</description>
<expected-problems>0</expected-problems>