From 2be293a647acf2fcab84a6dc20a7cf0a7f94ac90 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 11 Jul 2019 13:07:10 +0200 Subject: [PATCH] Fix std::vector<> Java typemaps for primitive types For such types, the generated proxy class inherited from java.util.AbstractSet> (where BoxedType is "Integer", for example, when T is "int"), but defined an overloaded add() taking T, instead of overriding the base class virtual add() taking BoxedType, resulting in an exception being thrown whenever add() was called during run-time. Extend Java unit test to bring it to parity with C# one added in the previous commit. See #1568. --- Examples/test-suite/java/li_std_set_runme.java | 9 +++++++++ Examples/test-suite/li_std_set.i | 3 --- Lib/java/std_set.i | 6 +++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Examples/test-suite/java/li_std_set_runme.java b/Examples/test-suite/java/li_std_set_runme.java index 017908c21..0e013eba1 100644 --- a/Examples/test-suite/java/li_std_set_runme.java +++ b/Examples/test-suite/java/li_std_set_runme.java @@ -77,5 +77,14 @@ public class li_std_set_runme { checkThat(fooSet.isEmpty()); checkThat(fooSet.add(new Foo(17))); checkThat(fooSet.size() == 1); + + // And a set of primitive type. + java.util.AbstractSet intSet = new IntSet(); + checkThat(intSet.isEmpty()); + checkThat(intSet.add(17)); + checkThat(!intSet.add(17)); + checkThat(intSet.size() == 1); + checkThat(intSet.add(289)); + checkThat(intSet.size() == 2); } } diff --git a/Examples/test-suite/li_std_set.i b/Examples/test-suite/li_std_set.i index 5dbcc33e1..507272d8d 100644 --- a/Examples/test-suite/li_std_set.i +++ b/Examples/test-suite/li_std_set.i @@ -40,10 +40,7 @@ }; %} - // This one doesn't work in Java correctly yet. -#ifdef SWIGCSHARP %template(IntSet) std::set; -#endif %template(StringSet) std::set; %template(FooSet) std::set; #endif diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i index 04658f765..73e0c2cf9 100644 --- a/Lib/java/std_set.i +++ b/Lib/java/std_set.i @@ -57,6 +57,10 @@ class set { return sizeImpl(); } + public boolean add($typemap(jboxtype, T) key) { + return addImpl(key); + } + public boolean addAll(java.util.Collection collection) { boolean didAddElement = false; for (java.lang.Object object : collection) { @@ -172,7 +176,7 @@ class set { %fragment("SWIG_SetSize"); // Returns whether item was inserted. - bool add(const T& key) { + bool addImpl(const T& key) { return self->insert(key).second; }