[java] Update rule AvoidCalendarDateCreation to use XPath 2.0
This commit is contained in:
@ -115,27 +115,29 @@ public class Test {
|
||||
</rule>
|
||||
|
||||
<rule name="AvoidCalendarDateCreation"
|
||||
since="6.17.0"
|
||||
since="6.25.0"
|
||||
language="java"
|
||||
message="A Calendar is used to create a Date or DateTime, this is expensive."
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
typeResolution="true"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidcalendardatecreation">
|
||||
<description>Problem: A Calendar is a heavyweight object and expensive to create.
|
||||
Solution: Use 'new Date()', Java 8+ java.time.[Local/Zoned]DateTime.now() or joda time '[Local]DateTime.now()'.
|
||||
<description>
|
||||
Problem: A Calendar is a heavyweight object and expensive to create.
|
||||
|
||||
Solution: Use `new Date()`, Java 8+ `java.time.LocalDateTime.now()` or `ZonedDateTime.now()`.
|
||||
</description>
|
||||
<priority>2</priority>
|
||||
<properties>
|
||||
<property name="version" value="1.0"/>
|
||||
<property name="version" value="2.0"/>
|
||||
<property name="xpath">
|
||||
<value><![CDATA[
|
||||
//PrimaryPrefix[Name[ends-with(@Image, 'Calendar.getInstance')]] [count(../PrimarySuffix) > 2 and ../PrimarySuffix[last()-1][@Image = 'getTime' or @Image='getTimeInMillis']]
|
||||
|
|
||||
//Block/BlockStatement//Expression/PrimaryExpression/
|
||||
PrimaryPrefix/Name[typeIs('java.util.Calendar') and (ends-with(@Image,'.getTime') or ends-with(@Image,'.getTimeInMillis'))]
|
||||
PrimaryPrefix/Name[pmd-java:typeIs('java.util.Calendar') and (ends-with(@Image,'.getTime') or ends-with(@Image,'.getTimeInMillis'))]
|
||||
|
|
||||
//ClassOrInterfaceType[typeIs('org.joda.time.DateTime') or typeIs('org.joda.time.LocalDateTime')][../Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name[ends-with(@Image, 'Calendar.getInstance')]]
|
||||
]]></value>
|
||||
//ClassOrInterfaceType[pmd-java:typeIs('org.joda.time.DateTime') or pmd-java:typeIs('org.joda.time.LocalDateTime')][../Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name[ends-with(@Image, 'Calendar.getInstance')]]
|
||||
]]></value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
|
@ -1,8 +1,9 @@
|
||||
<?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">
|
||||
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: [Gregorian]Calendar.getInstance().getTime()</description>
|
||||
<expected-problems>4</expected-problems>
|
||||
@ -13,18 +14,18 @@ import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class Foo {
|
||||
void foo() {
|
||||
Date now = Calendar.getInstance().getTime();
|
||||
setDate(Calendar.getInstance().getTime());
|
||||
setDate(GregorianCalendar.getInstance().getTime());
|
||||
void foo() {
|
||||
Date now = Calendar.getInstance().getTime();
|
||||
setDate(Calendar.getInstance().getTime());
|
||||
setDate(GregorianCalendar.getInstance().getTime());
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
Date now2 = cal.getTime();
|
||||
}
|
||||
private void setDate(Date when){
|
||||
}
|
||||
Calendar cal = Calendar.getInstance();
|
||||
Date now2 = cal.getTime();
|
||||
}
|
||||
private void setDate(Date when){
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
@ -37,12 +38,12 @@ import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class Foo {
|
||||
void foo() {
|
||||
DateTime nowDT1 = new DateTime(GregorianCalendar.getInstance());
|
||||
DateTime nowDT2 = new DateTime(Calendar.getInstance());
|
||||
}
|
||||
void foo() {
|
||||
DateTime nowDT1 = new DateTime(GregorianCalendar.getInstance());
|
||||
DateTime nowDT2 = new DateTime(Calendar.getInstance());
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
@ -53,12 +54,12 @@ public class Foo {
|
||||
import java.util.Calendar;
|
||||
|
||||
public class Foo {
|
||||
void foo() {
|
||||
long time = Calendar.getInstance().getTimeInMillis();
|
||||
String timeStr = Long.toString(Calendar.getInstance().getTimeInMillis());
|
||||
}
|
||||
void foo() {
|
||||
long time = Calendar.getInstance().getTimeInMillis();
|
||||
String timeStr = Long.toString(Calendar.getInstance().getTimeInMillis());
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
@ -69,14 +70,14 @@ public class Foo {
|
||||
import java.util.Calendar;
|
||||
|
||||
public class Foo {
|
||||
void foo() {
|
||||
long time1 = 0;
|
||||
Calendar cal = Calendar.getInstance();
|
||||
long time2 = cal.getTimeInMillis();
|
||||
time1 = cal.getTimeInMillis();
|
||||
}
|
||||
void foo() {
|
||||
long time1 = 0;
|
||||
Calendar cal = Calendar.getInstance();
|
||||
long time2 = cal.getTimeInMillis();
|
||||
time1 = cal.getTimeInMillis();
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
@ -87,15 +88,12 @@ import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class Foo {
|
||||
void foo() {
|
||||
int warmestMonth = Calendar.getInstance().AUGUST;
|
||||
long time = System.currentTimeMillis();
|
||||
Date now = new Date();
|
||||
}
|
||||
void foo() {
|
||||
int warmestMonth = Calendar.getInstance().AUGUST;
|
||||
long time = System.currentTimeMillis();
|
||||
Date now = new Date();
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
|
||||
|
||||
</test-data>
|
||||
|
Reference in New Issue
Block a user