Package jep

Class AllowPythonClassEnquirer

java.lang.Object
jep.AllowPythonClassEnquirer
All Implemented Interfaces:
ClassEnquirer

public class AllowPythonClassEnquirer extends Object implements ClassEnquirer

A ClassEnquirer that defines specific packages as Python packages. This implementation takes a delegate ClassEnquirer to delegate all import determinations as Java or Python except in the cases of the specific Python package names provided. Imports of the specified packages in the embedded Python interpreter will use the Python importer and not the Java importer. This is useful when you have package name conflicts between Java and Python. Some examples are projects like py4j and tensorflow which have Python package names matching Java package names. In those cases, using the default ClassEnquirer of ClassList will lead to the Java packages on the classpath taking precedence on imports instead of the Python package. That can be solved by using this ClassEnquirer with a delegate of ClassList's instance.

Since:
4.2.1
  • Constructor Details

    • AllowPythonClassEnquirer

      public AllowPythonClassEnquirer(ClassEnquirer delegate, String... pythonPackageNames)
      Constructor
      Parameters:
      delegate - the ClassEnquirer instance to delegate method calls to except in the case of the specified Python package names
      pythonPackageNames - the names of Python packages that should not be treated as a potential import from Java and instead should immediately default to being imported by the normal Python importer
  • Method Details

    • isJavaPackage

      public boolean isJavaPackage(String name)
      Description copied from interface: ClassEnquirer
      Checks if the name is likely available in Java as a package. A return value of true implies the name corresponds to a Java package, but does not guarantee that an import will succeed. A return value of false implies that an import from Java would fail, but does not guarantee that an import will fail. Note: A fully-qualified Java class name should return false since it is not a package name and the importer hook is expecting that.
      Specified by:
      isJavaPackage in interface ClassEnquirer
      Parameters:
      name - the name to check, such as java, java.util, java.util.ArrayList
      Returns:
      true if it's likely a package supported by Java, false if it's likely a Python module (or a Java class name, or an invalid import)
    • getClassNames

      public String[] getClassNames(String pkgName)
      Description copied from interface: ClassEnquirer
      Given a Java package name, gets the fully-qualified classnames available for import in the package. This method is primarily used for introspection using Python's dir() method. This method can return null if dir() support is not necessary.
      Specified by:
      getClassNames in interface ClassEnquirer
      Parameters:
      pkgName - the name of a package the ClassEnquirer supports, such as java.util
      Returns:
      the list of classnames in the package, or null
    • getSubPackages

      public String[] getSubPackages(String pkgName)
      Description copied from interface: ClassEnquirer
      Given a Java package name, gets the sub-packages available. For example, a sub-package of package "java" is "util", and a sub-package of package "java.util" is "concurrent". This method is primarily used for introspection using Python's dir() method. This method can return null if dir() support is not necessary.
      Specified by:
      getSubPackages in interface ClassEnquirer
      Parameters:
      pkgName - the name of a package the ClassEnquirer supports, such as java.util
      Returns:
      the list of sub-packages in the package, or null