Daniel.Sun
2007-01-31 14:31:13 UTC
Hi all,
When I inspect Groovy, I found a issue that instance(whose class does not
implement plus method) + String will go wrong. For example:
class MyClass {
String toString() {
return "MyClass"
}
}
def mc = new MyClass()
println mc + " is alive"
If you execute above code, you will get following exception:
Exception thrown: groovy.lang.MissingMethodException: No signature of
method: MyClass.plus() is applicable for argument types: (java.lang.String)
values: {" is alive"}
groovy.lang.MissingMethodException: No signature of method: MyClass.plus()
is applicable for argument types: (java.lang.String) values: {" is alive"}
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:572)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at gjdk.groovy.lang.MetaClassImpl_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:119)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at MyClass.invokeMethod(Script1)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:136)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at Script1.run(Script1:7)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:484)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:425)
at gjdk.groovy.lang.GroovyShell_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:131)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy:503)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:97)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at groovy.lang.Closure.call(Closure.java:188)
at groovy.lang.Closure.call(Closure.java:183)
at groovy.lang.Closure.run(Closure.java:264)
at java.lang.Thread.run(Thread.java:619)
I think the following default implementation of plus method is much better
class MyClass {
String toString() {
return "MyClass"
}
// better default implementation
def plus(String s) {
return toString() + s
}
}
def mc = new MyClass()
println mc + " is alive"
If you execute the above code, you will get your expected result in Java:
MyClass is alive
Best regards,
Daniel.Sun
When I inspect Groovy, I found a issue that instance(whose class does not
implement plus method) + String will go wrong. For example:
class MyClass {
String toString() {
return "MyClass"
}
}
def mc = new MyClass()
println mc + " is alive"
If you execute above code, you will get following exception:
Exception thrown: groovy.lang.MissingMethodException: No signature of
method: MyClass.plus() is applicable for argument types: (java.lang.String)
values: {" is alive"}
groovy.lang.MissingMethodException: No signature of method: MyClass.plus()
is applicable for argument types: (java.lang.String) values: {" is alive"}
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:572)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at gjdk.groovy.lang.MetaClassImpl_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:119)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at MyClass.invokeMethod(Script1)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:136)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at Script1.run(Script1:7)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:484)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:425)
at gjdk.groovy.lang.GroovyShell_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:131)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy:503)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:97)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at groovy.lang.Closure.call(Closure.java:188)
at groovy.lang.Closure.call(Closure.java:183)
at groovy.lang.Closure.run(Closure.java:264)
at java.lang.Thread.run(Thread.java:619)
I think the following default implementation of plus method is much better
class MyClass {
String toString() {
return "MyClass"
}
// better default implementation
def plus(String s) {
return toString() + s
}
}
def mc = new MyClass()
println mc + " is alive"
If you execute the above code, you will get your expected result in Java:
MyClass is alive
Best regards,
Daniel.Sun
--
View this message in context: http://www.nabble.com/If-the-default-implementation-of-plus%28%29-is-%22toString%28%29-%2B-s%22%2C-groovy-will-be-more-harmony-with-Java.-tf3149011.html#a8729836
Sent from the groovy - jsr mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
View this message in context: http://www.nabble.com/If-the-default-implementation-of-plus%28%29-is-%22toString%28%29-%2B-s%22%2C-groovy-will-be-more-harmony-with-Java.-tf3149011.html#a8729836
Sent from the groovy - jsr mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email