Groovy call method name contains special character

I would like to call a method whose name contains a space in a static context, but it doesn't work. Any suggestions?

class Test2 { 
    void "test"() {
        "test a"()
    }

    void "test a"() { 
        println "test a" 
    } 

    public static void main(String[] args) { 
        def t = new Test2() 
        t."test"() //it works
        t."test a"()  //raise error,  Illegal class name "Test2$test a" in class file Test2$test a 
    } 
} 

G:\tmp\groovy\gp1\src>groovy -version 
Groovy Version: 2.3.2 JVM: 1.7.0_02 Vendor: Oracle Corporation OS: Windows 7 

G:\tmp\groovy\gp1\src>groovy Test2.groovy 
Test1.main 
Caught: java.lang.ClassFormatError: Illegal class name "Test2$test a" in class file Test2$test a 
java.lang.ClassFormatError: Illegal class name "Test2$test a" in class file Test2$test a 
        at Test2.main(Test2.groovy:15) 

      

+3


source to share


2 answers


How about invokeMethod

?



class Invokes { 

    def "test a"() { 
        "test a" 
    } 

    static main(args) { 
        def t = new Invokes() 
        assert t.invokeMethod("test a", null) == "test a" 
    } 
} 

      

+1


source


It will work like this:

class Test2 { 

    void "test a"() { 
        println "test a" 
    } 

    public static void main(String[] args) { 
        def t = new Test2() 
        def v = 'test a'
        t."$v"()
    } 
} 

      



As described here and here , but not sure why your example doesn't work. Don't work.

+1


source







All Articles