1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// Test to check the exception classes in the throws attribute of the typemaps and except feature is working
%module java_throws
// Exceptions are chosen at random but are ones which have to have a try catch block to compile
%typemap(in, throws=" ClassNotFoundException") int num {
$1 = (int)$input;
}
%typemap(freearg, throws="InstantiationException ") int num "/*not written*/"
%typemap(argout, throws="CloneNotSupportedException ") int num "/*not written*/"
%typemap(check, throws="NoSuchFieldException") int num {
if ($input == 10) {
jenv->ExceptionClear();
jclass excep = jenv->FindClass("java/lang/NoSuchFieldException");
if (excep)
jenv->ThrowNew(excep, "Value of 10 not acceptable");
return $null;
}
}
// Duplicate exceptions should be removed from the generated throws clause
%typemap(out, throws="IllegalAccessException, NoSuchFieldException, CloneNotSupportedException ") short {
$result = (jshort)$1;
}
%inline %{
short full_of_exceptions(int num) {
return 0;
}
%}
%typemap(throws, throws="IllegalAccessException") int {
(void)$1;
jclass excep = jenv->FindClass("java/lang/IllegalAccessException");
if (excep) {
jenv->ThrowNew(excep, "Test exception");
}
return $null;
}
%inline %{
void throw_spec_function(int value) throw (int) { throw (int)0; }
%}
// Check newfree typemap throws attribute
%newobject makeTestClass;
%typemap(newfree, throws="NoSuchMethodException") TestClass* "/*not written*/"
%inline %{
class TestClass {
int x;
public:
TestClass(int xx) : x(xx) {}
};
TestClass* makeTestClass() { return new TestClass(1000); }
%}
// javain typemap throws attribute
// Will only compile if the fileFunction has a java.io.IOException throws clause as getCanonicalPath() throws this exception
%typemap(jstype) char* someFileArgument "java.io.File"
%typemap(javain, throws="java.io.IOException") char* someFileArgument "$javainput.getCanonicalPath()"
%inline %{
void fileFunction(char* someFileArgument) {}
%}
// javout typemap throws attribute
%typemap(javaout, throws="java.io.IOException") int {
int returnValue=$jnicall;
if (returnValue==0) throw new java.io.IOException("some IOException");
return returnValue;
}
%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)
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");
}
%}
|