fixes hashCode overflow. (disclamer: i wont talk about collision probabiliity or whatever. In my point of view, it seems very low, as the id() is based on the address of the object, it except if you uses tons of memory, you wont get it. And whatever the changes are, this will never keep up safe from colliding with others java object.). Closes #146

This commit is contained in:
Mathieu Virbel 2015-03-02 11:22:05 +01:00
parent a0c9467d42
commit ce3572ba79
1 changed files with 4 additions and 2 deletions

View File

@ -15,15 +15,17 @@ from .reflect import *
# XXX monkey patch methods that cannot be in cython. # XXX monkey patch methods that cannot be in cython.
# Cython doesn't allow to set new attribute on methods it compiled # Cython doesn't allow to set new attribute on methods it compiled
HASHCODE_MAX = 2 ** 31 - 1
class PythonJavaClass_(PythonJavaClass): class PythonJavaClass_(PythonJavaClass):
@java_method('()I', name='hashCode') @java_method('()I', name='hashCode')
def hashCode(self): def hashCode(self):
return id(self) return id(self) % HASHCODE_MAX
@java_method('()Ljava/lang/String;', name='hashCode') @java_method('()Ljava/lang/String;', name='hashCode')
def hashCode_(self): def hashCode_(self):
return '{}'.format(id(self)) return '{}'.format(self.hashCode())
@java_method('()Ljava/lang/String;', name='toString') @java_method('()Ljava/lang/String;', name='toString')
def toString(self): def toString(self):