Skip to content

Commit

Permalink
Merge pull request #1012 from Thrameos/classtype_hints
Browse files Browse the repository at this point in the history
java.lang.Class and arrays conversion hints
  • Loading branch information
Thrameos authored Nov 2, 2021
2 parents 77b2aff + bcf9ed6 commit bbdca90
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions doc/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ This changelog *only* contains changes from the *first* pypi release (0.5.4.3) o
Latest Changes:
- **1.3.1_dev0 - 2021-06-05**

- Support for user defined conversions for java.lang.Class and array types.

- Fixed issue with ssize_t on Windows for Python 3.10.


- **1.3.0 - 2021-05-19**

- Fixes for memory issues found when upgrading to Python 3.10 beta.
Expand Down
2 changes: 2 additions & 0 deletions native/common/jp_arrayclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ JPMatch::Type JPArrayClass::findJavaConversion(JPMatch &match)
|| charArrayConversion->matches(this, match)
|| byteArrayConversion->matches(this, match)
|| sequenceConversion->matches(this, match)
|| hintsConversion->matches(this, match)
)
return match.type;
JP_TRACE("None");
Expand All @@ -57,6 +58,7 @@ void JPArrayClass::getConversionInfo(JPConversionInfo &info)
charArrayConversion->getInfo(this, info);
byteArrayConversion->getInfo(this, info);
sequenceConversion->getInfo(this, info);
hintsConversion->getInfo(this, info);
PyList_Append(info.ret, PyJPClass_create(frame, this).get());
}

Expand Down
12 changes: 6 additions & 6 deletions native/common/jp_classtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ JPClassType::~JPClassType()
JPMatch::Type JPClassType::findJavaConversion(JPMatch& match)
{
JP_TRACE_IN("JPClass::findJavaConversion");
if (nullConversion->matches(this, match) != JPMatch::_none)
return match.type;
if (objectConversion->matches(this, match) != JPMatch::_none)
return match.type;
if (classConversion->matches(this, match) != JPMatch::_none)
if (nullConversion->matches(this, match)
|| objectConversion->matches(this, match)
|| classConversion->matches(this, match)
|| hintsConversion->matches(this, match))
return match.type;
return match.type = JPMatch::_none;
JP_TRACE_OUT;
Expand All @@ -52,5 +51,6 @@ void JPClassType::getConversionInfo(JPConversionInfo &info)
nullConversion->getInfo(this, info);
objectConversion->getInfo(this, info);
classConversion->getInfo(this, info);
hintsConversion->getInfo(this, info);
PyList_Append(info.ret, PyJPClass_create(frame, this).get());
}
}
42 changes: 42 additions & 0 deletions test/jpypetest/test_classhints.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ def blah(self):
pass


class ClassProxy:
def __init__(self, proxy):
self.proxy = proxy


class ArrayProxy:
def __init__(self, proxy):
self.proxy = proxy


class StringProxy:
def __init__(self, proxy):
self.proxy = proxy


class ClassHintsTestCase(common.JPypeTestCase):

def setUp(self):
Expand Down Expand Up @@ -85,3 +100,30 @@ def StrToCustom(jcls, args):
cht.call(MyImpl())
self.assertIsInstance(cht.input, self.MyCustom)
self.assertIsInstance(cht.input.arg, MyImpl)

def testClassCustomizer(self):

@jpype.JConversion("java.lang.Class", instanceof=ClassProxy)
def ClassCustomizer(jcls, obj):
return obj.proxy

hints = jpype.JClass('java.lang.Class')._hints
self.assertTrue(ClassProxy in hints.implicit)

def testArrayCustomizer(self):

@jpype.JConversion(jpype.JInt[:], instanceof=ArrayProxy)
def ArrayCustomizer(jcls, obj):
return obj.proxy

hints = jpype.JClass(jpype.JInt[:])._hints
self.assertTrue(ArrayProxy in hints.implicit)

def testStringCustomizer(self):

@jpype.JConversion("java.lang.String", instanceof=StringProxy)
def STringCustomizer(jcls, obj):
return obj.proxy

hints = jpype.JClass("java.lang.String")._hints
self.assertTrue(StringProxy in hints.implicit)

0 comments on commit bbdca90

Please sign in to comment.