From 2fce1634c794bdb3b16ddd36c4751efd7e25e4a6 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sun, 27 Jun 2004 21:07:15 +0000 Subject: [PATCH] Test for %javaexception classes being added to a method's throws clause git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6003 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- .../test-suite/java/java_throws_runme.java | 33 ++++++++++ SWIG/Examples/test-suite/java_throws.i | 61 ++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/SWIG/Examples/test-suite/java/java_throws_runme.java b/SWIG/Examples/test-suite/java/java_throws_runme.java index b27eef11d..0ea435d62 100644 --- a/SWIG/Examples/test-suite/java/java_throws_runme.java +++ b/SWIG/Examples/test-suite/java/java_throws_runme.java @@ -55,6 +55,39 @@ public class java_throws_runme { if (!pass) throw new RuntimeException("Test 4 failed"); + + // Check except feature throws attribute... + // Static method + pass = false; + try { + FeatureTest.staticMethod(); + } + catch (MyException e) { pass = true; } + if (!pass) + throw new RuntimeException("Test 5 failed"); + + FeatureTest f = new FeatureTest(); + // Instance method + pass = false; + try { + f.method(); + } + catch (MyException e) { pass = true; } + + if (!pass) + throw new RuntimeException("Test 6 failed"); + + // Global function + pass = false; + try { + java_throws.globalFunction(10); + } + catch (MyException e) { pass = true; } + catch (ClassNotFoundException e) {} + catch (NoSuchFieldException e) {} + + if (!pass) + throw new RuntimeException("Test 7 failed"); } } diff --git a/SWIG/Examples/test-suite/java_throws.i b/SWIG/Examples/test-suite/java_throws.i index 15145b183..af5db84c5 100644 --- a/SWIG/Examples/test-suite/java_throws.i +++ b/SWIG/Examples/test-suite/java_throws.i @@ -1,4 +1,4 @@ -// Test to check the exception classes in the throws attribute of the typemaps is working +// Test to check the exception classes in the throws attribute of the typemaps and except feature is working %module java_throws @@ -14,6 +14,7 @@ jclass excep = jenv->FindClass("java/lang/NoSuchFieldException"); if (excep) jenv->ThrowNew(excep, "Value of 10 not acceptable"); + return $null; } } @@ -73,3 +74,61 @@ void fileFunction(char* someFileArgument) {} %inline %{ int ioTest() { return 0; } %} + +// except feature (%javaexception) specifying a checked exception class for the throws clause +%typemap(javabase) MyException "Throwable"; +%inline %{ + struct MyException { + MyException(const char *msg) {} + }; +%} + +%define JAVAEXCEPTION(METHOD) +%javaexception("MyException") METHOD %{ +try { + $action +} catch (MyException) { + jclass excep = jenv->FindClass("java_throws/MyException"); + if (excep) + jenv->ThrowNew(excep, "exception message"); + return $null; +} +%} +%enddef + +JAVAEXCEPTION(FeatureTest::FeatureTest) // doesn't seem to work +JAVAEXCEPTION(FeatureTest::method) +JAVAEXCEPTION(FeatureTest::staticMethod) + +%inline %{ + struct FeatureTest { + static void staticMethod() { + throw MyException("no message"); + } + void method() { + throw MyException("no message"); + } + }; +%} + +// Mixing except feature and typemaps when both generate a class for the throws clause +%typemap(in, throws="ClassNotFoundException") int both { + $1 = (int)$input; +} +%javaexception("MyException , NoSuchFieldException") globalFunction %{ +try { + $action +} catch (MyException) { + jclass excep = jenv->FindClass("java_throws/MyException"); + if (excep) + jenv->ThrowNew(excep, "exception message"); + return $null; +} +%} + +%inline %{ + void globalFunction(int both) { + throw MyException("no message"); + } +%} +