zoukankan      html  css  js  c++  java
  • Groovy In Action 笔记 (8) -- MOP

    /**
     * @author :ZackZhou
     * @date :Created in 2020/10/9 2:31 PM
     * @description :
     * All classes that are compiled by Groovy implement the GroovyObject interface
     * Again, you’re free to implement any such methods in your Groovy class to your liking.
     * If you don’t, then the Groovy compiler will insert a default implementation for you.
     * This default implementation is the same as if you’d inherit from GroovyObjectSupport,
     * which basically relays all calls to the metaclass.
     *
     * public abstract class GroovyObjectSupport implements GroovyObject {
     * public Object invokeMethod(String name, Object args) {
     * return getMetaClass().invokeMethod(this, name, args);
     * }
     * public Object getProperty(String property) {
     * return getMetaClass().getProperty(this, property);
     * }
     * public void setProperty(String property, Object newValue) {
     * getMetaClass().setProperty(this, property, newValue);* }
     * // more here...
     * }
     *
     * As soon as a class implements GroovyObject, the following rules apply:
     * ■ Every access to a property calls the getProperty() method.6
     * ■ Every modification of a property calls the setProperty() method.
     * ■ Every call to an unknown method calls invokeMethod(). If the method is known,
     * invokeMethod() is only called if the class implements GroovyObject and the
     * marker interface GroovyInterceptable.
     *
     *
     * @modified By:
     * @version:
     */
    
    // Groovy中提供的非JDK内置的方法,是通过  org.codehaus.groovy.runtime.DefaultGroovyMethods 这个类实现的
    String myStr = ""
    myStr.metaClass
    println(myStr)
    
    /*
    // MOP pseudo code
    def mopInvoke(Object obj, String method, Object args) {
        if (obj instanceof GroovyObject) {
            return groovyObjectInvoke(obj, method, args)
        }
        registry.getMetaClass(obj.class).invokeMethod(obj, method, args)
    }
    
    def groovyObjectInvoke(Object obj, String method, Object args){
        if (obj instanceof GroovyInterceptable) {
            return obj.metaClass.invokeMethod(method, args)
        }
        if (! obj.metaClass.respondsTo(method, args)) {
            return obj.metaClass.invokeMethod(method, args)
        }
        obj.metaClass.invokeMethod(obj, method, args)
    }
    
    // Default meta class pseudo code
    def invokeMethod(Object obj, String method, Object args) {
        if (obj.metaClass.respondsTo(method, args)) {
            return methodCall(obj, method, args)
        }
        if (methodMissingAvailable(obj)) {
            return obj.metaClass.methodMissing(method, args)
        }
        throw new MissingMethodException()
    }
    */

     

  • 相关阅读:
    Vagrant 扩大磁盘根目录
    阿里云 轻量应用服务器 vnc 远程桌面连接
    图片加水印C#源代码
    Asp.net网站Pdf加水印C#源代码
    [FAQ] uni-app 如何让页面不展示返回箭头图标
    [PHP] composer, PHP Fatal error: Allowed memory size of xx bytes exhausted
    [FE] uni-app 导航栏开发指南
    [FE] uni-app 动态改变 navigationBarTitleText 导航标题
    [FE] yarn, npm 切换镜像源
    [FAQ] Phpstorm 代码提示功能失效问题
  • 原文地址:https://www.cnblogs.com/FsharpZack/p/13787477.html
Copyright © 2011-2022 走看看