好的 - 尝试查看/阅读,但不确定我有答案。
我有一个实用程序类,它在内部包装了一个静态 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"}
但这也没有被称为。
因此,除了在使用之前在代码/脚本中添加到元类之外,如何在实用程序类中声明想要捕获属性访问的
我目前的实际类(class)看起来像这样
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]
}
}
我很欣赏为静态实用程序类而不是实例级属性拦截执行此操作的 secret ,并在类声明中执行此操作 - 而不是在调用之前添加到元类。
这样您就可以看到实际的类和调用的脚本 - 我已将这些附加在下面
我调用该类的脚本如下所示
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)
最佳答案
调用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!
关于groovy - Groovy 中 propertyMissing 方法的静态版本是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51921068/