New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added AvoidRecompilingPatterns (Kotlin) including test #296
Merged
Merged
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
de508ae
fix for multi match because of multiple import matches
cc5687a
AvoidRecompilingXPathExpression. Convert applicable rules to Kotlin #185
jborgers 3d3e39d
Merge branch 'master' into kotlin/AvoidRecompilingXPathExpression
jborgers 7da8d59
Convert applicable rules to Kotlin #185,
jborgers 7824328
Add TODO to EvaluateRules doc
jborgers 1ed00dd
Merge remote-tracking branch 'origin/kotlin/AvoidDecimalAndChoiceForm…
jborgers 99198af
Added good ThreadLocal example and removed 'TODO'
jborgers e921a27
added braces around 'x and y' for clarity
jborgers ae12b77
added AvoidRecompilingPatterns including test (Convert applicable rul…
7af02b9
Merge branch 'master' into kotlin/AvoidRecompilingPatterns
ac8957a
update kotlin progress table
58c759f
removed xpath version
jborgers 7452395
removed other xpath version
jborgers 74d572f
added testkt script
jborgers aca86ac
Merge remote-tracking branch 'origin/master' into kotlin/AvoidRecompi…
jborgers 22eec3b
Merge pull request #295 from jborgers/kotlin/AvoidRecompilingXPathExp…
jborgers e12d3ca
update EvaluateRulesForKotlin
jborgers 00adc32
Fix AvoidRecompilingXPathExpression Java Good example.
jborgers 1f61132
Update EvaluateRulesForKotlin
jborgers c221ab7
added AvoidRecompilingPatterns including test (Convert applicable rul…
25bc928
remove deprecated version tags
a1a3807
Merge remote-tracking branch 'origin/kotlin/AvoidRecompilingPatterns'…
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...test/java/com/jpinpoint/perf/lang/kotlin/ruleset/common/AvoidRecompilingPatternsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.jpinpoint.perf.lang.kotlin.ruleset.common; | ||
|
||
import net.sourceforge.pmd.testframework.PmdRuleTst; | ||
|
||
public class AvoidRecompilingPatternsTest extends PmdRuleTst { | ||
} |
100 changes: 100 additions & 0 deletions
100
.../resources/com/jpinpoint/perf/lang/kotlin/ruleset/common/xml/AvoidRecompilingPatterns.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?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>violation: Avoid recompiling patterns</description> | ||
<expected-problems>3</expected-problems> | ||
<expected-linenumbers>5,12,13</expected-linenumbers> | ||
<code><![CDATA[ | ||
import java.util.regex.Pattern | ||
|
||
class Foo { | ||
fun createNameFromAction(action: String?) { | ||
val p = Pattern.compile("(?=\\p{Lu})") // bad | ||
} | ||
|
||
companion object { | ||
const val PATTERN: String = "^\\s*%s=(.*)" | ||
|
||
fun methodBad(action: String?) { | ||
val p = Pattern.compile("(?=\\p{Lu})") // bad: violation, should not be inside method, make static final | ||
val s = Pattern.compile(PATTERN) // bad: violation | ||
} | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
<test-code> | ||
<description>no violation: Avoid recompiling patterns</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
object Bar { | ||
const val PATTERN: String = "^\\s*%s=(.*)" | ||
|
||
fun createEventNameFromAction(action: String): List<*> { | ||
val q1 = Pattern.compile("($action?=\\p{Lu})") // ok, dynamic -- TODO make function for this? | ||
val q2 = Pattern.compile("(" + action + "?=\\p{Lu})") // ok, dynamic | ||
val q3 = Pattern.compile("(${action}?=\\p{Lu})") // ok, dynamic | ||
val r = Pattern.compile(String.format(PATTERN, action)) // ok, dynamic | ||
val t = Pattern.compile(action) // dynamic, so ok (fix for JPCC-63) | ||
|
||
val actionSplit = q.split(action) // ok | ||
val list: MutableList<Array<String>> = ArrayList() | ||
list.add(actionSplit) | ||
return list | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>no violation: Avoid recompiling patterns, other compile</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
class GoodHere { | ||
fun otherCompile() { | ||
MyCompiler.compile("stuff") | ||
} | ||
} | ||
|
||
internal object MyCompiler { | ||
fun compile(s: String?) { | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>violation: Avoid recompiling patterns, with chained operations, issue #180</description> | ||
<expected-problems>2</expected-problems> | ||
<expected-linenumbers>5,10</expected-linenumbers> | ||
<code><![CDATA[ | ||
import java.util.regex.Pattern; | ||
|
||
class Foo { | ||
private fun isAmountAllowed(amount: String): Boolean { | ||
return LIMIT > Pattern.compile(SEP).matcher(amount).replaceAll("").length() // bad | ||
} | ||
|
||
private val isAmountAllowed2: Boolean | ||
get() { | ||
val bla: String = Pattern.compile("[.|,]").matcher("5.000,00").replaceAll("") // bad | ||
val b = (11 > bla.length) | ||
return b | ||
} | ||
|
||
companion object { | ||
private const val LIMIT = 11 | ||
private const val SEP = "[.|,]" | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
</test-data> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove version property, since it is deprecated in pmd-7