groovy

static imports not working with groovy classes

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.1-beta-2
  • Fix Version/s: 1.1-rc-1
  • Component/s: groovy-jdk
  • Labels:
    None
  • Environment:
    red hat linux
  • Number of attachments :
    0

Description

Re: problem with static imports Click to flag this post

by Paul King Jul 12, 2007; 05:47pm :: Rate this Message: (use ratings to moderate[?])

Reply | Reply to Author | Show Only this Message

Looks like a bug.

If I follow the steps as you suggest I can replicate the error.
If I add "groovyc com/test1/Runner.groovy" then I can invoke
"groovy com/test1/Runner.groovy" with no problem.

Please raise an issue.

Paul.

Jason Weden wrote:
> Paul, thanks for your reply. That "static" keyword in the class definition
> shouldn't have been in there. Could you tell me what's different between
> your setup and mine? It's still isn't working for me:
>
> [nvw384@ma34jasper sandbox]$ groovy -version
> Groovy Version: 1.1-beta-2 JVM: 1.5.0_11-b03
>
> [nvw384@ma34jasper sandbox]$ less com/test1/Runner.groovy
>
> package com.test1
>
> import static com.test1.test2.Utils.*
>
> printMe('hello world');
>
> [nvw384@ma34jasper sandbox]$ less com/test1/test2/Utils.groovy
> package com.test1.test2
>
> class Utils {
> static printMe (aString) { > println "$aString" > }
> }
>
> [nvw384@ma34jasper sandbox]$ groovy com/test1/Runner.groovy
> Caught: groovy.lang.MissingMethodException: No signature of method:
> com.test1.Runner.printMe() is applicable for argument types:
> (java.lang.String) values: {"hello world"}
> at com.test1.Runner.run(Runner.groovy:6)
> at com.test1.Runner.main(Runner.groovy)
>
>
>
>

Activity

Hide
blackdrag blackdrag added a comment -

ok, after looking into this I found the reason... at the point the ResolveVisitor finds the static import the class is not yet parsed. Which means finding the static method must be done at a later step than it is currently done.

Show
blackdrag blackdrag added a comment - ok, after looking into this I found the reason... at the point the ResolveVisitor finds the static import the class is not yet parsed. Which means finding the static method must be done at a later step than it is currently done.
Hide
blackdrag blackdrag added a comment -

I splitted ResolveVisitor inot the normal resolving process and a StaticImportVisitor that is doing only static imports. The new class works after ResolveVisitor was run and should then no longer have a problem with not yet parsed classes.

Show
blackdrag blackdrag added a comment - I splitted ResolveVisitor inot the normal resolving process and a StaticImportVisitor that is doing only static imports. The new class works after ResolveVisitor was run and should then no longer have a problem with not yet parsed classes.
Hide
Jason Weden added a comment -

I'm using snapshot build 447 and this still isn't working for me:

[nvw384@ma34jasper sandbox]$ groovy -version
Groovy Version: 1.1-beta-2 JVM: 1.5.0_11-b03
[nvw384@ma34jasper sandbox]$

nvw384@ma34jasper sandbox]$ echo $GROOVY_HOME
/opt/groovy-1.1-beta-3-SNAPSHOT
[nvw384@ma34jasper sandbox]$ groovy test1/mytest.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: test1.mytest.printMe() is applicable for argument types: () values: {}
at test1.mytest.run(mytest.groovy:5)
at test1.mytest.main(mytest.groovy)

[nvw384@ma34jasper sandbox]$ less test1/mytest.groovy
package test1

import static test1.test2.Utils.*

printMe();

[nvw384@ma34jasper sandbox]$ less test1/test2/Utils.groovy
package test1.test2

class Utils {
def static printMe () { println "hi" }
}

Show
Jason Weden added a comment - I'm using snapshot build 447 and this still isn't working for me: [nvw384@ma34jasper sandbox]$ groovy -version Groovy Version: 1.1-beta-2 JVM: 1.5.0_11-b03 [nvw384@ma34jasper sandbox]$ nvw384@ma34jasper sandbox]$ echo $GROOVY_HOME /opt/groovy-1.1-beta-3-SNAPSHOT [nvw384@ma34jasper sandbox]$ groovy test1/mytest.groovy Caught: groovy.lang.MissingMethodException: No signature of method: test1.mytest.printMe() is applicable for argument types: () values: {} at test1.mytest.run(mytest.groovy:5) at test1.mytest.main(mytest.groovy) [nvw384@ma34jasper sandbox]$ less test1/mytest.groovy package test1 import static test1.test2.Utils.* printMe(); [nvw384@ma34jasper sandbox]$ less test1/test2/Utils.groovy package test1.test2 class Utils { def static printMe () { println "hi" } }
Hide
Paul King added a comment -

I did a quick jad of code generated using groovyc. For this code:

import static java.lang.Math.*
println cos(2*PI)

beta-2 generates:

...
            ScriptBytecodeAdapter.invokeStaticMethodN(class1, java.lang.Math.class, "cos", new Object[] {
                ScriptBytecodeAdapter.invokeMethodN(class1, new Integer(2), "multiply", new Object[] {
                    ScriptBytecodeAdapter.getProperty(class1, java.lang.Math.class, "PI")
                })
            })
...

beta-3-SNAPSHOT generates:

...
            ScriptBytecodeAdapter.invokeMethodOnCurrentN(class1, (GroovyObject)ScriptBytecodeAdapter.castToType(this, groovy.lang.GroovyObject.class), "cos", new Object[] {
                ScriptBytecodeAdapter.invokeMethodN(class1, new Integer(2), "multiply", new Object[] {
                    ScriptBytecodeAdapter.getGroovyObjectProperty(class1, this, "PI")
                })
            })
...

The static import visitor seems to be doing the right thing but that isn't reflected in the generated code.

Show
Paul King added a comment - I did a quick jad of code generated using groovyc. For this code:
import static java.lang.Math.*
println cos(2*PI)
beta-2 generates:
...
            ScriptBytecodeAdapter.invokeStaticMethodN(class1, java.lang.Math.class, "cos", new Object[] {
                ScriptBytecodeAdapter.invokeMethodN(class1, new Integer(2), "multiply", new Object[] {
                    ScriptBytecodeAdapter.getProperty(class1, java.lang.Math.class, "PI")
                })
            })
...
beta-3-SNAPSHOT generates:
...
            ScriptBytecodeAdapter.invokeMethodOnCurrentN(class1, (GroovyObject)ScriptBytecodeAdapter.castToType(this, groovy.lang.GroovyObject.class), "cos", new Object[] {
                ScriptBytecodeAdapter.invokeMethodN(class1, new Integer(2), "multiply", new Object[] {
                    ScriptBytecodeAdapter.getGroovyObjectProperty(class1, this, "PI")
                })
            })
...
The static import visitor seems to be doing the right thing but that isn't reflected in the generated code.
Hide
Paul King added a comment -

Can you try again with HEAD. GROOVY-2031 may have fixed this case also.

Show
Paul King added a comment - Can you try again with HEAD. GROOVY-2031 may have fixed this case also.
Hide
Paul King added a comment -

I'd like to close this issue. Is it still causing you a problem with beta-3? It works for me here.

Show
Paul King added a comment - I'd like to close this issue. Is it still causing you a problem with beta-3? It works for me here.
Hide
Guillaume Laforge added a comment -

If you can't reproduce it with beta-3, you can close it.

Show
Guillaume Laforge added a comment - If you can't reproduce it with beta-3, you can close it.
Hide
Jason Weden added a comment -

Thanks for the nudge. Sorry, I've been so busy. Using the same example code listed in my previous comment, I can see that this is fixed. And.....THANK YOU.

[nvw384@ma34jasper 1978]$ groovy -version
Groovy Version: 1.1-beta-2 JVM: 1.5.0_11-b03

[nvw384@ma34jasper 1978]$ groovy test1/mytest.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: test1.mytest.printMe() is applicable for argument types: () values: {}
at test1.mytest.run(mytest.groovy:5)
at test1.mytest.main(mytest.groovy)

[nvw384@ma34jasper 1978]$ /opt/groovy-1.1-beta-3/bin/groovy test1/mytest.groovy
hi

Show
Jason Weden added a comment - Thanks for the nudge. Sorry, I've been so busy. Using the same example code listed in my previous comment, I can see that this is fixed. And.....THANK YOU. [nvw384@ma34jasper 1978]$ groovy -version Groovy Version: 1.1-beta-2 JVM: 1.5.0_11-b03 [nvw384@ma34jasper 1978]$ groovy test1/mytest.groovy Caught: groovy.lang.MissingMethodException: No signature of method: test1.mytest.printMe() is applicable for argument types: () values: {} at test1.mytest.run(mytest.groovy:5) at test1.mytest.main(mytest.groovy) [nvw384@ma34jasper 1978]$ /opt/groovy-1.1-beta-3/bin/groovy test1/mytest.groovy hi

People

Vote (2)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: