当前位置:网站首页>[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
边栏推荐
- 如何向mongoDB中添加新的字段附代码(全)
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- database mirroring
- 344. Reverse String. Sol
- The new content of the text component can be added through the tag_ Config set foreground and background colors
- 笔记本电脑蓝牙怎么用来连接耳机
- Basic grammar of interview (Part 1)
- How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
- Performance testing of software testing
- Livelocks and deadlocks of concurrency control
猜你喜欢

ICMP introduction
![Sparse array [matrix]](/img/62/27b02deeeaa5028a16219ef51ccf82.jpg)
Sparse array [matrix]

2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*

Metaverse Ape上线倒计时,推荐活动火爆进行

Reptile practice

Installation of VMware Workstation

Granularity of blocking of concurrency control

Interprocess communication in the "Chris Richardson microservice series" microservice architecture

Solutions for unexplained downtime of MySQL services

Talking about MySQL index
随机推荐
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
854. String BFS with similarity K
How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
CA certificate trampled pit
How to view Apache log4j 2 remote code execution vulnerability?
Blocking protocol for concurrency control
Type of fault
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
Shell script, awk uses if, for process control
Storage optimization of performance tuning methodology
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
MySQL服务莫名宕机的解决方案
Win11运行cmd提示“请求的操作需要提升”的解决方法
微服務鏈路風險分析
2022软件测试工程师涨薪攻略,3年如何达到30K
Sentinel production environment practice (I)
Kubernetes Administrator certification (CKA) exam notes (IV)
数据泄露怎么办?'华生·K'7招消灭安全威胁
Cobaltstrike builds an intranet tunnel