|
From: <bc...@wo...> - 2001-01-25 19:15:15
|
[Will]
>I have run into some surprising behavior of Java Integer objects while using
>jython:
>
>public class Test {
>
> public void foo(Integer I) {
> System.out.println("Called with an Integer.");
> }
>
> public void foo(int i) {
> System.out.println("Called with an int.");
> }
>
> public static void main(String[] args) {
>
> ... initialize PythonInterpreter ...
>
> myInterpreter.exec("test = Test()");
> myInterpreter.exec("test.foo(Integer(1))");
> myInterpreter.exec("test.foo(2)");
> }
> }
>
>
>Expected output:
> Called with an Integer.
> Called with an int.
>
>Actual output:
> Called with an int.
> Called with an int.
>
>
>I have noticed similar behavior with Double objects.
>
>What am I doing wrong? How can I keep Integer and Double objects from being
>converted to Java primitives?
You can't. The eagerness with which Jython converts the java.lang.*
wrapper classes to instances of native python classes have been
discussed previousely:
http://mail.python.org/pipermail/jpython-interest/1999-December/002590.html
It is basicly the same problem you have re-discovered. I'm still not
convinved that it is a net win to reduce this eagerness.
http://mail.python.org/pipermail/jpython-interest/1999-December/002596.html
>Also, why does the following call to equals() on an Integer object...
>
> myInterpreter.set("myInteger", new Integer(1));
> myInterpreter.exec("myInteger.equals(Integer(1))");
>
>...result in an exception?
>
> Exception in thread "main" Traceback (innermost last):
> File "<string>", line 1, in ?
> AttributeError: 'int' object has no attribute 'equals'
Use this instead:
myInterpreter.set("myInteger",
new PyJavaInstance(new Integer(1)));
myInterpreter.exec("myInteger.equals(Integer(1))");
regards,
finn
|