Domanda

Ho problemi a usare propertyMissing() insieme a GroovyShell

Ho i file

/**
 * @file FooScript.groovy
 */
abstract class FooScript extends Script {

    def propertyMissing(String name) {
        "This is the property '$name'"
    }

    def propertyMissing(String name, value) {
        println "You tried to set property '$name' to '$value'"
    }
}

e

/**
 * @file FooScriptTest.groovy
 */

import org.codehaus.groovy.control.*


def fooScript = """\
                foo = 'bar'
                println foo"""

def conf = new CompilerConfiguration()
conf.setScriptBaseClass("FooScript")
def sh = new GroovyShell(conf)

sh.evaluate fooScript

Quando corro FooScriptTest.groovy Mi aspetto l'output

Hai provato a impostare la proprietà "foo" su "bar"

Questa è la proprietà 'foo'

Quello che ottengo è:

sbarra

Sembra il mio propertyMissing() è ignorato da quello predefinito. Come prevenirlo?

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top