如何在 Groovy 中拦截此构造函数调用?
在脚本中,方法接收 File 类型的参数,并将其发送到 File 的构造函数。 这会爆炸,因为 File 没有将另一个文件作为参数的构造函数。
如何拦截此调用,并将参数修改为 parameter.absolutePath
?
例如:
def x = new File("some_file")
...
def meth(def param) {
def y = new File(param) // if param is of type File, this blows up
// and I'd like groovy's intercepting capabilities to invoke this instead
// def y = new File(param.absolutePath)
}
如果不能做到这一点,我该如何添加这个构造函数:
File(File other) {
this(other.absolutePath)
}
In a script a method receives a parameter of type File, and sends it to the constructor of File. This blows up because File doesn't have a constructor that takes another file as a parameter.
How can I intercept this call, and modify the parameter to parameter.absolutePath
?
For example :
def x = new File("some_file")
...
def meth(def param) {
def y = new File(param) // if param is of type File, this blows up
// and I'd like groovy's intercepting capabilities to invoke this instead
// def y = new File(param.absolutePath)
}
If that can't be done, how could I add this constructor :
File(File other) {
this(other.absolutePath)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法在此处找到了答案。 这是使我上面写的代码起作用的代码:
I managed to find the answer here. Here's the code that makes what I wrote above work :