Groovy 2.4 - 绑定 - getProperty - 不依赖于try / catch块

时间:2017-11-17 05:30:03

标签: groovy

Groovy Binding类方法getProperty()依赖于try / catch块。我们观察到在高负载下CPU利用率很高,而APM工具表明这种方法对高CPU有贡献。

问题1 :如何实现不依赖于try / catch块的getProperty方法(与源代码中的注释完全相同)?

问题2 :在什么情况下“super.getProperty(property);”实际上返回一个值?示例代码将有所帮助。

来源:https://github.com/groovy/groovy-core/commit/b58342060031f517d6ba42a64dd3ddf392e820bb#diff-c5805e4bd65201710573d9582ebaf461

2.4.12中的版本

/**
 * Overloaded to make variables appear as bean properties or via the subscript operator
 */
public Object getProperty(String property) {
    /** @todo we should check if we have the property with the metaClass instead of try/catch  */
    try {
        return super.getProperty(property);
    }
    catch (MissingPropertyException e) {
        return getVariable(property);
    }
}

提案:此代码是否正确?

public Object getProperty(String property) {        
        if(getMetaClass().hasProperty(property)!=null){
            return super.getProperty(property);
        } else {
            return getVariable(property);
        }
}

更新1 该提案不起作用。 getMetaClass()。hasProperty(property)似乎依次(通过嵌套调用)调用getProperty()并且程序因StackOverflowError(循环调用)而失败

objc[50062]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
java.lang.StackOverflowError
    at org.codehaus.groovy.util.AbstractConcurrentMap.getOrPut(AbstractConcurrentMap.java:37)
    at com.webmd.cp.rule.service.CustomBinding.getProperty(CustomBinding.groovy:24)
    at com.webmd.cp.rule.service.CustomBinding.getVariable(CustomBinding.groovy:62)
    at com.webmd.cp.rule.service.CustomBinding.getProperty(CustomBinding.groovy:27)
    at com.webmd.cp.rule.service.CustomBinding.getVariable(CustomBinding.groovy:62)
    at com.webmd.cp.rule.service.CustomBinding.getProperty(CustomBinding.groovy:27)
    at com.webmd.cp.rule.service.CustomBinding.getVariable(CustomBinding.groovy:62)
    at com.webmd.cp.rule.service.CustomBinding.getProperty(CustomBinding.groovy:27)
    at com.webmd.cp.rule.service.CustomBinding.getVariable(CustomBinding.groovy:62)
    at com.webmd.cp.rule.service.CustomBinding.getProperty(CustomBinding.groovy:27)
    at com.webmd.cp.rule.service.CustomBinding.getVariable(CustomBinding.groovy:62)

0 个答案:

没有答案