logo       

RE: bug with reflection: msg#00020

lang.jython.user

Subject: RE: bug with reflection


In Python you can call a method on an instance two ways:

1) theInstance.method(args)
2) theClass.method(theInstance,args)

When you are calling getName on your interface objects,
you are expecting to be using #1. However, if the interface
has a getName method, then you are actually doing #2.
That is why it complains it expects one argument, the
instance. For your code to work always, you ought to
do it like this:

for i in interfaces:
print "interface: ", java.lang.Class.getName(i)


> -----Original Message-----
> From: Curtis Brown [mailto:cpbrown@xxxxxxxxxxxxxxxxx]
> Sent: Wednesday, February 12, 2003 8:16 AM
> To: jython-users@xxxxxxxxxxxxxxxxxxxxx
> Subject: [Jython-users] bug with reflection
>
>
> I was trying to get jython to do some simple reflection, but it
> sometimes failes with a type error. Here is the code:
>
> import sys
> import java
>
> if __name__ == '__main__':
> arg = sys.argv[1]
> c = java.lang.Class.forName(arg)
> interfaces = c.getInterfaces()
> for i in interfaces:
> print "interface: ", i.getName()
>
> The script takes a java class as an agument and is supposed to print
> all the interfaces the class implements. It works sometimes but fails
> others:
>
> % jython bug.py java.util.HashMap
> interface: java.util.Map
> interface: java.lang.Cloneable
> interface: java.io.Serializable
>
> % jython bug.py java.security.Identity
> interface: Traceback (innermost last):
> File "bug.py", line 9, in ?
> TypeError: getName(): expected 1 args; got 0
>
> The object c is an instance of org.python.core.PyJavaClass in every
> case. Any idea why this doen not work? I'm running on solaris 2.8,
> with Jython 2.1 on java1.4.0 (JIT: null).
>
> Thanks,
> Curt
>
>
>
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
> http://www.vasoftware.com
> _______________________________________________
> Jython-users mailing list
> Jython-users@xxxxxxxxxxxxxxxxxxxxx
> https://lists.sourceforge.net/lists/listinfo/jython-users
>


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise