Merge branch 'java-director-exceptions'

* java-director-exceptions:
  Add recent Java director enhancements to the changes file
  Replace DirectorException::raiseJavaException with DirectorException::throwException
  Improve Java director exception customization documentation
  Add Swig::DirectorException directorthrows typemap for Java
  Cosmetic whitespace improvement in generated code from throws typemaps.
  Tidy up director_exception testcase
  Changes to use common DirectorException class
  Improved Java director exceptions documentation
  Java director exception handling improvements
This commit is contained in:
William S Fulton 2017-11-29 20:32:40 +00:00
commit 069ce1f6e9
21 changed files with 709 additions and 224 deletions

View file

@ -181,6 +181,7 @@ CPP_TEST_CASES += \
director_detect \
director_enum \
director_exception \
director_exception_catches \
director_extend \
director_finalizer \
director_frob \

View file

@ -20,14 +20,10 @@
#ifndef SWIG_DIRECTORS
namespace Swig {
class DirectorException {};
class DirectorMethodException: public Swig::DirectorException {};
class DirectorException {};
class DirectorMethodException: public Swig::DirectorException {};
}
#ifndef SWIG_fail
#define SWIG_fail
#endif
#endif /* !SWIG_DIRECTORS */
%}
%include "std_string.i"
@ -41,8 +37,8 @@ class DirectorMethodException: public Swig::DirectorException {};
}
%exception {
try { $action }
catch (Swig::DirectorException &) { SWIG_fail; }
try { $action }
catch (Swig::DirectorException &) { SWIG_fail; }
}
#endif
@ -56,8 +52,8 @@ class DirectorMethodException: public Swig::DirectorException {};
}
%exception {
try { $action }
catch (Swig::DirectorException &) { SWIG_fail; }
try { $action }
catch (Swig::DirectorException &) { SWIG_fail; }
}
#endif
@ -75,12 +71,12 @@ class DirectorMethodException: public Swig::DirectorException {};
// Change back to old 2.0 default behavior
%feature("director:except") {
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
// Dont clear exception, still be active when return to java execution
// Essentially ignore exception occurred -- old behavior.
return $null;
}
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
// Dont clear exception, still be active when return to java execution
// Essentially ignore exception occurred -- old behavior.
return $null;
}
}
#endif

View file

@ -0,0 +1,25 @@
%module(directors="1") director_exception_catches
%include <std_string.i>
%feature("director") BaseClass;
%{
// define dummy director exception classes to prevent spurious errors
// in target languages that do not support directors.
#ifndef SWIG_DIRECTORS
namespace Swig {
class DirectorException {};
}
#endif /* !SWIG_DIRECTORS */
%}
%catches(Swig::DirectorException) BaseClass::call_description;
%inline %{
struct BaseClass {
virtual std::string description() const = 0;
static std::string call_description(BaseClass& bc) { return bc.description(); }
virtual ~BaseClass() {}
};
%}

View file

@ -0,0 +1,35 @@
import director_exception_catches.*;
public class director_exception_catches_runme {
static {
try {
System.loadLibrary("director_exception_catches");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void main(String argv[]) {
BaseClass b = new director_exception_catches_MyClass();
try {
String s = BaseClass.call_description(b);
throw new RuntimeException("Failed to catch exception");
} catch (NullPointerException e) {
if (!e.getMessage().startsWith("Testing exception thrown in BaseClass.description"))
throw new RuntimeException("Unexpected exception message: " + e.getMessage());
}
}
}
class director_exception_catches_MyClass extends BaseClass {
@Override
public String description() {
throw new NullPointerException("Testing exception thrown in BaseClass.description");
}
}

View file

@ -126,19 +126,28 @@ public class java_director_exception_feature_nspace_runme {
try { b.genericpong(1); fail("No exception thrown in genericpong(1)"); }
catch (MyJavaException1 e) {
failif( ! java_director_exception_feature_nspace_Consts.GENERICPONGEXCP1.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
StackTraceElement[] st = e.getStackTrace();
failif( st.length != 5, "Stack length is only " + st.length);
failif( ! st[0].toString().startsWith("java_director_exception_feature_nspace_MyFooDirectorImpl.genericpong(java_director_exception_feature_nspace_runme.java:"), "Incorrect top of stack: " + st[0]);
}
try { b.genericpong(2); fail("No exception thrown in genericpong(2)");}
catch (java_director_exception_feature_nspace_NewCheckedException e) {
failif( ! java_director_exception_feature_nspace_Consts.GENERICPONGEXCP2.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
StackTraceElement[] st = e.getStackTrace();
failif( st.length != 5, "Stack length is only " + st.length);
failif( ! st[0].toString().startsWith("java_director_exception_feature_nspace_MyFooDirectorImpl.genericpong(java_director_exception_feature_nspace_runme.java:"), "Incorrect top of stack: " + st[0]);
}
try { b.genericpong(3); fail("No exception thrown in genericpong(3)");}
catch (java_director_exception_feature_nspace_NewUncheckedException e) {
failif( ! java_director_exception_feature_nspace_Consts.GENERICPONGEXCP3.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
StackTraceElement[] st = e.getStackTrace();
failif( st.length != 5, "Stack length is only " + st.length);
failif( ! st[0].toString().startsWith("java_director_exception_feature_nspace_MyFooDirectorImpl.genericpong(java_director_exception_feature_nspace_runme.java:"), "Incorrect top of stack: " + st[0]);
}
try { b.genericpong(4); fail("No exception thrown in genericpong(4)");}
catch (RuntimeException e) {
failif ( e.getClass() != RuntimeException.class, "Exception " + e + " is not exactly RumtimeException");
failif( ! java_director_exception_feature_nspace_Consts.GENERICPONGEXCP4.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
failif ( e.getClass() != RuntimeException.class, "Exception " + e + " is not exactly RuntimeException");
failif( ! "Unspecified DirectorException message".equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
}
}
catch (Exception e) {

View file

@ -127,19 +127,28 @@ public class java_director_exception_feature_runme {
try { b.genericpong(1); fail("No exception thrown in genericpong(1)"); }
catch (MyJavaException1 e) {
failif( ! java_director_exception_feature_Consts.GENERICPONGEXCP1.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
StackTraceElement[] st = e.getStackTrace();
failif( st.length != 5, "Stack length is only " + st.length);
failif( ! st[0].toString().startsWith("java_director_exception_feature_MyFooDirectorImpl.genericpong(java_director_exception_feature_runme.java:"), "Incorrect top of stack: " + st[0]);
}
try { b.genericpong(2); fail("No exception thrown in genericpong(2)");}
catch (NewCheckedException e) {
failif( ! java_director_exception_feature_Consts.GENERICPONGEXCP2.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
StackTraceElement[] st = e.getStackTrace();
failif( st.length != 5, "Stack length is only " + st.length);
failif( ! st[0].toString().startsWith("java_director_exception_feature_MyFooDirectorImpl.genericpong(java_director_exception_feature_runme.java:"), "Incorrect top of stack: " + st[0]);
}
try { b.genericpong(3); fail("No exception thrown in genericpong(3)");}
catch (NewUncheckedException e) {
failif( ! java_director_exception_feature_Consts.GENERICPONGEXCP3.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
StackTraceElement[] st = e.getStackTrace();
failif( st.length != 5, "Stack length is only " + st.length);
failif( ! st[0].toString().startsWith("java_director_exception_feature_MyFooDirectorImpl.genericpong(java_director_exception_feature_runme.java:"), "Incorrect top of stack: " + st[0]);
}
try { b.genericpong(4); fail("No exception thrown in genericpong(4)");}
catch (RuntimeException e) {
failif ( e.getClass() != RuntimeException.class, "Exception " + e + " is not exactly RumtimeException");
failif( ! java_director_exception_feature_Consts.GENERICPONGEXCP4.equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
failif ( e.getClass() != RuntimeException.class, "Exception " + e + " is not exactly RuntimeException");
failif( ! "Unspecified DirectorException message".equals(e.getMessage()), "Expected exception has unexpected message: '" + e.getMessage() + "'");
}
}

View file

@ -42,7 +42,6 @@
%feature("director:except") MyNS::Foo::ping {
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
jenv->ExceptionClear(); // clear java exception since mapping to c++ exception
if (Swig::ExceptionMatches(jenv,$error,"$packagepath/MyJavaException1")) {
throw 1;
} else if (Swig::ExceptionMatches(jenv,$error,"$packagepath/MyJavaException2")) {
@ -71,7 +70,6 @@
%feature("director:except") MyNS::Foo::pong %{
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
jenv->ExceptionClear();
$directorthrowshandlers
throw ::MyNS::Unexpected(Swig::JavaExceptionMessage(jenv,$error).message());
}
@ -121,7 +119,10 @@
%feature("director:except") MyNS::Foo::genericpong {
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
jenv->ExceptionClear();
if (Swig::ExceptionMatches(jenv,$error,"UnconstructableException")) {
// Purposefully test NULL
throw Swig::DirectorException(jenv, NULL);
}
throw Swig::DirectorException(jenv,$error);
}
}
@ -131,9 +132,10 @@
%}
%feature ("except",throws="Exception") MyNS::Bar::genericpong %{
try { $action }
catch (Swig::DirectorException & direxcp) {
direxcp.raiseJavaException(jenv); // jenv always available in JNI code
try {
$action
} catch (Swig::DirectorException & direxcp) {
direxcp.throwException(jenv); // jenv always available in JNI code
return $null;
}
%}

View file

@ -49,7 +49,6 @@
%feature("director:except") MyNS::Foo::ping {
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
jenv->ExceptionClear(); // clear java exception since mapping to c++ exception
if (Swig::ExceptionMatches(jenv,$error,"$packagepath/MyNS/MyJavaException1")) {
throw 1;
} else if (Swig::ExceptionMatches(jenv,$error,"$packagepath/MyNS/MyJavaException2")) {
@ -78,7 +77,6 @@
%feature("director:except") MyNS::Foo::pong %{
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
jenv->ExceptionClear();
$directorthrowshandlers
throw ::MyNS::Unexpected(Swig::JavaExceptionMessage(jenv,$error).message());
}
@ -128,7 +126,10 @@
%feature("director:except") MyNS::Foo::genericpong {
jthrowable $error = jenv->ExceptionOccurred();
if ($error) {
jenv->ExceptionClear();
if (Swig::ExceptionMatches(jenv,$error,"java_director_exception_feature_nspace_UnconstructibleException")) {
// Purposefully test NULL
throw Swig::DirectorException(jenv, NULL);
}
throw Swig::DirectorException(jenv,$error);
}
}
@ -138,9 +139,10 @@
%}
%feature ("except",throws="Exception") MyNS::Bar::genericpong %{
try { $action }
catch (Swig::DirectorException & direxcp) {
direxcp.raiseJavaException(jenv); // jenv always available in JNI code
try {
$action
} catch (Swig::DirectorException & direxcp) {
direxcp.throwException(jenv); // jenv always available in JNI code
return $null;
}
%}