当前位置:网站首页>[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
2022-07-05 22:17:00 【Programmer community】
List of articles
- One 、GroovyObject Interface profile
- Two 、MetaClass brief introduction
- 3、 ... and 、 Use GroovyObject#invokeMethod Execute class methods
One 、GroovyObject Interface profile
In class , If you don't realize a Method perhaps Member attribute , You can use Metaprogramming Replace in class Method or property ;
When compiling bytecode files , Every Groovy Classes will be compiled into GroovyObject Interface object ;
/** * be-all Groovy Classes must implement interfaces * <p> * stay Java Used in the world Groovy Objects are especially convenient . */public interface GroovyObject {
/** * Invokes the given method. * * @param name Method name to execute * @param args Method call method parameters * @return The return method executes the return value */ @Internal // Marked as internal just for backward compatibility , e.g. AbstractCallSite.createGroovyObjectGetPropertySite Will check the `isMarkedInternal` default Object invokeMethod(String name, Object args) {
return getMetaClass().invokeMethod(this, name, args); } /** * Retrieve attribute values . * * @param propertyName The attribute name * @return Return the found attribute value */ @Internal // Marked as internal just for backward compatibility , e.g. AbstractCallSite.createGroovyObjectGetPropertySite will check `isMarkedInternal` default Object getProperty(String propertyName) {
return getMetaClass().getProperty(this, propertyName); } /** * Set the given property to the new value . * * @param propertyName Set the property name of * @param newValue new property */ @Internal // Marked as internal just for backward compatibility , e.g. AbstractCallSite.createGroovyObjectGetPropertySite will check `isMarkedInternal` default void setProperty(String propertyName, Object newValue) {
getMetaClass().setProperty(this, propertyName, newValue); } /** * Returns the metaclass of a given class . * * @return Metaclass of this instance */ MetaClass getMetaClass(); /** * It is allowed to replace metaclasses with derived implementations . * * @param metaClass New metaclass */ void setMetaClass(MetaClass metaClass);}
Two 、MetaClass brief introduction
MetaClass Metaclasses can be used to implement Metaprogramming Of Methods to inject , Method intercept , Synthetic delegation Wait for the operation ;
/** * Groovy The metaclass in defines any given Groovy or Java Class . * Metaclass interface defines two parts . * client API, It's through ExtendeMetaObjectProtocol Interface * and Groovy The contract of the runtime system defines . * * Usually , The compiler and Groovy The runtime engine interacts with methods on this class , * The metaclass client and MetaObjectProtocol Interface defined method interaction * * @see MetaClassImpl * @see groovy.lang.MetaObjectProtocol */public interface MetaClass extends MetaObjectProtocol {
}
3、 ... and 、 Use GroovyObject#invokeMethod Execute class methods
Define a class , Define in it hello Method , You can call the method directly , It can also be done through GroovyObject#invokeMethod Execute the method ;
Code example :
class Student {
def name; def hello() {
println "Hello ${name}" }}def student = new Student(name: "Tom")// Call directly hello Method student.hello()// adopt GroovyObject#invokeMethod call hello Method // The second parameter is the function parameter , If void Then in nullstudent.invokeMethod("hello", null)
Execution results :
Hello TomHello Tom
边栏推荐
- Win11运行cmd提示“请求的操作需要提升”的解决方法
- 50. Pow(x, n). O(logN) Sol
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- Three "factions" in the metauniverse
- Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
- 90后测试员:“入职阿里,这一次,我决定不在跳槽了”
- Business learning of mall commodity module
- A substring with a length of three and different characters in the leetcode simple question
- Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
- What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
猜你喜欢
Index optimization of performance tuning methodology
Create a virtual machine on VMware (system not installed)
Damn, window in ie open()
Nacos installation and service registration
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
50. Pow(x, n). O(logN) Sol
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Leetcode simple question: find the nearest point with the same X or Y coordinate
What about data leakage? " Watson k'7 moves to eliminate security threats
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
随机推荐
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Cobaltstrike builds an intranet tunnel
FBO and RBO disappeared in webgpu
AD637 usage notes
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
Platformio create libopencm3 + FreeRTOS project
boundary IoU 的计算方式
700. Search in a Binary Search Tree. Sol
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
How to develop and introduce applet plug-ins
装饰器学习01
Installation of VMware Workstation
Character conversion PTA
A trip to Suzhou during the Dragon Boat Festival holiday
Kubernetes Administrator certification (CKA) exam notes (IV)
Dbeaver executes multiple insert into error processing at the same time
IIC bus realizes client device
Common interview questions of redis factory
What about data leakage? " Watson k'7 moves to eliminate security threats