How to create a wrapper for a large interface

对着背影说爱祢 提交于 2019-12-11 14:15:24

问题


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.


回答1:


Have you tried overriding invokeMethod for the interface?

YourInterface.metaClass.invokeMethod = {String name, args ->
   def result
   println "Calling method $name"
   try{
      result = metaClass.getMetaMethod(name, args).invoke(delegate, args)
   }catch(YourException | AnyOtherException | Exception e){
       println "Handling exception for method $name"
       result = //Call retry code here
   }
   println "Called method $name"

   result
}

Overriding invokeMethod works as as interceptor for all the method calls in the interface. Handle the exception for each method and return the success result.




回答2:


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
}


来源:https://stackoverflow.com/questions/17438544/how-to-create-a-wrapper-for-a-large-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!