Java 方法和构造函数<&燃气轮机;两者都从成员继承,都具有getExceptionTypes()方法。在这种情况下,如何避免代码重复?

Java 方法和构造函数<&燃气轮机;两者都从成员继承,都具有getExceptionTypes()方法。在这种情况下,如何避免代码重复?,java,oop,refactoring,code-duplication,Java,Oop,Refactoring,Code Duplication,我的代码库中有这两种方法,我希望以某种方式合并它们以避免代码重复: protected IJavaType[] getExceptionTypes(Method method) { Class<?>[] declaredExceptions = method.getExceptionTypes(); IJavaType[] exceptions = new IJavaType[declaredExceptions.length]; for (int i =

我的代码库中有这两种方法,我希望以某种方式合并它们以避免代码重复:

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;
}
受保护的IJavaType[]getExceptionTypes(方法){
类[]declaredExceptions=method.getExceptionTypes();
IJavaType[]异常=新的IJavaType[declaredExceptions.length];
for(int i=0;i
是否有任何方法可以排除代码重复(除了使用模板模式的子类化)?

您可以非常轻松地提取大部分代码:

protected IJavaType[] getExceptionTypes(Method method) {
    return getExceptionTypesImpl(method.getExceptionTypes());
}

protected IJavaType[] getExceptionTypes(Constructor<?> c) {
    return getExceptionTypesImpl(c.getExceptionTypes());    
}

private void getExceptionTypesImpl(Class<?>[] declaredExceptions) {
    IJavaType[] exceptions = new IJavaType[declaredExceptions.length];

    for (int i = 0; i < declaredExceptions.length; i++) {
        exceptions[i] = getType(declaredExceptions[i]);
    }

    return exceptions;
}
受保护的IJavaType[]getExceptionTypes(方法){
返回getExceptionTypeSimple(方法.getExceptionTypes());
}
受保护的IJavaType[]getExceptionTypes(构造函数c){
返回getExceptionTypeSimple(c.getExceptionTypes());
}
私有void GetExceptionTypeSimple(类[]declaredExceptions){
IJavaType[]异常=新的IJavaType[declaredExceptions.length];
for(int i=0;i
您可以非常轻松地提取其中的大部分:

protected IJavaType[] getExceptionTypes(Method method) {
    return getExceptionTypesImpl(method.getExceptionTypes());
}

protected IJavaType[] getExceptionTypes(Constructor<?> c) {
    return getExceptionTypesImpl(c.getExceptionTypes());    
}

private void getExceptionTypesImpl(Class<?>[] declaredExceptions) {
    IJavaType[] exceptions = new IJavaType[declaredExceptions.length];

    for (int i = 0; i < declaredExceptions.length; i++) {
        exceptions[i] = getType(declaredExceptions[i]);
    }

    return exceptions;
}
受保护的IJavaType[]getExceptionTypes(方法){
返回getExceptionTypeSimple(方法.getExceptionTypes());
}
受保护的IJavaType[]getExceptionTypes(构造函数c){
返回getExceptionTypeSimple(c.getExceptionTypes());
}
私有void GetExceptionTypeSimple(类[]declaredExceptions){
IJavaType[]异常=新的IJavaType[declaredExceptions.length];
for(int i=0;i
简单地说:

private IJavaType[] getExceptionTypes(Class<?>[] declaredExceptions) {
    IJavaType[] exceptions = new IJavaType[declaredExceptions.length];

    for (int i = 0; i < declaredExceptions.length; i++) {
        exceptions[i] = getType(declaredExceptions[i]);
    }

    return exceptions;
}

protected IJavaType[] getExceptionTypes(Method method) {
    return getExceptionTypes(method.getExceptionTypes());
}

protected IJavaType[] getExceptionTypes(Constructor<?> c) {
    return getExceptionTypes(c.getExceptionTypes());
}
private IJavaType[]getExceptionTypes(类[]declaredExceptions){
IJavaType[]异常=新的IJavaType[declaredExceptions.length];
for(int i=0;i
简单地说:

private IJavaType[] getExceptionTypes(Class<?>[] declaredExceptions) {
    IJavaType[] exceptions = new IJavaType[declaredExceptions.length];

    for (int i = 0; i < declaredExceptions.length; i++) {
        exceptions[i] = getType(declaredExceptions[i]);
    }

    return exceptions;
}

protected IJavaType[] getExceptionTypes(Method method) {
    return getExceptionTypes(method.getExceptionTypes());
}

protected IJavaType[] getExceptionTypes(Constructor<?> c) {
    return getExceptionTypes(c.getExceptionTypes());
}
private IJavaType[]getExceptionTypes(类[]declaredExceptions){
IJavaType[]异常=新的IJavaType[declaredExceptions.length];
for(int i=0;i