当前位置:网站首页>[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
边栏推荐
- Oracle triggers
- 如何快速体验OneOS
- How can Bluetooth in notebook computer be used to connect headphones
- Technology cloud report: how many hurdles does the computing power network need to cross?
- 854. String BFS with similarity K
- 极狐公司官方澄清声明
- How to use tensorflow2 for cat and dog classification and recognition
- Go语言学习教程(十五)
- Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
- 如何开发引入小程序插件
猜你喜欢

Oracle triggers

MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server

MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server

Kubernetes Administrator certification (CKA) exam notes (IV)

Common interview questions of redis factory

A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition

Concurrency control of performance tuning methodology

Recovery technology with checkpoints

Search: Future Vision (moving sword)

Storage optimization of performance tuning methodology
随机推荐
Metaverse Ape获Negentropy Capital种子轮融资350万美元
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
科技云报道:算力网络,还需跨越几道坎?
ICMP introduction
如何开发引入小程序插件
Leetcode simple question check whether all characters appear the same number of times
Win11运行cmd提示“请求的操作需要提升”的解决方法
Technology cloud report: how many hurdles does the computing power network need to cross?
笔记本电脑蓝牙怎么用来连接耳机
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
The new content of the text component can be added through the tag_ Config set foreground and background colors
Leetcode simple question: check whether each row and column contain all integers
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Cobaltstrike builds an intranet tunnel
The real situation of programmers
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
Leetcode simple question: find the nearest point with the same X or Y coordinate
装饰器学习01
Promql demo service
K210 learning notes (IV) k210 runs multiple models at the same time