0
votes

My groovy version is 2.1.7

here is my code:

def repeat(val){
    for(i = 0; i < 5; i++){
        println val;
    }
}
repeat("Hello World")

When i run it, some exception throw. It only happened when i call the given method with param. If it is a method without param, everything is OK.

Exception thrown 九月 29, 2013 5:38:49 下午 org.codehaus.groovy.runtime.StackTraceUtils sanitize

WARNING: Sanitizing stacktrace:

java.lang.ClassFormatError: Illegal class name "2$repeat" in class file 2$repeat

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:791)

at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.define(ClassLoaderForClassArtifacts.java:42)

at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:86)

at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:84)

at java.security.AccessController.doPrivileged(Native Method)

at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.defineClassAndGetConstructor(ClassLoaderForClassArtifacts.java:84)

at org.codehaus.groovy.runtime.callsite.CallSiteGenerator.compilePogoMethod(CallSiteGenerator.java:217)

at org.codehaus.groovy.reflection.CachedMethod.createPogoMetaMethodSite(CachedMethod.java:228)

at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.createCachedMethodSite(PogoMetaMethodSite.java:212)

at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.createPogoMetaMethodSite(PogoMetaMethodSite.java:188)

at groovy.lang.MetaClassImpl.createPogoCallCurrentSite(MetaClassImpl.java:3121)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallCurrentSite(CallSiteArray.java:108)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)

at 2.run(2.groovy:6)

1
How do you run your code? I've tested this and it works fine (with/without param). I Think you have some sort of configuration or runtime problem. The code is fine - zaerymoghaddam
This looks like a problem with class generation. That stacktrace sure is weird. Are you using... jdk8, maybe? What is your java -version? What is your groovy filename? - Will
As you have your groovy code in a file called 2.groovy, Groovy compiles this to a class called 2. This is not a valid classname. However I cannot work out why it allows def repeat(){} or def repeat( String val ){}, but not def repeat( val ){} or def repeat( Object val ){} - tim_yates
This is a good question to ask the groovy-user mailing list - tim_yates

1 Answers

1
votes

I'm so stupid.I name this class to begin with a number.