Overriding an overloaded method causes an ArrayIndexOutOfBoundsException

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Overriding an overloaded method causes an ArrayIndexOutOfBoundsException

Jeffrey McGee
I get ArrayIndexOutOfBoundsException and other strange errors when I
try to override a method that was overloaded in a base class.

This code works just fine:
class GoodBase {
    public void paint(int step, Runnable g) { }
    public void paint(Runnable g) { }
public class GoodTest extends GoodBase {
    public void paint(Runnable g) { }

But when you put this code into BeanShell, you get an
class BadBase {
    public void paint(Runnable g, int step) { }
    public void paint(Runnable g) { }
public class BadTest extends BadBase {
    public void paint(Runnable g) { }

I believe the following patch to classContainsMethod() fixes the problem:
Index: ClassGeneratorUtil.java
--- ClassGeneratorUtil.java     (revision 41)
+++ ClassGeneratorUtil.java     (working copy)
@@ -710,6 +710,10 @@
                                        String [] methodParamTypes =
methods[i].getParameterTypes() );
+                                       if(methodParamTypes.length !=
+                                               continue;
                                        boolean found = true;
                                        for( int j=0;
j<methodParamTypes.length; j++)

Two methods are obviously different if they have a different number of
parameters so you can skip the for loop entirely.

Jeff McGee

Once upon a time, long, long ago, there lay in a valley far, far away
in the mountains the most contented kingdom the world has ever known.
It was called Happy Valley, and it was ruled over by a wise old king
called Otto. And all his subjects flourished and were happy, and there
were no discontents or grumblers, because wise King Otto had had them
all put to death.
 -- John Cleese of Monty Python

Beanshell-developers mailing list
[hidden email]