From ce3572ba79bff1a91066f7899673f63c1c3ecf7c Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Mon, 2 Mar 2015 11:22:05 +0100 Subject: [PATCH] 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 --- jnius/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jnius/__init__.py b/jnius/__init__.py index 8ac24eb..34c2599 100644 --- a/jnius/__init__.py +++ b/jnius/__init__.py @@ -15,15 +15,17 @@ from .reflect import * # 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):