如何为大型界面创建包装 [英] How to create a wrapper for a large interface

查看:106
本文介绍了如何为大型界面创建包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包装器来捕获特定的异常,并在大型(100多种方法)接口中重试所有方法。我有重试代码,不用担心,但我无法弄清楚如何在不切割所有方法的情况下连接界面的实现。

我尝试使用缺少的方法处理程序,但这意味着我无法实现接口。摘要很明显,因为我无法实例化它。



我希望得到一个更好的解决方案,而不是将它作为模板创建,但我我愿意这样做。

解决方案

我尝试使用@ dmahapatro的示例,但不断收到IllegalArgumentException。我最终意识到,它只发生在mixin方法中(该方法显示mixin的签名)。

  errorProneInstance.metaClass.invokeMethod = {}我不需要调用()我需要使用doMethodInvoke()来获得适当的类型转换。字符串名称,参数 - > 
def结果

def method = delegate.metaClass.getMetaMethod(name,args)

while(true){
try {
result = method.doMethodInvoke(delegate,args)

break

} catch(AnnoyingIntermittentButRetryableException e){
print忽略异常
}


结果
}


I want to create a wrapper that traps a particular exception and retries for all methods in a large (100+ methods) interface. I have the retry code working no worries, but I can't figure out how to hook up an implementation of the interface without cut'n'paste into all the methods.

I tried to use a missing method handler but that meant that I couldn't have it implement the interface. Abstract is obviously out as I won't be able to instantiate it.

I'm hoping for a better solution than creating the class as a template on the fly but I'm willing to do that.

解决方案

I tried to use @dmahapatro's example but I kept getting IllegalArgumentException. I eventually realised that it only happened for mixin methods (the method shows the signature of the mixin). Instead of invoke() I needed to use doMethodInvoke() to get the appropriate type coersion.

errorProneInstance.metaClass.invokeMethod = { String name, args -> 
    def result

    def method = delegate.metaClass.getMetaMethod(name, args)

    while(true) {
        try {
            result = method.doMethodInvoke(delegate, args)

            break

        } catch (AnnoyingIntermittentButRetryableException e) {
            print "ignoring exception"
        }
    }

    result
}

这篇关于如何为大型界面创建包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆