方法和构造函数?两者都继承自 Member,都有 getExceptionTypes() 方法。在这种情况下如何避免代码重复?
我的代码库中有这两种方法,我想以某种方式合并它们以避免代码重复:
protected IJavaType[] getExceptionTypes(Method method) {
Class<?>[] declaredExceptions = method.getExceptionTypes();
IJavaType[] exceptions = new IJavaType[declaredExceptions.length];
for (int i = 0; i < declaredExceptions.length; i++) {
exceptions[i] = getType(declaredExceptions[i]);
}
return exceptions;
}
protected IJavaType[] getExceptionTypes(Constructor<?> c) {
Class<?>[] declaredExceptions = c.getExceptionTypes();
IJavaType[] exceptions = new IJavaType[declaredExceptions.length];
for (int i = 0; i < declaredExceptions.length; i++) {
exceptions[i] = getType(declaredExceptions[i]);
}
return exceptions;
}
是否有任何方法可以排除代码重复(除了使用模板模式的子类化之外)?
I have both these methods in my code base, which I'd like to merge in some way to avoid code duplication:
protected IJavaType[] getExceptionTypes(Method method) {
Class<?>[] declaredExceptions = method.getExceptionTypes();
IJavaType[] exceptions = new IJavaType[declaredExceptions.length];
for (int i = 0; i < declaredExceptions.length; i++) {
exceptions[i] = getType(declaredExceptions[i]);
}
return exceptions;
}
protected IJavaType[] getExceptionTypes(Constructor<?> c) {
Class<?>[] declaredExceptions = c.getExceptionTypes();
IJavaType[] exceptions = new IJavaType[declaredExceptions.length];
for (int i = 0; i < declaredExceptions.length; i++) {
exceptions[i] = getType(declaredExceptions[i]);
}
return exceptions;
}
Is there any way to factor out the code repetition (other than using subclassing with template pattern)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单来说怎么样:
How about simply:
那么你可以相当容易地提取其中的大部分:
Well you can extract most of it fairly easily: