|
From: Oti <oh...@gm...> - 2007-07-27 21:08:11
|
On 7/27/07, Charlie Groves <cha...@gm...> wrote: > On 7/26/07, Oti <oh...@gm...> wrote: > > On 7/23/07, Charlie Groves <cha...@gm...> wrote: > > > > It does seem like it would be possible to eliminate the need for > > > add_package if we auto-created nonexistent packages if a user imports > > > a fully qualified Java class. Since we can ask the classloader if a > > > class really exists, we could use its existence to confirm the > > > existence of the packages that contain it. Those packages would still > > > be odd since they wouldn't be fully populated with things that haven't > > > been imported, but I think that would be better than requiring > > > add_package. Yet another thing for the list of things it would be > > > nice to have in Jython.... > > > > That's exactly what I'm doing when an explicit import of a class in a > > not package-scanned package happens: > > 1) find out if it is an existent java package > > 2) if yes, do the add_package and the import > > This means that sys.add_package() should not be necessary if explicit > > imports are used. > > What do you mean by an explicit import? Does 'from > org.eclipse.jface.viewers import > IStructuredContentProvider' as in David's example qualify? Yes, because both java package and java class are mentioned. > > For step 1), I am using > > Thread.currentThread().getContextClassLoader().getResource() > > Of course this is not the same as Class.forName(), but IMHO both > > should succeed if the classloader is the same. > > > > There might be cases when the context class loader is the wrong one > > (e.g. if PythonInterpreter is running inside of an Eclipse PLUGIN - > > they do their own classloader magic). > > I think using Py.findClass might be a better way to do this. It tries > the ClassLoader in the current PySystemState first, which is what a > user will have set in an Eclipse plugin or anywhere they're forced to > use a custom class loader. It's probably worth writing some bugtests > to try out various permutations of classloader setups. Thanks for the tip! I'll check that. Oti. |