groovyjenkins-groovygroovy-eclipse

Groovy compiling despite error in function arguments


Is this legal in Groovy?

class RequestContext {
    def static requestContext

    def static setRequestContext(rc) {
        requestContext = rc
    }
}

Given the above I expect the following to fail at compile time using the groovy-eclipse-compiler:

RequestContext.setRequestContext()

Yet this passes and I'm trying to get this to fail at mvn compile time.


Solution

  • It can't fail at compile time, because you might add that method dynamically at runtime via the metaclass, ie:

    class Test {
    }
    
    Test.metaClass.static.woo = { -> println "yay" }
    
    Test.woo() // prints 'yay'
    

    To fail at compile time, you'd need to annotate the calling class with @CompileStatic or @TypeChecked