あなたは間違いなくStringUtils
クラスで混合することによって影響を受けています。これを実行する場合:
String...
7
0
43:[charAt, codePointAt, codePointBefore, codePointCount, compareTo, compareToIgnoreCase, concat, contains, contentEquals, copyValueOf, endsWith, equals, equalsIgnoreCase, format, getBytes, getChars, getClass, hashCode, indexOf, intern, isEmpty, join, lastIndexOf, length, matches, notify, notifyAll, offsetByCodePoints, regionMatches, replace, replaceAll, replaceFirst, split, startsWith, subSequence, substring, toCharArray, toLowerCase, toString, toUpperCase, trim, valueOf, wait]
StringUtils...
0
0
StringUtils mixin...
0
0
149:[abbreviate, abbreviateMiddle, appendIfMissing, appendIfMissingIgnoreCase, capitalize, center, charAt, chomp, chop, codePointAt, codePointBefore, codePointCount, compareTo, compareToIgnoreCase, concat, contains, containsAny, containsIgnoreCase, containsNone, containsOnly, containsWhitespace, contentEquals, copyValueOf, countMatches, defaultIfBlank, defaultIfEmpty, defaultString, deleteWhitespace, difference, endsWith, endsWithAny, endsWithIgnoreCase, equals, equalsIgnoreCase, format, getBytes, getCR, getChars, getClass, getEMPTY, getFuzzyDistance, getINDEX_NOT_FOUND, getJaroWinklerDistance, getLF, getLevenshteinDistance, getPAD_LIMIT, getSPACE, hashCode, indexOf, indexOfAny, indexOfAnyBut, indexOfDifference, indexOfIgnoreCase, intern, isAllLowerCase, isAllUpperCase, isAlpha, isAlphaSpace, isAlphanumeric, isAlphanumericSpace, isAsciiPrintable, isBlank, isEmpty, isNotBlank, isNotEmpty, isNumeric, isNumericSpace, isWhitespace, join, lastIndexOf, lastIndexOfAny, lastIndexOfIgnoreCase, lastOrdinalIndexOf, left, leftPad, length, lowerCase, matches, mid, normalizeSpace, notify, notifyAll, offsetByCodePoints, ordinalIndexOf, overlay, prependIfMissing, prependIfMissingIgnoreCase, regionMatches, remove, removeEnd, removeEndIgnoreCase, removePattern, removeStart, removeStartIgnoreCase, repeat, replace, replaceAll, replaceChars, replaceEach, replaceEachRepeatedly, replaceFirst, replaceOnce, replacePattern, reverse, reverseDelimited, right, rightPad, setCR, setEMPTY, setINDEX_NOT_FOUND, setLF, setPAD_LIMIT, setSPACE, split, splitByCharacterType, splitByCharacterTypeCamelCase, splitByWholeSeparator, splitByWholeSeparatorPreserveAllTokens, splitPreserveAllTokens, startsWith, startsWithAny, startsWithIgnoreCase, strip, stripAccents, stripEnd, stripStart, stripToEmpty, stripToNull, subSequence, substring, substringAfter, substringAfterLast, substringBefore, substringBeforeLast, substringBetween, substringsBetween, swapCase, toCharArray, toLowerCase, toString, toUpperCase, trim, trimToEmpty, trimToNull, uncapitalize, upperCase, valueOf, wait, wrap]
行動がmixin StringUtils
なしで異なっていることを表示:
import org.apache.commons.lang3.StringUtils
String str = ",,,,,,"
println "String..."
println str.split(",",-1).length
println str.split(",").length
def methods = String.metaClass.methods*.name.sort().unique()
println "$methods.size:$methods"
println "\nStringUtils..."
println StringUtils.split(str, ",").length
println StringUtils.split(str,",",-1).length
println "\nStringUtils mixin..."
String.mixin StringUtils
println str.split(",",-1).length
println str.split(",").length
methods = String.metaClass.methods*.name.sort().unique()
println "$methods.size:$methods"
あなたは出力が得られます。 普通のJavaのsplit()
メソッドを使用した結果である-1と0を付け加えれば、Groovyは7を返しています。そして、Java、Groovyは同じ結果を返します。
StringUtils
-1パラメータの有無にかかわらず0を返します。
また、あなたはString
クラスには、追加の方法がそうStringUtils
で見つかったものと一致する149個の方法を、持っているString
を示しミックスインを適用する前に43個のメソッドを持っていることがわかります、あなたがいることがわかりますprintln "\StringUtils..."
ステートメントの後の2行は、StringUtils
で混合して実行した場合と同じ結果を出力します。両方のステートメントは0を返します。
mixinを実行する場合、元のString 'str'が渡される点でcurryingと似ています最初の引数としてStringUtils.split()
メソッドに渡します。このため、2つの引数と1のそれぞれを持つmixinを使用するときの2つのステートメントは、2と3の引数と2つの引数を持つmixinを除いたStringUtils
を使用する2つのステートメントに相当します。具体的には
:
str.split(",",-1) == StringUtils.split(str, ",", -1)
私は-1パラメータをドロップする場合は、ミックスイン
を適用したら、それは0ですが、あなたはそれを印刷する何を期待し出力しますか? – pczeus
申し訳ありませんが、私の誤りは、私のスクリプトに 'String.mixin org.apache.commons.lang.StringUtils'がありました。このような単純な振る舞いを無効にすることはできますが、それは非常に奇妙です。 – haventchecked
Javaタグを削除しました。これは、元のメソッドをgroovyのmixinで上書きしたときに呼び出す場合と同じです。 –