Groovy中propertyMissing方法的静态版本是什么?

时间:2018-08-19 19:21:37

标签: groovy interception mop

好-尝试看书/阅读,不确定是否对此有答案。

我有一个Utility类,该类在内部包装了一个静态ConcurrentLinkedQueue。

该实用工具类本身添加了一些静态方法-我不希望调用new来创建该实用工具的实例。

我想拦截getProperty调用实用程序类-并在类定义中内部实现

在使用它之前,我可以通过将以下内容添加到实用程序类元类中来实现此目的

UnitOfMeasure.metaClass.static.propertyMissing = {name -> println "accessed prop called $name"}

println UnitOfMeasure.'Each'

但是我想做的是在类定义本身中声明拦截。我在类定义中尝试了此方法-但它似乎从未被调用

static def propertyMissing (receiver, String propName) {
    println "prop $propName, saught"
}

我也尝试过

static def getProperty(String prop){println“ accessed $ prop”}

但是这个都没有。

除了在使用前在代码/脚本中添加metaClass之外,如何在实用程序类中声明要捕获属性访问的

我目前的实际班级

class UnitOfMeasure {
    static ConcurrentLinkedQueue UoMList = new ConcurrentLinkedQueue(["Each", "Per Month", "Days", "Months", "Years", "Hours", "Minutes", "Seconds" ])

    String uom

    UnitOfMeasure () {
        if (!UoMList.contains(this) )
            UoMList << this
    }

    static list () {
        UoMList.toArray()
    }

    static getAt (index) {
        def value = null
        if (index in 0..(UoMList.size() -1))
            value = UoMList[index]
        else if (index instanceof String) {
            Closure matchClosure = {it.toUpperCase().contains(index.toUpperCase())}
            def position = UoMList.findIndexOf (matchClosure)
            if (position != -1)
                value = UoMList[position]
        }
        value
    }

     static def propertyMissing (receiver, String propName) {
        println "prop $propName, saught"
    }


    //expects either a String or your own closure, with String will do case insensitive find
    static find (match) {
        Closure matchClosure
        if (match instanceof Closure)
            matchClosure = match
        if (match instanceof String) {
            matchClosure = {it.toUpperCase().contains(match.toUpperCase())}
        }
        def inlist = UoMList.find (matchClosure)
    }

    static findWithIndex (match) {
        Closure matchClosure
        if (match instanceof Closure)
            matchClosure = match
        else if (match instanceof String) {
            matchClosure = {it.toUpperCase().contains(match.toUpperCase())}
        }
        def position = UoMList.findIndexOf (matchClosure)
        position != -1  ? [UoMList[position], position] : ["Not In List", -1]
    }
}

我很欣赏为静态实用程序类而不是实例级属性拦截执行此操作并在类声明中执行此操作的秘密-而不是在进行调用之前添加到metaClass中。

只是为了您可以看到实际的类和调用的脚本-我在下面附上了这些内容

我调用类的脚本看起来像这样

println UnitOfMeasure.list()

def (uom, position) = UnitOfMeasure.findWithIndex ("Day")
println "$uom at postition $position"

// works UnitOfMeasure.metaClass.static.propertyMissing = {name -> println "accessed prop called $name"}
println UnitOfMeasure[4]
println UnitOfMeasure.'Per' 

这样的错误

[Each, Per Month, Days, Months, Years, Hours, Minutes, Seconds]
Days at postition 2
Years
Caught: groovy.lang.MissingPropertyException: No such property: Per for class: com.softwood.portfolio.UnitOfMeasure
Possible solutions: uom
groovy.lang.MissingPropertyException: No such property: Per for class: com.softwood.portfolio.UnitOfMeasure
Possible solutions: uom
    at com.softwood.scripts.UoMTest.run(UoMTest.groovy:12)

1 个答案:

答案 0 :(得分:3)

propertyMissing方法的静态版本称为$static_propertyMissing

static def $static_propertyMissing(String name) {
    // do something
}

此方法由MetaClassImpl at line 1002调用:

protected static final String STATIC_METHOD_MISSING = "$static_methodMissing";
protected static final String STATIC_PROPERTY_MISSING = "$static_propertyMissing";

// ...

protected Object invokeStaticMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) {
    MetaClass mc = instance instanceof Class ? registry.getMetaClass((Class) instance) : this;
    if (isGetter) {
        MetaMethod propertyMissing = mc.getMetaMethod(STATIC_PROPERTY_MISSING, GETTER_MISSING_ARGS);
        if (propertyMissing != null) {
            return propertyMissing.invoke(instance, new Object[]{propertyName});
        }
    } else {
        // .....
    }
    // ....
}

示例:

class Hello {
    static def $static_propertyMissing(String name) {
        println "Hello, $name!"
    }
}

Hello.World

输出:

Hello, World!