当前位置:网站首页>[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
边栏推荐
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
- U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
- Leetcode simple question: check whether each row and column contain all integers
- Daily question brushing record (XIV)
- Basic grammar of interview (Part 1)
- Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
- Metaverse Ape上线倒计时,推荐活动火爆进行
- Blocking protocol for concurrency control
- A long's perception
- Concurrency control of performance tuning methodology
猜你喜欢

Damn, window in ie open()

The real situation of programmers

Web3为互联网带来了哪些改变?

Installation of VMware Workstation

Blocking of concurrency control

Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference

Oracle triggers

"Chris Richardson microservices series" uses API gateway to build microservices

Daily question brushing record (XIV)
![[Yugong series] go teaching course 003-ide installation and basic use in July 2022](/img/9d/7d01bc1daa61f6545f619b6746f8bb.png)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
随机推荐
Sentinel production environment practice (I)
How to add new fields to mongodb with code (all)
Unique occurrence times of leetcode simple questions
Official clarification statement of Jihu company
[Yugong series] go teaching course in July 2022 004 go code Notes
Leetcode simple question: the minimum cost of buying candy at a discount
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
How to quickly experience oneos
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
[Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
Index optimization of performance tuning methodology
Platform bus
Learning of mall permission module
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
Damn, window in ie open()
How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
Oracle views the data size of a table
A substring with a length of three and different characters in the leetcode simple question
Server optimization of performance tuning methodology
Practice: fabric user certificate revocation operation process