12.2Groovy使用MetaClass拦截方法
来源:互联网 发布:游戏神作 知乎 编辑:程序博客网 时间:2024/05/18 00:47
在要拦截类的外部,以闭包的形式,增加MetaClass的invokeMethod()方法,这种方式适合我们无权修改的源代码。
class Car{ // 不用实现GroovyInterceptable接口 def check(){System.out.println "check called..." } // 检查 def start(){System.out.println "start called..." } // 启动 def drive(){System.out.println "drive called..." } // 驾驶}// 在类Car的外面,以闭包的形式,添加(或者叫定义)invokeMethod()方法Car.metaClass.invokeMethod = {String name, args -> System.out.print("call to $name intecepted...") if(name!='check') { System.out.print("running filter...") Car.metaClass.getMetaMethod('check').invoke(delegate, null) // 是delegate非thisi } def validMethod = Car.metaClass.getMetaMethod(name, args) if (validMethod != null) { validMethod.invoke(delegate, args) } else { Car.metaClass.invokeMissingMethod(delegate, name, args) // 是invokeMissingMethod()非invokeMethod() }}car = new Car()car.start()car.drive()car.check()try { car.speed() // Car中不存在的方法} catch (Exception e) { println e}
运行结果:
call to start intecepted...running filter...check called...
start called...
call to drive intecepted...running filter...check called...
drive called...
call to check intecepted...check called...
call to speed intecepted...running filter...check called...
groovy.lang.MissingMethodException: No signature of method: Car.speed() is applicable for argument types: () values: []
Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), split(groovy.lang.Closure), check(), start(), grep()
上面拦截的是Groovy类,再看一个栗子,拦截Java类(POJO)Integer中的方法:
// 在Integer的MetaClass上添加invokeMethod()方法,拦截Integer上的方法。Integer.metaClass.invokeMethod = { String name, args -> System.out.println("Call to $name intercepted on $delegate...") def validMethod = Integer.metaClass.getMetaMethod(name, args) if (validMethod == null) { Integer.metaClass.invokeMissingMethod(delegate, name, args) }else { System.out.println("running pro-filter...") result = validMethod.invoke(delegate, args) System.out.println("running post-filter...") result }}println 5.floatValue()println 5.intValue()try { println 5.empty() // Integer中不存在的方法} catch (Exception e) { println e}
运行结果:
Call to floatValue intercepted on 5...
running pro-filter...
running post-filter...
5.0
Call to intValue intercepted on 5...
running pro-filter...
running post-filter...
5
Call to empty intercepted on 5...
groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.empty() is applicable for argument types: () values: []
Possible solutions: every(), every(groovy.lang.Closure), upto(java.lang.Number, groovy.lang.Closure), wait(), any(), next()
--------------------------------------------------------------------------------------------------
可以在Groovy类中同时提供invokeMethod()方法和methodMissing()方法,invokeMethod()方法会优先于methodMissing方法使用。
- 12.2Groovy使用MetaClass拦截方法
- 12.1Groovy使用GroovyInterceptable拦截方法
- groovy元编程 方法拦截 动态改变
- 当在程序中使用jasperreport加载模板时报class: groovy/runtime/metaclass/java/util/HashMapMetaCl
- Groovy基础——MetaClass详解
- Groovy方法拦截,注入,合成,委托和动态类
- 当在程序中使用jasperreport加载模板时报class: groovy/runtime/metaclass/java/util/HashMapMetaClass, method:
- 使用metaclass验证子类
- GROOVY 类拦截器
- Groovy探索之MOP 三 Class、MetaClass和ExpandoMetaClass
- 13.2Groovy使用ExpandoMetaClass注入方法
- python metaclass 和 __new__方法
- Groovy方法
- metaclass
- 当在程序中使用jasperreport加载模板时报class: groovy/runtime/metaclass/java/util/HashMapMetaClass, method: super$2$invokeMethod signature
- metaclass 在 ORM中的使用
- Groovy探索 使用集合方法,写出更加Groovy风格的代码
- Groovy探索 使用集合方法,写出更加Groovy风格的代码
- OpenCV实践之路——人脸识别之一数据收集和预处理
- 设计模式-概念(策略模式,观察者模式,装饰者模式)
- Android 7.0 LowMemoryKiller 初识
- 子窗口与弹出窗口的坑使用笔记
- 卷积神经网络基础
- 12.2Groovy使用MetaClass拦截方法
- Android 5.x新特性之-Palette类
- sql一对多查询最大值的列出来
- svn中的eclipse项目转为Android studio项目
- Thopos 7 v7.1 1CD(地形测量软件)
- Windows10 安装Ubuntu子系统,解决从应用商店下载慢的问题。
- 浏览器调试出错
- ThinkPHP 事务回滚!
- oracle 表被锁定如何解锁