I've noticed this behavior before as well.
What happens is that the instance method 'Object.toString()' is found, instead of the static 'Modifier.toString( arg )'. So Jython ends up calling 1.toString(), which prints '1'.
This incorrect method selection appears to be because of an error in org.python.core.ReflectedArgs.compareTo, which is used to sort the methods on a Java class proxy.
It looks like there have been a couple of attempts to solve this or similar issues in the 'matches' method, which ideally should share code with compareTo, but this means we're likely to break something. :-(
PyReflectedFunction.addArgs uses ReflectedArgs.compareTo to place overloaded methods in the correct search order. The relevant code is:
// Decision based on number of args
int n = args.length;
if (n < oargs.length) return -1;
if (n > oargs.length) return +1;
// Decide based on static/non-static
if (isStatic && !other.isStatic) return +1;
if (!isStatic && other.isStatic) return -1;
So static/non-static only affects the sort order if the methods have the same number of arguments, not including the implicit 'this'.
I think this needs to always add one to the argument list for non-static methods, and use that length:
// Decision based on number of args
int n = args.length;
int othern = oargs.length;
// Adjust for implicit 'this' of non-static methods
if ( !isStatic ) n += 1;
if ( !other.isStatic ) othern += 1;
if (n < othern) return -1;
if (n > othern) return +1;
Looks like the next chunk of code is broken as well (it only ends up looking at the last argument!). It would also need to be adjusted to handle the implicit 'this'.
// Compare the arg lists
int cmp = 0;
for (int i=0; i<n; i++) {
int tmp = compare(args[i], oargs[i]);
if (tmp == +2 || tmp == -2) cmp = tmp; //!!KB: should break
if (cmp == 0) cmp = tmp;//!!KB: should break?
}
if (cmp != 0) return cmp > 0 ? +1 : -1;
Seems like we might need to restructure ReflectedArgs...
kb
Jeff Lewis wrote:
>
> I hope this isn't a dumb newbie question.
> I was working with the java reflection classes, and trying to print out
> attributes of classes.
> However, when I used java.lang.reflect.Modifier.toString, I don't get
> what I expect.
> Instead I get:
>
> >>> java.lang.reflect.Modifier.toString( 1 )
> '1'
> >>>
>
> That method is static, and this should work.
> However, if I make an instance of Modifier, and then call toString
> I do get what I expect:
>
> >>> m = java.lang.reflect.Modifier()
> >>> m.toString( 1 )
> 'public'
>
> Here is the jython version info from when I start it up:
>
> Jython 2.1 on java1.3.1 (JIT: null)
>
> I am running on Windows NT, but using Sun's JDK1.3.1.
> What's up? Why doesn't the static toString() work?
>
> Thanks in advance,
> Jeff Lewis
> e-mail: jef...@lm...
>
> _______________________________________________
> Jython-dev mailing list
> Jyt...@li...
> https://lists.sourceforge.net/lists/listinfo/jython-dev
|