开发者

Fully qualified class name in Jython

Supposing some.module.MyClass is a Java class, if it is imported in Jython 2.1, its fully qualified class name can be got like this:

from some.module import MyClass
print MyClass.__name__

then "some.module.MyClass" was printed.

Now, the same code in Jython 2.5 prints "MyClass" only.

To get the complete name in Jython 2.5, I've found the following:

print MyClass.name

It prints "some.module.MyClass" (I guess java.lang.Class.getName() is called).

The problem is if MyClass has a getName method. Then the above fails.

The solution I've found is to call

print MyClass.__module__ + "." + MyClass.__name__

but it is a lot more wordy.

If anyone knows a simpler way (like the original __name__), it would be welcome.

Note: I already know the full class name (in fact I have imported it). I want it this way to detect changes in ca开发者_如何学编程se the class is renamed or moved. For example:

className = "some.module.MyClass"   # needs to be changed manually if MyClass is moved

opposed to

className = MyClass.name   # no need to be changed if MyClass is moved


The following works:

print MyClass.canonicalName

It would print "some.module.MyClass" as expected.

In this case, java.lang.Class.getCanonicalName is called.

It is less likely that an arbitrary class will have a method called getCanonicalName() than getName(), so in principle it is more reliable than

print MyClass.name

Nevertheless, a pure Jython simple way (if existing) would also be appreciated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜