|
From: marco <ma...@em...> - 2001-06-29 11:17:01
|
hy there, how can i cast a java-instance to another type. ex. x = myJavaClass1() y = (myJavaClass2) x where: myJavaClass2 extends myJavaClass1 x ma -- ******************************************************* Marco Machmer Phone : +49-2638-94 666 4 Fax : +49-2638-94 666 7 email : mailto:ma...@em... www : http://www.emha.de http://www.Marco-Machmer.de Um ein tadelloses Mitglied einer Schafherde sein zu koennen, muss man vor allem ein Schaf sein. Albert Einstein |
|
From: Kevin B. <kb...@ca...> - 2001-06-29 15:52:58
|
marco wrote: > > hy there, > > how can i cast a java-instance to another type. > ex. > x = myJavaClass1() > y = (myJavaClass2) x In Jython, that looks like: x = myJavaClass1() y = x :-) No casts needed. kb |
|
From: Stuart S. <st...@me...> - 2001-06-29 21:31:57
|
It isn't clear to me how object 'y' would be of Class myJavaClass2... Without any additional information, I would expect Jython to assign y to be a reference to x, of Class myJavaClass1. Unless y had been previously instantiated or referenced to (assigned to?) an object of Class myJavaClass2. I have had some difficulty in dealing with this (upcasting) in Java, and resorted to a shallow copy based on reflection... Stuart Kevin Butler wrote: > marco wrote: > > > > hy there, > > > > how can i cast a java-instance to another type. > > ex. > > x = myJavaClass1() > > y = (myJavaClass2) x > > In Jython, that looks like: > > x = myJavaClass1() > y = x > > :-) > > No casts needed. > > kb > > _______________________________________________ > Jython-dev mailing list > Jyt...@li... > http://lists.sourceforge.net/lists/listinfo/jython-dev |
|
From: Brian Z. <bri...@ya...> - 2001-06-29 23:40:26
|
In dynamic languages like Python/Smalltalk/Scheme, you usually don't need
cast. Because "the methods a particular instance can perform" becomes the
type of the instance, it's not as important which class that instance
actually belongs. At instantiation time, "all the methods the instance can
perform" is determined. At runtime, you either can call the method or you
get an AttributeError.
For example
>>> import java
>>> v = java.util.Vector()
>>> v.add(java.util.Properties()) # Properties extends Hashtable
1
>>> v.add(java.util.Hashtable())
1
>>> p = v.elementAt(0)
>>> h = v.elementAt(1)
>>> h.contains("abc")
0
>>> p.contains("abc")
0
>>>
>>> h.getProperty("abc")
Traceback (innermost last):
File "<console>", line 1, in ?
AttributeError: getProperty
>>> p.getProperty("abc") # works ok, just returns None
In the above example, the JVM+jython knows you can can call contains() on
both p and h, but you can only call getProperty() on p.
You can see that you don't need casting when assigning the returned Object
from v.elementAt() to a variable. Java must keep some class information
available at runtime for polymorphism to work, otherwise even with cast you
wouldn't know whether you can perform a certain method call or not.
BTW, this topic probably should be in jython-users list instead..
Regards,
-Brian
----- Original Message -----
From: "Stuart Swerdloff" <st...@me...>
To: <jyt...@li...>
Sent: Friday, June 29, 2001 2:31 PM
Subject: Re: [Jython-dev] casting
> It isn't clear to me how object 'y' would be of Class myJavaClass2...
> Without any additional information, I would expect Jython to assign y to
> be
> a reference to x, of Class myJavaClass1. Unless y had been previously
> instantiated
> or referenced to (assigned to?) an object of Class myJavaClass2.
>
> I have had some difficulty in dealing with this (upcasting) in Java, and
> resorted to
> a shallow copy based on reflection...
>
> Stuart
>
> Kevin Butler wrote:
>
> > marco wrote:
> > >
> > > hy there,
> > >
> > > how can i cast a java-instance to another type.
> > > ex.
> > > x = myJavaClass1()
> > > y = (myJavaClass2) x
> >
> > In Jython, that looks like:
> >
> > x = myJavaClass1()
> > y = x
> >
> > :-)
> >
> > No casts needed.
> >
> > kb
> >
> > _______________________________________________
> > Jython-dev mailing list
> > Jyt...@li...
> > http://lists.sourceforge.net/lists/listinfo/jython-dev
>
>
> _______________________________________________
> Jython-dev mailing list
> Jyt...@li...
> http://lists.sourceforge.net/lists/listinfo/jython-dev
>
|
|
From: Roman M. <ro...@sp...> - 2001-06-30 00:18:34
|
>>>>> "BZ" == Brian Zhou <bri...@ya...> writes:
BZ> In dynamic languages like Python/Smalltalk/Scheme, you usually
BZ> don't need cast. Because "the methods a particular instance
BZ> can perform" becomes the type of the instance, it's not as
BZ> important which class that instance actually belongs. At
BZ> instantiation time, "all the methods the instance can perform"
BZ> is determined. At runtime, you either can call the method or
BZ> you get an AttributeError.
What if you need to pass an object to a java method that requires it
to be cast to a different java type?
What if one java method gives me an Object back, but it is actually a
String. I have another java method that requires a String argument. Is
there a way to do that cast in jython?
I ran in to this when trying to use the Java xml-rpc library from
jython.
^Roman
|
|
From: Brian Z. <bri...@ya...> - 2001-06-30 00:37:23
|
That's exactly like the example I gave. You get the Object back (you know it's actually a String, right?), then you can just pass the instance directly to the xml-rpc method expecting a String. No cast needed. Because you call the method from Jython, there won't be any compilation error. Runtime, if it's really a String, no problem; if not, it will raise AttributeError for any String method get invoked. -Brian ----- Original Message ----- From: "Roman Milner" <ro...@sp...> To: "Brian Zhou" <bri...@ya...> Cc: <jyt...@li...> Sent: Friday, June 29, 2001 5:21 PM Subject: Re: [Jython-dev] casting > >>>>> "BZ" == Brian Zhou <bri...@ya...> writes: > > BZ> In dynamic languages like Python/Smalltalk/Scheme, you usually > BZ> don't need cast. Because "the methods a particular instance > BZ> can perform" becomes the type of the instance, it's not as > BZ> important which class that instance actually belongs. At > BZ> instantiation time, "all the methods the instance can perform" > BZ> is determined. At runtime, you either can call the method or > BZ> you get an AttributeError. > > What if you need to pass an object to a java method that requires it > to be cast to a different java type? > > What if one java method gives me an Object back, but it is actually a > String. I have another java method that requires a String argument. Is > there a way to do that cast in jython? > > I ran in to this when trying to use the Java xml-rpc library from > jython. > > ^Roman > |
|
From: Kevin B. <kb...@ca...> - 2001-06-30 04:48:15
|
???
public class A
{
public Object get()
{
return "Hello";
}
public void doit( String s )
{
System.out.println( s );
}
}
$ jython
Jython 2.1a1 on java1.3.1 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import A
>>> a = A()
>>> o = a.get()
>>> o
'Hello'
>>> type( o )
<jclass org.python.core.PyString at 4729773>
>>> a.doit( o )
Hello
>>> type( "" )
<jclass org.python.core.PyString at 4729773>
>>>
jython converts Java String objects into python strings, and transparently
converts them back into Java strings if you call a method that requires
them.
You may have problems if you put them in a jarray, a Collection, etc., but
just passing Strings should work fine.
Or you may have problems if you want to do String method ops on the
returned value:
>>> o.length()
Traceback (innermost last):
File "<console>", line 1, in ?
AttributeError: 'string' object has no attribute 'length'
So you can construct a new Java String from the Python string object:
>>> from java.lang import *
>>> o2 = String( o )
>>> o2.length()
5
Does that help?
kb
Roman Milner wrote:
> >>>>> "BZ" == Brian Zhou <bri...@ya...> writes:
>
> BZ> In dynamic languages like Python/Smalltalk/Scheme, you usually
> BZ> don't need cast. Because "the methods a particular instance
> BZ> can perform" becomes the type of the instance, it's not as
> BZ> important which class that instance actually belongs. At
> BZ> instantiation time, "all the methods the instance can perform"
> BZ> is determined. At runtime, you either can call the method or
> BZ> you get an AttributeError.
>
> What if you need to pass an object to a java method that requires it
> to be cast to a different java type?
>
> What if one java method gives me an Object back, but it is actually a
> String. I have another java method that requires a String argument. Is
> there a way to do that cast in jython?
>
> I ran in to this when trying to use the Java xml-rpc library from
> jython.
>
> ^Roman
>
> _______________________________________________
> Jython-dev mailing list
> Jyt...@li...
> http://lists.sourceforge.net/lists/listinfo/jython-dev
|
|
From: Kevin B. <kb...@ca...> - 2001-06-29 23:11:32
|
Woops!
I completely misread your post:
Marco wrote:
> how can i cast a java-instance to another type.
> ex.
> x = myJavaClass1()
> y = (myJavaClass2) x
>
> where:
> myJavaClass2 extends myJavaClass1
So you have:
public class A
{
}
public class B extends A
{
}
and you want to do:
{
A a = new A();
B b = (B) a;
}
Right?
You can't do that -- you will get a ClassCastException, because you're trying to use an object as if it were something it _knows_ it is not.
You can only cast to what the instance _really_ _is_.
The usual pattern is:
{
A a = new B();
B b = (B) b;
}
Which works as I described: So I've been using an instance of B as if it were an instance of A, but after the cast, I want to use it as what it really is.
The idiom, if you _really_ have an "A" and need to get a "B" is to do do a shallow copy/a delegation pattern something like this:
public B( A other ) // "copy constructor"
{
// either a shallow copy, as you said, or retain reference to 'other' instance to delegate to
}
public static B makeB( A other )
{
if ( other instanceof B )
{
return (B) other;
}
else
{
return new B( other );
}
}
This is not something you want to do often, and generally indicates you have a problem in the design.
kb
|