Groovy中propertyMissing方法的静态版本是什么?
好的-试过看/读,但不确定我是否有答案 我有一个实用程序类,它在内部包装了一个静态ConcurrentLinkedQueue 实用程序类本身添加了一些静态方法——我不希望调用new来创建实用程序的实例 我想截获实用程序类的getProperty调用,并在类定义中内部实现这些调用 在我使用它之前,我可以通过向实用程序类元类添加以下内容来实现这一点Groovy中propertyMissing方法的静态版本是什么?,groovy,interception,mop,Groovy,Interception,Mop,好的-试过看/读,但不确定我是否有答案 我有一个实用程序类,它在内部包装了一个静态ConcurrentLinkedQueue 实用程序类本身添加了一些静态方法——我不希望调用new来创建实用程序的实例 我想截获实用程序类的getProperty调用,并在类定义中内部实现这些调用 在我使用它之前,我可以通过向实用程序类元类添加以下内容来实现这一点 UnitOfMeasure.metaClass.static.propertyMissing = {name -> println "access
UnitOfMeasure.metaClass.static.propertyMissing = {name -> println "accessed prop called $name"}
println UnitOfMeasure.'Each'
然而,我想做的是在类定义本身中声明拦截。我在类定义中尝试了这一点,但它似乎从未被调用
static def propertyMissing (receiver, String propName) {
println "prop $propName, saught"
}
我也试过了
静态def getProperty(字符串属性){println“accessed$prop”}
但这也不是所谓的
因此,除了在使用之前在代码/脚本中添加元类之外,如何在希望捕获属性访问的实用程序类中声明
目前我的实际班级是这样的
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]
}
}
哪些错误是这样的
[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)
propertyMissing
方法的静态版本称为$Static\u propertyMissing
:
static def $static_propertyMissing(String name) {
// do something
}
此方法由以下程序调用:
例如:
class Hello {
static def $static_propertyMissing(String name) {
println "Hello, $name!"
}
}
Hello.World
输出:
Hello, World!
上帝啊,你不必爱我,绝对是聪明的。工作是一种享受,我可以从这里开始。显然,这不是一个常见的用例,否则它可能是某本书或其他文章。非常感谢!
class Hello {
static def $static_propertyMissing(String name) {
println "Hello, $name!"
}
}
Hello.World
Hello, World!