* This is handy in cases where the package scan cannot run, or when the initial classpath does * not contain all .jar files (such as in J2EE containers). *
* There is some self-healing in the sense that a correct, explicit import of a java class will
* succeed even if sys.modules already contains a Py.None entry for the corresponding java
* package.
*
* @param packageName The dotted name of the java package
* @param fromlist A tuple with the from names to import. Can be null or empty.
*
* @return true if a java package was doubtlessly identified and added,
* false otherwise.
*/
protected static boolean tryAddPackage(final String packageName, PyObject fromlist) {
// make sure we do not turn off the added flag, once it is set
boolean packageAdded = false;
if (packageName != null) {
// check explicit imports first (performance optimization)
// handle 'from java.net import URL' like explicit imports
List
* May return false even if the given package name is a valid java package !
*
* @param packageName
*
* @return true if the package with the given name is already loaded by the VM,
* false otherwise.
*/
protected static boolean isLoadedPackage(String packageName) {
return isLoadedPackage(packageName, buildLoadedPackages());
}
/**
* Convert the fromlist into a java.lang.String based list.
*
* Do some sanity checks: filter out '*' and empty tuples, as well as non tuples.
*
* @param fromlist
* @return a list containing java.lang.String entries
*/
private static final List
* May return false even if the given package name is a valid java package !
*
* @param packageName
* @param packages A Map containing all packages actually known to the VM. Such a Map can be
* obtained using {@link JavaImportHelper.buildLoadedPackagesTree()}
*
* @return true if the package with the given name is already loaded by the VM,
* false otherwise.
*/
private static boolean isLoadedPackage(String javaPackageName, MapMap of the currently known packages to the VM.
*
* All parent packages appear as single entries like python modules, e.g. java,
* java.lang, java.lang.reflect,
*/
private static Maptrue if the java class can be found by the current Py classloader setup
*/
private static boolean isJavaClass(String packageName, String className) {
return className != null && className.length() > 0
&& Py.findClass(packageName + "." + className) != null;
}
/**
* Add a java package to sys.modules, if not already done.
*
* @return true if something was really added, false otherwise
*/
private static boolean addPackage(String packageName, boolean packageAdded) {
PyObject modules = Py.getSystemState().modules;
String internedPackageName = packageName.intern();
PyObject module = modules.__finditem__(internedPackageName);
// a previously failed import could have created a Py.None entry in sys.modules
if (module == null || module == Py.None) {
int dotPos = 0;
do {
PyJavaPackage p = PySystemState.add_package(packageName);
if(dotPos == 0) {
modules.__setitem__(internedPackageName, p);
} else {
module = modules.__finditem__(internedPackageName);
if (module == null || module == Py.None) {
modules.__setitem__(internedPackageName, p);
}
}
dotPos = packageName.lastIndexOf(DOT);
if (dotPos > 0) {
packageName = packageName.substring(0, dotPos);
internedPackageName = packageName.intern();
}
} while(dotPos > 0);
// make sure not to turn off the packageAdded flag
packageAdded = true;
}
return packageAdded;
}
}