Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: 1.5.1
-
Fix Version/s: 1.5.1
-
Component/s: None
-
Labels:None
-
Testcase included:yes
-
Number of attachments :
Description
I am using groovy with java. When i used GString in groovy and returned GString to java code or passed this string in a collection and returned this collection to java, this GString passes as GString object instead of String object. This sometimes causes some problems in Java if casting to String is used in Java. Can you convert GString values to real String while returning them back to Java?
A sample usage resulting calsscastexception can be as:
Assume that I want to execute following groovy code from java
Groovy Code
def name = "Mustafa"; def listOfProperties = []; listOfProperties += "My name is ${name}"; return listOfProperties;
In java i use this list as
Java Code
List properties = //get returned properties from groovy for (Iterator iterator = properties.iterator(); iterator.hasNext();) { String prop = (String) iterator.next(); . . . }
Above code will throw ClassCastException.
a GString is kind of a String, but it is no sublcass of String. String is final, so this option is impossible. This also means that a GString is not a String in the sense of OOP, and especially not in the sense of Java's class system. so a cast will fail. On the other hand we can not know if the user will require a String or GString in his list, leaving the problem for him to resolve. So you have two options:
1.) change the groovy code:
listOfProperties += "My name is $
{name}".toString()
2.) change the Java code:
String prop = iterator.next().toString();
but it is nothing we can easily "fix". Maybe a different syntax would solve this case... but something like that requires extensive discussion on the lists and will not go into Groovy before 2.0.. It is possibly a major breaking change.
Anyway, I am closing this issue for now. If you want to go the syntax based route, then make a suggestion on the list and/or fill a new jira issue.