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
This commit is contained in:
William S Fulton 2004-06-27 21:07:15 +00:00
commit 2fce1634c7
2 changed files with 93 additions and 1 deletions

View file

@ -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");
}
}

View file

@ -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");
}
%}