Skip to content

Commit

Permalink
fixes hashCode overflow. (disclamer: i wont talk about collision prob…
Browse files Browse the repository at this point in the history
…abiliity 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
  • Loading branch information
tito committed Mar 2, 2015
1 parent a0c9467 commit ce3572b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions jnius/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
# XXX monkey patch methods that cannot be in cython.
# Cython doesn't allow to set new attribute on methods it compiled

HASHCODE_MAX = 2 ** 31 - 1

class PythonJavaClass_(PythonJavaClass):

@java_method('()I', name='hashCode')
def hashCode(self):
return id(self)
return id(self) % HASHCODE_MAX

@java_method('()Ljava/lang/String;', name='hashCode')
def hashCode_(self):
return '{}'.format(id(self))
return '{}'.format(self.hashCode())

@java_method('()Ljava/lang/String;', name='toString')
def toString(self):
Expand Down

0 comments on commit ce3572b

Please sign in to comment.