From dde8c01dd3fc8c880742a6f46c6f89d05370b22d Mon Sep 17 00:00:00 2001 From: jborgers Date: Fri, 2 Aug 2024 23:12:38 +0200 Subject: [PATCH] Migrate java rules to pmd 7.x.x #309: rule number 10: AvoidRecreatingDateTimeFormatter --- .../resources/category/java/common.xml | 55 ---------- src/main/resources/category/java/common.xml | 69 +++++++++--- .../AvoidRecreatingDateTimeFormatterTest.java | 0 .../xml/AvoidRecreatingDateTimeFormatter.xml | 101 ++++++++++++++---- 4 files changed, 130 insertions(+), 95 deletions(-) rename src/{test-todo => test/java/com/jpinpoint/perf/lang}/java/ruleset/common/AvoidRecreatingDateTimeFormatterTest.java (100%) rename src/{test-todo/resources-todo => test/resources/com/jpinpoint/perf}/lang/java/ruleset/common/xml/AvoidRecreatingDateTimeFormatter.xml (51%) diff --git a/src/main-todo/resources/category/java/common.xml b/src/main-todo/resources/category/java/common.xml index c4040061..c433f69e 100644 --- a/src/main-todo/resources/category/java/common.xml +++ b/src/main-todo/resources/category/java/common.xml @@ -1,62 +1,7 @@ jPinpoint rule set for performance aware Java coding, sponsored by Rabobank. - - - XPathExpression is created and compiled on every method call, compiled possibly implicitly by XPath::evaluate. - Problem: Creation of XPath and compilation of XPathExpression takes time. It may slow down your application. - Solution: 1. Avoid XPath usage. 2. Compile the xpath expression as String into a XPathExpression. However, since XPath and XPathExpression classes are thread-unsafe, they are not easily cached. Caching it in a ThreadLocal may be a solution. - (jpinpoint-rules) - 2 - - - - XPathExpression is created and compiled on every method call, compiled possibly implicitly by XPath::evaluate. + Problem: Creation of XPath and compilation of XPathExpression takes time. It may slow down your application. + Solution: 1. Avoid XPath usage. 2. Compile the xpath expression as String into a XPathExpression. However, since XPath and XPathExpression classes are thread-unsafe, they are not easily cached. Caching it in a ThreadLocal may be a solution. + (jpinpoint-rules) + 2 + + + + - - - - + + + + - - + ]]> + + + + + Problem: Recreating a DateTimeFormatter is relatively expensive. + Solution: Java 8+ java.time.DateTimeFormatter is thread-safe and can be shared among threads. Create the + formatter from a pattern only once, to initialize a static final field. + (jpinpoint-rules) + 2 + + + + + + + + + diff --git a/src/test-todo/java/ruleset/common/AvoidRecreatingDateTimeFormatterTest.java b/src/test/java/com/jpinpoint/perf/lang/java/ruleset/common/AvoidRecreatingDateTimeFormatterTest.java similarity index 100% rename from src/test-todo/java/ruleset/common/AvoidRecreatingDateTimeFormatterTest.java rename to src/test/java/com/jpinpoint/perf/lang/java/ruleset/common/AvoidRecreatingDateTimeFormatterTest.java diff --git a/src/test-todo/resources-todo/lang/java/ruleset/common/xml/AvoidRecreatingDateTimeFormatter.xml b/src/test/resources/com/jpinpoint/perf/lang/java/ruleset/common/xml/AvoidRecreatingDateTimeFormatter.xml similarity index 51% rename from src/test-todo/resources-todo/lang/java/ruleset/common/xml/AvoidRecreatingDateTimeFormatter.xml rename to src/test/resources/com/jpinpoint/perf/lang/java/ruleset/common/xml/AvoidRecreatingDateTimeFormatter.xml index 221875b9..ff84f171 100644 --- a/src/test-todo/resources-todo/lang/java/ruleset/common/xml/AvoidRecreatingDateTimeFormatter.xml +++ b/src/test/resources/com/jpinpoint/perf/lang/java/ruleset/common/xml/AvoidRecreatingDateTimeFormatter.xml @@ -4,9 +4,57 @@ 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"> - Avoid recreation of DateTimeFormatter object. - 13 - 4,5,6,14,17,20,23,27,30,34,46,53,57 + Avoid recreation of java.time.format DateTimeFormatter object. + 9 + 5,6,7,12,16,19,23,32,38 + + + + + Avoid recreation of joda DateTimeFormatter object. + 11 + 4,5,6,12,15,18,21,24,27,31,40 + - Avoid recreation of DateTimeFormatter object. But allow in constructor if member variable is final. + Avoid recreation of joda DateTimeFormatter object. But allow in constructor if member variable is final. 3 5,6,9 - Avoid recreation of DateTimeFormatter object. But allow if there is a parameter from method call involved. - 3 - 7,8,9 + Avoid recreation of joda DateTimeFormatter object. But allow if there is a parameter from method call involved. + 4 + 7,8,9,10 + + + + Avoid recreation of java.time.DateTimeFormatter object. But allow if there is a parameter from method call involved. + 3 + 7,8,9 +