[groovy-user] Can't get extensions to work?

15 views
Skip to first unread message

OC

unread,
Jan 29, 2013, 11:33:54 AM1/29/13
to Groovy User
Hello there,

can you please check and tell me what am I doing wrong? An extension gets completely ignored -- I must be dumbly missing something pretty obvious :(

===========
144 /tmp> >StringExtension.groovy
package cz.ocs
class StringExtension {
static String reverseToUpperCase(String self) { // textbook example
StringBuilder sb = new StringBuilder(self)
sb.reverse()
sb.toString().toUpperCase()
}
}
145 /tmp> mkdir build
146 /tmp> groovyc -d build StringExtension.groovy
148 /tmp> mkdir build/META-INF/services
149 /tmp> >build/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
moduleName=OCSExtensions
moduleVersion=1.0
extensionClasses=cz.ocs.StringExtension
staticExtensionClasses=
150 /tmp> jar -cf OCGroovier.jar -C build .
154 /tmp> >test.groovy
println 'So...'
println 'Doesnt work, why?'.reverseToUpperCase()
155 /tmp> groovyc -cp OCGroovier.jar test.groovy
156 /tmp> java test
So...
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.lang.String.reverseToUpperCase() is applicable for argument types: () values: []
...
157 /tmp> groovy test
So...
Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.reverseToUpperCase() is applicable for argument types: () values: []
groovy.lang.MissingMethodException: No signature of method: java.lang.String.reverseToUpperCase() is applicable for argument types: () values: []
at test.run(test.groovy:2)
158 /tmp> groovy -version
Groovy Version: 2.1.0 JVM: 1.6.0_33 Vendor: Apple Inc. OS: Mac OS X
159 /tmp> groovyc -version
Groovy compiler version 2.1.0
Copyright 2003-2013 The Codehaus. http://groovy.codehaus.org/

160 /tmp> java -version
java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
161 /tmp> sw_vers
ProductName: Mac OS X
ProductVersion: 10.8
BuildVersion: 12A269
162 /tmp>
===========

Thanks a big lot!
OC


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Tim Yates

unread,
Jan 29, 2013, 11:46:32 AM1/29/13
to us...@groovy.codehaus.org
You seem to just include the jar when compiling...

Does it work if you put the jar on the classpath when you run the script?


OC

unread,
Jan 29, 2013, 11:58:12 AM1/29/13
to us...@groovy.codehaus.org
Tim,

On Jan 29, 2013, at 5:46 PM, Tim Yates <tim....@gmail.com> wrote:

> You seem to just include the jar when compiling...

Oh, I see -- a leftover from times my JAR contained only ASTs :)

> Does it work if you put the jar on the classpath when you run the script?

Alas, does not either. I've even added a direct usage to make sure the class indeed is available:

===
198 /tmp> <test.groovy
import cz.ocs.*

println 'So...'
println "we have the class all right: ${StringExtension.reverseToUpperCase('abcd')}"
println 'but doesnt work as an extension'.reverseToUpperCase()
199 /tmp> groovyc -cp "OCGroovier.jar:." test.groovy
200 /tmp> java -cp "OCGroovier.jar:." test
So...
we have the class all right: DCBA
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.lang.String.reverseToUpperCase() is applicable for argument types: () values: []
...
===

Any idea what might be the culprit?

Jochen Eddelbüttel

unread,
Jan 29, 2013, 1:41:54 PM1/29/13
to us...@groovy.codehaus.org
Hi OC,

have you followed the instructions regarding the configuration of globally
applied extensions via a META-INF/services/<have to look this up in the
docs> file included in the jar? Guillaume's description in his "What's new
in 2.0" was precise enough. My guess that the configuration kind of puts a
use () { } block around all code executed on that classpath and the
extensions really are just categories in new guise.

Cheers,

Jochen

-----Ursprüngliche Nachricht-----
Von: OC [mailto:o...@ocs.cz]
Gesendet: Dienstag, 29. Januar 2013 17:58
An: us...@groovy.codehaus.org
Betreff: Re: [groovy-user] Can't get extensions to work?

Jochen Eddelbüttel

unread,
Jan 29, 2013, 1:45:53 PM1/29/13
to us...@groovy.codehaus.org
Ooops,
you clearly wrote that you did. Well no problem here working with NetBeans
7.3 beta, except for a couple of things that expansions won't do that work
with ExpandoMetaClass.

Jochen

-----Ursprüngliche Nachricht-----
Von: Jochen Eddelbüttel [mailto:joc...@eddelbuettel.net]
Gesendet: Dienstag, 29. Januar 2013 19:42
An: 'us...@groovy.codehaus.org'
Betreff: AW: [groovy-user] Can't get extensions to work?

Hi OC,

have you followed the instructions regarding the configuration of globally
applied extensions via a META-INF/services/<have to look this up in the
docs> file included in the jar? Guillaume's description in his "What's new
in 2.0" was precise enough. My guess that the configuration kind of puts a
use () { } block around all code executed on that classpath and the
extensions really are just categories in new guise.

Cheers,

Jochen

-----Ursprüngliche Nachricht-----
Von: OC [mailto:o...@ocs.cz]
Gesendet: Dienstag, 29. Januar 2013 17:58
An: us...@groovy.codehaus.org
Betreff: Re: [groovy-user] Can't get extensions to work?

Cédric Champeau

unread,
Jan 30, 2013, 2:32:29 AM1/30/13
to us...@groovy.codehaus.org
mmm, reading your original instructions, it seems that your
StringExtension file is not in a cz/ocs directory (corresponding to the
package). May be the problem.

Le 29/01/2013 17:58, OC a �crit :
--
C�dric Champeau
SpringSource - A Division Of VMware
http://www.springsource.com/
http://twitter.com/CedricChampeau

Ondřej Čada

unread,
Jan 30, 2013, 7:12:43 AM1/30/13
to us...@groovy.codehaus.org
Cédric,

On Jan 30, 2013, at 8:32 AM, Cédric Champeau wrote:

> mmm, reading your original instructions, it seems that your StringExtension file is not in a cz/ocs directory (corresponding to the package). May be the problem.

Hope not -- there seems to be absolutely no reason to sort the sources in folders by package. Eclipse does it (and it's darn annoying), but the compiler does not care, I believe. What's important is that the .class-es are at the proper folders in the jar, and that the compiler makes sure of due to the "package" declaration.

Anyway, I have just downloaded Groovy 2.1 to another computer where there were no groovy at all previously, and it _does_ work all right there.

Which is quadruple weird.

For sweet world I can't find why it does not work on the first machine. I've added a Groovy version log to be sure no old groovy-all.jar gets into the mess; it's all right, we are in 2.1. It must be somewhere in the runtime -- I've tried the JAR from the "ok" machine on the "bad" one, did not work. I've tried the JAR from the "bad" machine on the "ok" one, works all right.

The only difference I can see is the Mac OS X and Java version?!?

I've prepared a simple script (attached below). I'd be grateful, if anyone here happens to have a Mac OS X 10.8, if he could try and let me know the result; also, I'd be grateful if any of you Groovy Gurus might see the culprit why this might not work on my 10.8 machine -- details of the sources below in the script :-O

Here are the results on my two computers:

202 /tmp> ./tst /Users/ocs/groovy-2.1.0/embeddable/groovy-all-2.1.0.jar
ProductName: Mac OS X
ProductVersion: 10.7.3
BuildVersion: 11D50d
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04-415-11M3646)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01-415, mixed mode)
- compiling StringExtension.groovy
- creating jar
- compiling test
- running
Groovy 2.1.0
direct IS OK
extension IS OK
- done
203 /tmp>

255 /tmp> ./tst /Users/ocs/Downloads/groovy-sdk-2.1.0/embeddable/groovy-all-2.1.0.jar
ProductName: Mac OS X
ProductVersion: 10.8
BuildVersion: 12A269
java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
- compiling StringExtension.groovy
- creating jar
- compiling test
- running
Groovy 2.1.0
direct IS OK
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.lang.String.reverseToUpperCase() is applicable for argument types: () values: []
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at test.run(test.groovy:4)
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.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1085)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:848)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:831)
at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:407)
at org.codehaus.groovy.runtime.InvokerHelper$runScript.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at test.main(test.groovy)
- done
256 /tmp>


Thanks a big lot for all the help!
OC

=== here's the script:

#!/bin/zsh
if [[ -z $1 ]] ; then
echo "Checks Groovy extensions. Usage:\n"
echo "$0 groovy-all-jar-path [testdirname]\n"
echo "The test dir, default 'test', will be created in /tmp"
echo "groovy/java bin must be at path, so that groovyc/java/jar can be run"
exit 0
fi
TD=${2-test}
if [[ -d /tmp/$TD ]] ; then
echo /tmp/$TD already exists, use another test dir name
exit 0
fi
sw_vers
java -version
mkdir -p /tmp/$TD/jardir/META-INF/services
pushd /tmp/$TD
echo "package cz.ocs\nclass StringExtension {\nstatic String reverseToUpperCase(String self) {\nStringBuilder sb = new StringBuilder(self)\nsb.reverse()\nsb.toString().toUpperCase()\n}\n}" > StringExtension.groovy
echo "- compiling StringExtension.groovy"
if groovyc -cp "$1" -d jardir StringExtension.groovy ; then
echo "moduleName=OCSExtensions\nmoduleVersion=1.0\nextensionClasses=cz.ocs.StringExtension\nstaticExtensionClasses=" > jardir/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
echo "- creating jar"
if jar -cf jar.jar -C jardir . ; then
echo "import cz.ocs.*\nprintln \"Groovy \${GroovySystem.getVersion()}\"\nprintln \"direct \${StringExtension.reverseToUpperCase('KO SI')}\"\nprintln \"extension \${'KO SI'.reverseToUpperCase()}\"" > test.groovy
echo "- compiling test"
if groovyc -cp "$1:jar.jar" test.groovy ; then
echo "- running"
java -cp "$1:jar.jar:." test
fi
fi
fi
popd
rm -rf /tmp/$TD
echo "- done"

Ondřej Čada

unread,
Jan 30, 2013, 11:44:29 AM1/30/13
to us...@groovy.codehaus.org
Hello there,

it gets curiouser and curiouser. I've found the culprit, but it does not make any sense to me:

- if the groovy-all JAR is in Java/Extensions, it DOES NOT work
- if it is not there (used just explicitly in classpath) it DOES work

Like this (the tst script quoted below for reference):

295 /tmp> rm ~/Library/Java/Extensions/groovy-all-2.1.0.jar
296 /tmp> ./tst /Users/ocs/Downloads/groovy-sdk-2.1.0/embeddable/groovy-all-2.1.0.jar
...
- running
Groovy 2.1.0
direct IS OK
extension IS OK
- done
297 /tmp> ln /Users/ocs/Downloads/groovy-sdk-2.1.0/embeddable/groovy-all-2.1.0.jar ~/Library/Java/Extensions
298 /tmp> ./tst /Users/ocs/Downloads/groovy-sdk-2.1.0/embeddable/groovy-all-2.1.0.jar
...
- running
Groovy 2.1.0
direct IS OK
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.lang.String.reverseToUpperCase() is applicable for argument types: () values: []
...
- done
299 /tmp>

I can repeat this consistently on both Mac OS X 10.8 and 10.7.

Can you repeat the behaviour, too? Does it make any sense to you? Can you see a way to fix the problem without a need to explicitly classpath groovy-all for all groovy applications, which is a thing I'd rather do without, if possible?

Thanks and all the best,
OC
Reply all
Reply to author
Forward
0 new messages