当前位置:网站首页>[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
边栏推荐
- Recovery technology with checkpoints
- 了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
- Oracle views the data size of a table
- Win11运行cmd提示“请求的操作需要提升”的解决方法
- Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
- FBO and RBO disappeared in webgpu
- Type of fault
- How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
- Performance testing of software testing
- AD637 usage notes
猜你喜欢
[Yugong series] go teaching course in July 2022 004 go code Notes
Meituan dynamic thread pool practice ideas, open source
科技云报道:算力网络,还需跨越几道坎?
Server optimization of performance tuning methodology
K210 learning notes (IV) k210 runs multiple models at the same time
The real situation of programmers
Recovery technology with checkpoints
Reptile practice
Depth first DFS and breadth first BFS -- traversing adjacency tables
Two stage locking protocol for concurrency control
随机推荐
Draw a red lantern with MATLAB
Text组件新增内容通过tag_config设置前景色、背景色
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
"Chris Richardson microservices series" uses API gateway to build microservices
FBO and RBO disappeared in webgpu
How to quickly experience oneos
Concurrency control of performance tuning methodology
Nacos 的安装与服务的注册
Create a virtual machine on VMware (system not installed)
Two stage locking protocol for concurrency control
Type of fault
Overriding equals() & hashCode() in sub classes … considering super fields
119. Pascal‘s Triangle II. Sol
AD637使用笔记
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Technology cloud report: how many hurdles does the computing power network need to cross?
如何向mongoDB中添加新的字段附代码(全)
K210 learning notes (IV) k210 runs multiple models at the same time