I find it tedious to import a java class in jython because of the long package names. e.g. com.example.x.y.z.SomeClass. It’s a lot of typing. I want to import by using only the simple name of the class (SomeClass in my example). Is there a way of achieving it? Current solution in my head is to enumerate all the classes in the classpath and prepare a map of simple class name to the complete package name like –
SomeClass -> com.example.x.y.z.SomeClass
SomeOtherClass -> com.example.p.q.r.SomeOtherClass
etc. Then call a function something like below-
def intelligent_import(simple_class_name):
#No error checking, simplified for clarity
package_name = dict[simle_class_name]
simple_class_name = __import__(package_name)
for each of the classes. The only problem is that I don’t know a method to enumearte all classes in a directory. Is there’s a better way to do this? If not a method to enumerate all java classes in a directory will do.
This is a bit of a hack, I’m sure there’s better our there, but if everything else fails, remember that JAR files are just ZIPs, and that each class will have a .class file in the jar.
However, rather than hacking the import mechanism like this, I’d rather just use an IDE with proper autocompletion (i.e. Eclipse/PyDev, it’s the only one that can do it reliably in my experience), and rock the ctrl-space.