PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Examples/test-suite/java_throws.i

#
Swig | 142 lines | 121 code | 21 blank | 0 comment | 0 complexity | 28ac33cd33b2a2cdd2fd96d4711c9322 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1// Test to check the exception classes in the throws attribute of the typemaps and except feature is working
  2
  3%module java_throws
  4
  5// Exceptions are chosen at random but are ones which have to have a try catch block to compile
  6%typemap(in, throws="	 ClassNotFoundException") int num { 
  7    $1 = (int)$input;
  8}
  9%typemap(freearg, throws="InstantiationException  ") int num "/*not written*/"
 10%typemap(argout, throws="CloneNotSupportedException		") int num "/*not written*/"
 11%typemap(check, throws="NoSuchFieldException") int num {
 12    if ($input == 10) {
 13        jenv->ExceptionClear();
 14        jclass excep = jenv->FindClass("java/lang/NoSuchFieldException");
 15        if (excep)
 16            jenv->ThrowNew(excep, "Value of 10 not acceptable");
 17        return $null;
 18    }
 19}
 20
 21// Duplicate exceptions should be removed from the generated throws clause
 22%typemap(out, throws="IllegalAccessException, NoSuchFieldException,   CloneNotSupportedException    ") short { 
 23    $result = (jshort)$1; 
 24}
 25
 26%inline %{
 27short full_of_exceptions(int num) {
 28    return 0;
 29}
 30%}
 31
 32
 33%typemap(throws, throws="IllegalAccessException") int {
 34    (void)$1;
 35    jclass excep = jenv->FindClass("java/lang/IllegalAccessException");
 36    if (excep) {
 37        jenv->ThrowNew(excep, "Test exception");
 38    }
 39    return $null;
 40}
 41%inline %{
 42#if defined(_MSC_VER)
 43  #pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
 44#endif
 45void throw_spec_function(int value) throw (int) { throw (int)0; }
 46#if defined(_MSC_VER)
 47  #pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
 48#endif
 49%}
 50
 51
 52// Check newfree typemap throws attribute
 53%newobject makeTestClass;
 54%typemap(newfree, throws="NoSuchMethodException") TestClass* "/*not written*/"
 55%inline %{
 56class TestClass {
 57    int x;
 58public:
 59    TestClass(int xx) : x(xx) {}
 60};
 61TestClass* makeTestClass() { return new TestClass(1000); }
 62%}
 63
 64
 65// javain typemap throws attribute
 66// Will only compile if the fileFunction has a java.io.IOException throws clause as getCanonicalPath() throws this exception
 67%typemap(jstype) char* someFileArgument "java.io.File"
 68%typemap(javain, throws="java.io.IOException") char* someFileArgument "$javainput.getCanonicalPath()"
 69
 70%inline %{
 71void fileFunction(char* someFileArgument) {}
 72%}
 73
 74
 75// javout typemap throws attribute
 76%typemap(javaout, throws="java.io.IOException") int {
 77    int returnValue=$jnicall;
 78    if (returnValue==0) throw new java.io.IOException("some IOException");
 79    return returnValue;
 80  }
 81
 82%inline %{
 83int ioTest() { return 0; }
 84%}
 85
 86// except feature (%javaexception) specifying a checked exception class for the throws clause
 87%typemap(javabase) MyException "Throwable";
 88%inline %{
 89    struct MyException {
 90        MyException(const char *msg) {}
 91    };
 92%}
 93
 94%define JAVAEXCEPTION(METHOD)
 95%javaexception("MyException") METHOD %{
 96try {
 97    $action
 98} catch (MyException) {
 99    jclass excep = jenv->FindClass("java_throws/MyException");
100    if (excep)
101        jenv->ThrowNew(excep, "exception message");
102    return $null;
103}
104%}
105%enddef
106
107JAVAEXCEPTION(FeatureTest::FeatureTest)
108JAVAEXCEPTION(FeatureTest::method)
109JAVAEXCEPTION(FeatureTest::staticMethod)
110
111%inline %{
112    struct FeatureTest {
113        static void staticMethod() {
114            throw MyException("no message");
115        }
116        void method() {
117            throw MyException("no message");
118        }
119    };
120%}
121
122// Mixing except feature and typemaps when both generate a class for the throws clause
123%typemap(in, throws="ClassNotFoundException") int both { 
124    $1 = (int)$input;
125}
126%javaexception("MyException , NoSuchFieldException") globalFunction %{
127try {
128    $action
129} catch (MyException) {
130    jclass excep = jenv->FindClass("java_throws/MyException");
131    if (excep)
132        jenv->ThrowNew(excep, "exception message");
133    return $null;
134}
135%}
136
137%inline %{
138    void globalFunction(int both) {
139        throw MyException("no message");
140    }
141%}
142