I have created and compiled the following Java classes:
A.java:
public class A {
public static void hello(String message) {
System.err.println("Hello "+message);
}
}
B.java
public class B extends A {
public static void hello(String message) {
System.err.println("Ola "+message);
}
}
I also have a Java test program that calls the static hello method of the B class:
public class Test {
public static void main(String[] args) {
B.hello("world");
}
}
When I compile and run the Test class in the java environment, the result is
"Ola world"
If I issue the following in the Beanshell interpreter
B.hello("world");
the result is
"Hello world"
It looks like the interpreter is looking up the static method from the superclass, and not doing the same thing that Java would do in this case.
Logged In: YES
user_id=812261
Originator: NO
It looks like the problem is that the algorithm that find the most specific match only looks at the arguments to the method call, and ignores sorting on the most specific class. It compares each candidate method call, and take the most recently 'best matched' call. The list was being prepared from the base class to the super class, so super class entries were compared later and were preferred. A simple fix is to reverse the order of the list with super class methods first, and base class methods later. Here is a patch that does that:
<pre>
*** BeanShell-2.0b5\src\bsh\Reflect.java Tue Sep 25 16:24:34 2007
--- Copy of BeanShell-2.0b5\src\bsh\Reflect.java Fri Jun 10 20:20:52 2005
***************
*** 534,545 ****
if ( candidates == null )
candidates = new Vector();
- // Do we have a superclass? (interfaces don't, etc.)
- Class superclass = baseClass.getSuperclass();
- if ( superclass != null )
- gatherMethodsRecursive( superclass,
- methodName, numArgs, publicOnly, candidates );
-
// Add methods of the current class to the vector.
// In public case be careful to only add methods from a public class
// and to use getMethods() instead of getDeclaredMethods()
--- 534,539 ----
***************
*** 558,563 ****
--- 552,563 ----
gatherMethodsRecursive( intfs[i],
methodName, numArgs, publicOnly, candidates );
+ // Do we have a superclass? (interfaces don't, etc.)
+ Class superclass = baseClass.getSuperclass();
+ if ( superclass != null )
+ gatherMethodsRecursive( superclass,
+ methodName, numArgs, publicOnly, candidates );
+
return candidates;
}
</pre>
Logged In: YES
user_id=406180
Originator: NO
fixed in Beanshell2 fork
http://code.google.com/p/beanshell2/issues/detail?id=18
Ticket has been migrated to github.
Please follow up on this over here: https://github.com/beanshell/beanshell/issues/437