Skip to content

Commit

Permalink
modernize jvmfinder a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
marscher committed Aug 20, 2024
1 parent ce75eea commit 155abab
Showing 1 changed file with 34 additions and 50 deletions.
84 changes: 34 additions & 50 deletions jpype/_jvmfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import sys

__all__ = ['getDefaultJVMPath',
'JVMNotFoundException', 'JVMNotSupportedException']


'get_default_jvm_path',
'JVMNotFoundException',
'JVMNotSupportedException']


class JVMNotFoundException(ValueError):
Expand All @@ -47,7 +47,7 @@ class JVMNotSupportedException(ValueError):
"""


def getDefaultJVMPath():
def getDefaultJVMPath() -> str:
"""
Retrieves the path to the default or first found JVM library
Expand All @@ -72,21 +72,17 @@ def getDefaultJVMPath():
get_default_jvm_path = getDefaultJVMPath


class JVMFinder(object):
class JVMFinder:
"""
JVM library finder base class
"""
# Library file name
_libfile = "libjvm.so"

def __init__(self):
"""
Sets up members
"""
# Library file name
self._libfile = "libjvm.so"

# Predefined locations
self._locations = ("/usr/lib/jvm", "/usr/java")
# Predefined locations
_locations = ("/usr/lib/jvm", "/usr/java")

def __init__(self):
# Search methods
self._methods = (self._get_from_java_home,
self._get_from_known_locations)
Expand Down Expand Up @@ -128,7 +124,8 @@ def find_libjvm(self, java_home):
"environment variable is pointing "
"to correct installation.")

def find_possible_homes(self, parents):
@staticmethod
def find_possible_homes(parents):
"""
Generator that looks for the first-level children folders that could be
Java installations, according to their name
Expand Down Expand Up @@ -251,21 +248,17 @@ class LinuxJVMFinder(JVMFinder):
Linux JVM library finder class
"""

def __init__(self):
"""
Sets up members
"""
# Call the parent constructor
JVMFinder.__init__(self)
# Java bin file
_java = "/usr/bin/java"

# Java bin file
self._java = "/usr/bin/java"
# Library file name
_libfile = "libjvm.so"

# Library file name
self._libfile = "libjvm.so"
# Predefined locations
_locations = ("/usr/lib/jvm", "/usr/java", "/opt/sun")

# Predefined locations
self._locations = ("/usr/lib/jvm", "/usr/java", "/opt/sun")
def __init__(self):
super().__init__()

# Search methods
self._methods = (self._get_from_java_home,
Expand Down Expand Up @@ -294,23 +287,20 @@ class DarwinJVMFinder(LinuxJVMFinder):
"""
Mac OS X JVM library finder class
"""
# Library file name
_libfile = "libjli.dylib"
# Predefined locations
_locations = ('/Library/Java/JavaVirtualMachines',)

def __init__(self):
"""
Sets up members
"""
# Call the parent constructor
LinuxJVMFinder.__init__(self)

# Library file name
self._libfile = "libjli.dylib"
super().__init__()

self._methods = list(self._methods)
self._methods.append(self._javahome_binary)

# Predefined locations
self._locations = ('/Library/Java/JavaVirtualMachines',)

def _javahome_binary(self):
"""
for osx > 10.5 we have the nice util /usr/libexec/java_home available. Invoke it and
Expand All @@ -321,7 +311,8 @@ def _javahome_binary(self):
from packaging.version import Version

current = Version(platform.mac_ver()[0][:4])
if current >= Version('10.6') and current < Version('10.9'):
# TODO: check if the java_home tool is still available and fix the version boundaries.
if Version('10.6') <= current: #< Version('10.9'):
return subprocess.check_output(
['/usr/libexec/java_home']).strip()

Expand Down Expand Up @@ -356,25 +347,18 @@ def _checkJVMArch(jvmPath, maxsize=sys.maxsize):
raise JVMNotSupportedException("Unable to determine JVM Type")


reg_keys = [r"SOFTWARE\JavaSoft\Java Runtime Environment",
r"SOFTWARE\JavaSoft\JRE",
]


class WindowsJVMFinder(JVMFinder):
"""
Windows JVM library finder class
"""
reg_keys = [r"SOFTWARE\JavaSoft\Java Runtime Environment",
r"SOFTWARE\JavaSoft\JRE",
]
# Library file name
_libfile = "jvm.dll"

def __init__(self):
"""
Sets up members
"""
# Call the parent constructor
JVMFinder.__init__(self)

# Library file name
self._libfile = "jvm.dll"
super().__init__()

# Search methods
self._methods = (self._get_from_java_home, self._get_from_registry)
Expand All @@ -395,7 +379,7 @@ def _get_from_registry():
except ImportError:
return None

for location in reg_keys:
for location in WindowsJVMFinder.reg_keys:

Check warning on line 382 in jpype/_jvmfinder.py

View check run for this annotation

Codecov / codecov/patch

jpype/_jvmfinder.py#L382

Added line #L382 was not covered by tests
try:
jreKey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, location)
cv = winreg.QueryValueEx(jreKey, "CurrentVersion")
Expand Down

0 comments on commit 155abab

Please sign in to comment.