From 62d361071ea54c9b7cba882313ab4dedef6f1286 Mon Sep 17 00:00:00 2001 From: Andres Noetzli Date: Thu, 16 May 2019 00:18:48 +0000 Subject: Fix iterators in Java API (#3000) Fixes #2989. SWIG 3 seems to have an issue properly resolving `T::const_iterator::value_type` if that type itself is a `typedef`. This is for example the case in the `UnsatCore` class, which `typedef`s `const_iterator` to `std::vector::const_iterator`. As a workaround, this commit changes the `JavaIteratorAdapter` class to take two template parameters, one of which is the `value_type`. The commit also adds a compile-time assertion that `T::const_iterator::value_type` can be converted to `value_type` to avoid nasty surprises. A nice side-effect of this solution is that explicit `typemap`s are not necessary anymore, so they are removed. Additionally, the commit adds a `toString()` method for the Java API of `UnsatCore` and adds examples that show and test the iteration over the unsat core and the statistics. Iterating over `Statistics` now returns instances of `Statistic` instead of `Object[]`, which is a bit cleaner and requires less glue code. --- src/smt/command.i | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'src/smt/command.i') diff --git a/src/smt/command.i b/src/smt/command.i index 32bda7bba..ff8094165 100644 --- a/src/smt/command.i +++ b/src/smt/command.i @@ -28,8 +28,10 @@ %ignore CVC4::CommandSequence::begin() const; %ignore CVC4::CommandSequence::end() const; %extend CVC4::CommandSequence { - CVC4::JavaIteratorAdapter iterator() { - return CVC4::JavaIteratorAdapter(*$self); + CVC4::JavaIteratorAdapter iterator() + { + return CVC4::JavaIteratorAdapter( + *$self); } } @@ -37,10 +39,10 @@ %typemap(javainterfaces) CVC4::CommandSequence "java.lang.Iterable"; // the JavaIteratorAdapter should not be public, and implements Iterator -%typemap(javaclassmodifiers) CVC4::JavaIteratorAdapter "class"; -%typemap(javainterfaces) CVC4::JavaIteratorAdapter "java.util.Iterator"; +%typemap(javaclassmodifiers) CVC4::JavaIteratorAdapter "class"; +%typemap(javainterfaces) CVC4::JavaIteratorAdapter "java.util.Iterator"; // add some functions to the Java side (do it here because there's no way to do these in C++) -%typemap(javacode) CVC4::JavaIteratorAdapter " +%typemap(javacode) CVC4::JavaIteratorAdapter " public void remove() { throw new java.lang.UnsupportedOperationException(); } @@ -54,13 +56,7 @@ } " // getNext() just allows C++ iterator access from Java-side next(), make it private -%javamethodmodifiers CVC4::JavaIteratorAdapter::getNext() "private"; - -// map the types appropriately -%typemap(jni) CVC4::CommandSequence::const_iterator::value_type "jobject"; -%typemap(jtype) CVC4::CommandSequence::const_iterator::value_type "edu.nyu.acsys.CVC4.Command"; -%typemap(jstype) CVC4::CommandSequence::const_iterator::value_type "edu.nyu.acsys.CVC4.Command"; -%typemap(javaout) CVC4::CommandSequence::const_iterator::value_type { return $jnicall; } +%javamethodmodifiers CVC4::JavaIteratorAdapter::getNext() "private"; #endif /* SWIGJAVA */ @@ -71,6 +67,6 @@ %include "bindings/java_iterator_adapter.h" %include "bindings/java_stream_adapters.h" -%template(JavaIteratorAdapter_CommandSequence) CVC4::JavaIteratorAdapter; +%template(JavaIteratorAdapter_CommandSequence) CVC4::JavaIteratorAdapter; #endif /* SWIGJAVA */ -- cgit v1.2.3