Avoid using FileInput/Output - see JDK-8080225
This commit is contained in:
@ -112,6 +112,123 @@ public class Test {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="AvoidFileInputStream"
|
||||
since="5.8.2"
|
||||
message="Avoid instantiating FileInputStream"
|
||||
language="java"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidfileinputstream">
|
||||
<description>
|
||||
Use Files.newInputStream(Paths.get(fileName)) in place of new FileInputStream(fileName).
|
||||
</description>
|
||||
<priority>1</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimaryPrefix/AllocationExpression/ClassOrInterfaceType[typeof(@Image, 'java.io.FileInputStream', 'FileInputStream')]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
FileInputStream fis = new FileInputStream(fileName); // causes garbage collection pauses, even if properly closed
|
||||
|
||||
try(InputStream is = Files.newInputStream(Paths.get(fileName))) { // prevent GC pauses, no finalization
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="AvoidFileOutputStream"
|
||||
since="5.8.2"
|
||||
message="Avoid instantiating FileOutputStream"
|
||||
language="java"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidfileoutputstream">
|
||||
<description>
|
||||
Use Files.newOutputStream(Paths.get(fileName)) in place of new FileOutputStream(fileName).
|
||||
</description>
|
||||
<priority>1</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimaryPrefix/AllocationExpression/ClassOrInterfaceType[typeof(@Image, 'java.io.FileOutputStream', 'FileOutputStream')]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
FileOutputStream fos = new FileOutputStream(fileName); // causes garbage collection pauses, even if properly closed
|
||||
|
||||
try(OutputStream os = Files.newOutputStream(Paths.get(fileName))) { // prevent GC pauses, no finalization
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
|
||||
<rule name="AvoidFileReader"
|
||||
since="5.8.2"
|
||||
message="Avoid instantiating FileReader"
|
||||
language="java"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidfilereader">
|
||||
<description>
|
||||
Use Files.newBufferedReader(Paths.get(fileName)) in place of new FileReader(fileName).
|
||||
</description>
|
||||
<priority>1</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimaryPrefix/AllocationExpression/ClassOrInterfaceType[typeof(@Image, 'java.io.FileReader', 'FileReader')]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
FileReader fr = new FileReader(fileName); // causes garbage collection pauses, even if properly closed
|
||||
|
||||
try(BufferedReader br = Files.newBufferedReader(Paths.get(fileName), StandardCharsets.UTF_8)) { // prevent GC pauses, no finalization
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="AvoidFileWriter"
|
||||
since="5.8.2"
|
||||
message="Avoid instantiating FileWriter"
|
||||
language="java"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidfilewriter">
|
||||
<description>
|
||||
Use Files.newBufferedWriter(Paths.get(fileName)) in place of new FileWriter(fileName).
|
||||
</description>
|
||||
<priority>1</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimaryPrefix/AllocationExpression/ClassOrInterfaceType[typeof(@Image, 'java.io.FileWriter', 'FileWriter')]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
FileWriter fw = new FileWriter(fileName); // causes garbage collection pauses, even if properly closed
|
||||
|
||||
try(BufferedWriter bw = Files.newBufferedWriter(Paths.get(fileName), StandardCharsets.UTF_8)) { // prevent GC pauses, no finalization
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="AvoidInstantiatingObjectsInLoops"
|
||||
since="2.2"
|
||||
message="Avoid instantiating new objects inside loops"
|
||||
|
@ -29,4 +29,9 @@ rather, use a wrapper ruleset such as migrating_to_13.xml.
|
||||
<rule ref="category/java/performance.xml/LongInstantiation" deprecated="true" />
|
||||
<rule ref="category/java/performance.xml/ShortInstantiation" deprecated="true" />
|
||||
|
||||
<rule ref="category/java/performance.xml/AvoidFileInputStream" deprecated="true" />
|
||||
<rule ref="category/java/performance.xml/AvoidFileOutputStream" deprecated="true" />
|
||||
<rule ref="category/java/performance.xml/AvoidFileReader" deprecated="true" />
|
||||
<rule ref="category/java/performance.xml/AvoidFileWriter" deprecated="true" />
|
||||
|
||||
</ruleset>
|
||||
|
@ -18,6 +18,10 @@ public class PerformanceRulesTest extends SimpleAggregatorTst {
|
||||
addRule(RULESET, "AddEmptyString");
|
||||
addRule(RULESET, "AppendCharacterWithChar");
|
||||
addRule(RULESET, "AvoidArrayLoops");
|
||||
addRule(RULESET, "AvoidFileInputStream");
|
||||
addRule(RULESET, "AvoidFileOutputStream");
|
||||
addRule(RULESET, "AvoidFileReader");
|
||||
addRule(RULESET, "AvoidFileWriter");
|
||||
addRule(RULESET, "AvoidInstantiatingObjectsInLoops");
|
||||
addRule(RULESET, "AvoidUsingShortType");
|
||||
addRule(RULESET, "BigIntegerInstantiation");
|
||||
|
@ -0,0 +1,36 @@
|
||||
<?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><![CDATA[
|
||||
instantiating FileInputStream
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.io.FileInputStream;
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
FileInputStream is = new FileInputStream(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
instantiating java.io.FileInputStream
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
java.io.FileInputStream is = new java.io.FileInputStream(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
</test-data>
|
@ -0,0 +1,36 @@
|
||||
<?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><![CDATA[
|
||||
instantiating FileOutputStream
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.io.FileOutputStream;
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
FileOutputStream os = new FileOutputStream(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
instantiating java.io.FileOutputStream
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
java.io.FileOutputStream os = new java.io.FileOutputStream(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
</test-data>
|
@ -0,0 +1,36 @@
|
||||
<?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><![CDATA[
|
||||
instantiating FileReader
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.io.FileReader;
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
FileReader rd = new FileReader(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
instantiating java.io.FileReader
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
java.io.FileReader rd = new java.io.FileReader(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
</test-data>
|
@ -0,0 +1,36 @@
|
||||
<?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><![CDATA[
|
||||
instantiating FileWriter
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.io.FileWriter;
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
FileWriter wr = new FileWriter(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
instantiating java.io.FileWriter
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
java.io.FileWriter wr = new java.io.FileWriter(fileName);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
</test-data>
|
Reference in New Issue
Block a user