问题描述
我有以下Groovy类:
I have the following Groovy class:
@Slf4j
class WidgetService {
WidgetDao widgetDao = new WidgetDao()
createWidget(String name, int type) {
try {
widgetDao.createWidget(name, type)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
Widget getWidgetById(Long id) {
try {
widgetDao.getWidgetById(id)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
Widget getWidgetByName(String name) {
try {
widgetDao.getWidgetByName(name)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
def deleteWidget(Widget w) {
try {
widgetDao.deleteWidget(w)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
...dozens of more methods with *exact* same catch block
}
正如你所看到的,我在try-catch块中有很多重复的样板代码。如果我可以定义一个闭包或某种基于AOP的处理程序只是将 widgetDao
方法传递给closure /处理程序作为一个lambda或东西类似:
As you can see I have a lot of duplicate, boilerplate code in my try-catch blocks. It would be nice if I could define a closure or some sort of AOP-based handler just pass the widgetDao
method-of-interest into the closure/handler as a lambda or something similar:
def createWidgetClosure = { it =>
widgetDao.createWidget(it.name, it.type)
}
def getWidgetByIdClosure = { it =>
widgetDao.getWidgetById(it.id)
}
def tryCatchClosure = { closure =>
try {
closure()
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
这样我的`WidgetService可以看起来像这样:
So that my `WidgetService could then look something like this:
@Slf4j
class WidgetService {
WidgetDao widgetDao = new WidgetDao()
createWidget(String name, int type) {
tryCatchClosure(createWidgetClosure())
}
Widget getWidgetById(Long id) {
tryCatchClosure(getWidgetByIdClosure())
}
...dozens of more methods with *exact* same catch block
}
这可能吗?如果是,如何?
Is this possible? If so, how?
推荐答案
您可以使用 tryCatchClosure
你现在有什么。您甚至可以使 tryCatchClosure
一个以 Closure
为参数的方法。
You can simply do as below by using tryCatchClosure
what you have now. You can even make tryCatchClosure
a method which takes Closure
as a parameter.
class WidgetService {
WidgetDao widgetDao = new WidgetDao()
def tryCatchClosure(Closure closure) {
try {
closure()
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
createWidget(String name, int type) {
tryCatchClosure {
widgetDao.createWidget(name, type)
}
}
Widget getWidgetById(Long id) {
tryCatchClosure {
widgetDao.getWidgetById(id)
}
}
Widget getWidgetByName(String name) {
tryCatchClosure {
widgetDao.getWidgetByName(name)
}
}
def deleteWidget(Widget w) {
tryCatchClosure {
widgetDao.deleteWidget(w)
}
}
// ...dozens of more methods with *exact* same catch block
}
通过重写 invokeMethod
方法对其metaClass和处理异常(try / catch),从而在 WidgetDao
。
Or you can also intercept each method call on WidgetDao
by overriding invokeMethod
method on its metaClass and handle exception (try/catch). Similar to this.
这篇关于重复的try-catch块与Groovy'闭合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!