美文网首页
Groovy:元编程(方法拦截)

Groovy:元编程(方法拦截)

作者: lv_mock | 来源:发表于2020-06-18 10:38 被阅读0次
class Animal {
    def color

    def run() {
        println 'animal run'
    }
}

def animal = new Animal(color:"blue")
animal.run()  //animal run
animal.invokeMethod('run',null)  //animal run

MetaMethod metaMethod = animal.metaClass.getMetaMethod('run',null)
metaMethod.invoke(animal,null)  //animal run
//输出
animal run
animal run
animal run
class Animal implements GroovyInterceptable{
    def color

    def run() {
        System.out.println 'animal run'
    }


    Object invokeMethod(String name,Object args) {
        System.out.println 'invokeMethod'
        if(metaClass.invokeMethod(this,'respondsTo',name,args)) {
            metaClass.invokeMethod(this,name,null)
        } else {
            System.out.println 'method not found'
        }
    }
}

def animal = new Animal(color:"blue")
animal.metaClass.invokeMethod(animal,'run',null)
animal.run()
animal.run1()
//输出
animal run
invokeMethod
animal run
invokeMethod
method not found
class Animal2 implements GroovyInterceptable{
   static Animal2 instance
    def isInit;
    static Animal2 getInstance() {
        if(null == instance) {
            synchronized (Animal2.class) {
                if(null == instance) {
                    instance = new Animal2();
                }
            }
        }
        return instance
    }

    def init() {
        isInit = true
    }

    def run() {
        System.out.println 'animal run'
    }


    Object invokeMethod(String name,Object args) {
        System.out.println 'invokeMethod'
        if(name != 'init') {
            if(!isInit) {
                System.out.println 'please init first'
                return
            }
        }
        if(metaClass.invokeMethod(this,'respondsTo',name,args)) {
            metaClass.invokeMethod(this,name,null)
        } else {
            System.out.println 'method not found'
        }
    }
}

Animal2.getInstance().run()
Animal2.getInstance().init()
Animal2.getInstance().run()
//输出
invokeMethod
please init first
invokeMethod
invokeMethod
animal run
invokeMethod
method not found
class Animal3{
   def run() {
       println 'animal run'
   }

}

//在单个对象上进行方法拦截
def animal = new Animal3()
animal.run()
animal.metaClass.run = {
    println 'run run'
}
animal.run()

new Animal3().run()
输出:
animal run
run run
animal run
class Animal3{
   def run() {
       println 'animal run'
   }

}

//在单个对象上进行方法拦截
def animal = new Animal3()
animal.run()
Animal3.metaClass.run = {
    println 'run run'
}
animal.run()

new Animal3().run()
输出:
animal run
animal run
run run
class Animal3{
    def run() {
        println 'animal run'
    }

}

def animal = new Animal3()
animal.metaClass.run = {
    println 'run run'
}
animal.metaClass.invokeMethod = {
    String name,Object args ->
        System.out.println "invoke"
        def method = delegate.metaClass.getMetaMethod(name,args)
        if(method) {
            method.invoke(delegate,args)
        }
}

animal.run()


String.metaClass.plus = {
    CharSequence i ->
        i
}
println("123" + "456")
output:
invoke
run run
456
class Animal4{

    def propertyMissing(String name) {
        return null
    }

    def propertyMissing(String name, def arg) {

    }

    def methodMissing(String name, def args) {
        println 'methodMissing'
        return 'run'
    }


    def run() {
        println 'run'
    }

}
def animal = new Animal4()

def scanner = new Scanner(System.in)
Thread.start {
    while (true) {
        def msg = scanner.nextLine()
        if(msg == "exit") {
            break
        }
        if(animal.respondsTo(msg)) {
            animal."$msg"()
        } else {
            def (name,body) = msg.split(":")
            animal.metaClass."$name" = {
                evaluate(body)
            }
        }
    }
}

相关文章

网友评论

      本文标题:Groovy:元编程(方法拦截)

      本文链接:https://www.haomeiwen.com/subject/ngpsxktx.html