Groovy "Contains" function deprecated

Hi,

I recently got a notification that the “contains” function in groovy was deprecated. Does anyone know of a replacement for it?

I made a test case in Groovy lang:

import java.lang.CharSequence
String text = "Hello, World!"
println "text contains it: " + text.contains("World!")
if (text instanceof CharSequence) {
    println "text of type java.lang.String is an instance of java.lang.CharSequece"
    CharSequence charseq = (CharSequence)text
    println "charseq contains it: " + charseq.contains("World!")
}

When I execute it, I got the following output in the log:

text contains it: true
text of type java.lang.String is an instance of java.lang.CharSequece
charseq contains it: true

This experiment proves that the following code is a valid Groovy statement:

         text.contains("World!")

And I believe it will stay valid in future. Yo do not have to worry about the notification you got too much.

I will tell you the reason why in the following comments.

3 Likes

Look at the following screenshot. Please note that the method name “contains” is stroke out by IDE. I suppose that the notification you got is talking about this IDE’s behavior.

You do not have to worry about it that contains is struck out. I will tell you why next

Please find the small window is saying that by calling the contains method of the text variable you effectively call the contains method of the org.codehause.groovy.runtime.StringGroovyMethods class. This is a trick done by Groovy language.

KatalonDiscussion10517_1.png

1 Like

Have a look at the Groovy lang API document:

The contains(java.lang.String self, java.lang.String text) method is , Yes, Deprecated. In this sense, IDE is working right. Striking out contains is grammatically correct. Though I find it unnecessary and a bit annoying.

The document says “Use the CharSequence version”. The contains(java.lang.CharSequence self, java.lang.CharSequence text) is not deprecated, will be avaialbe for long term use.

The java.lang.String class implements the java.lang.CharSequence interface. Therefore a Groovy statement:

         text.contains("World!")

will stay valid in future.

You can safely ignore it if contains is struck out by IDE.

KatalonDiscussion10517_2.png

3 Likes

Thank you it worked. :slight_smile:

You can rewrite this:

String text = "Hello, World"
println "text contains it: " + text.contains("World")

to the following:

CharSequence text2 = "Hello, World"
println "text2 contains it: " + text2.contains("World")

or to this:

String text2 = "Hello, World"
println "text2 contains it: " + ((CharSequence)text2).contains("World")

then you would be able to avoid contains being struck-out.

Hi
It’s depreciated but it’s a bug of eclipse. So don’t worry.
You can refer this.
https://stackoverflow.com/questions/38145451/why-are-methods-like-center-contains-deprecated-on-strings.

Regards
kavya

When I use the “contains” function, it returns error below:

import java.lang.CharSequence as CharSequence
CharSequence CurrentDay = Mobile.getAttribute(findTestObject(‘CurrentDate’), ‘label’, 1)
Mobile.verifyElementText(findTestObject(‘CurrentDate’), CurrentDay.contains(“Wednesday”))

Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords.verifyElementText() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.lang.Boolean) values: [TestObject - ‘Object Repository/CurrentDay_Monday’, true]
Possible solutions: verifyElementText(com.kms.katalon.core.testobject.TestObject, java.lang.String), verifyElementText(com.kms.katalon.core.testobject.TestObject, java.lang.String, com.kms.katalon.core.model.FailureHandling), verifyElementExist(com.kms.katalon.core.testobject.TestObject, int)
at PositionalCleaning.run(PositionalCleaning:26)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1571985881764.run(TempTestCase1571985881764.groovy:21)