From: Samuele P. <ped...@st...> - 2005-09-15 19:06:15
|
Moss Prescott wrote: > I'm trying to use Java reflection from Jython to discover the Bean-style > setters that take one of a set of types, and I've run into a problem > which seems to be the result of Jython's attempt to reconcile Python vs. > Java call semantics. > > Specifically, I can't get the name of a class which declares a method > called getName(), because Jython maps the call to a call to the class's > method (taking an instance as a parameter), instead of a call to the > method on the Class object itself: > > >>> import java > >>> c = java.lang.Class.forName('java.awt.Color') > >>> c.getName() # invokes Class.getName() > 'java.awt.Color' > >>> f = java.lang.Class.forName('java.awt.Font') > >>> f.getName() # WRONG! tries to invoke Font.getName() > TraceBack (innermost last): > ... > TypeError: getName(): expected 1 args; got 0 > >>> f.getName(java.awt.Font('serif', 0, 0)) > 'serif' > > Where the last is equivalent to the following in Java: > new Font('serif', 0, 0).getName() > > Is there a workaround to get the name from the Class? If not, I'll have > to take a different approach. > you can go through Python reflection in this case and use .__name__ >>> import java >>> import java.awt >>> Font=java.awt.Font >>> Font.__name__ 'java.awt.Font' or use the unbound method from Class, which is usually the safe way to access this kind of reflective functionality from java avoiding clashes >>> java.lang.Class.getName(Font) 'java.awt.Font' |