summaryrefslogtreecommitdiff
path: root/Examples/test-suite/java_throws.i
blob: 67d8d4ebce670f3ecdde8096d89a83b1cf9ed8f7 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Test to check the exception classes in the throws attribute of the typemaps and except feature is working

%module java_throws

// throw is invalid in C++17 and later, only SWIG to use it
#define TESTCASE_THROW1(T1) throw(T1)
%{
#define TESTCASE_THROW1(T1)
%}

// 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 %{
bool throw_spec_function(int value) TESTCASE_THROW1(int) { throw (int)0; }
%}

%catches(int) catches_function(int value);
%inline %{
bool catches_function(int value) { 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"
%typemap(javacode) MyException %{
  public static final long serialVersionUID = 0x52151000; // Suppress ecj warning
%}
%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");
        }
    };
%}

%include <swiginterface.i>
%interface_impl(InterfaceTest);
JAVAEXCEPTION(imethod)

%inline %{
    struct InterfaceTest {
        virtual void imethod(bool raise) = 0;
        virtual ~InterfaceTest() {}
    };

    struct InterfaceTestImpl : InterfaceTest {
        void imethod(bool raise) {
            if (raise)
                throw MyException("raise 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");
    }
%}

// Test %nojavaexception
%javaexception("MyException") %{
/* global exception handler */
try {
    $action
} catch (MyException) {
    jclass excep = jenv->FindClass("java_throws/MyException");
    if (excep)
        jenv->ThrowNew(excep, "exception message");
    return $null;
}
%}

%nojavaexception *::noExceptionPlease();
%nojavaexception NoExceptTest::NoExceptTest();

// Need to handle the checked exception in NoExceptTest.delete()
%typemap(javafinalize) SWIGTYPE %{
  @SuppressWarnings("deprecation")
  protected void finalize() {
    try {
      delete();
    } catch (MyException e) {
      throw new RuntimeException(e);
    }
  }
%}

%typemap(javabody) SWIGTYPE %{
  private transient long swigCPtr;
  protected transient boolean swigCMemOwn;

  protected $javaclassname(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr($javaclassname obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected static long swigRelease($javaclassname obj) {
    long ptr = 0;
    if (obj != null) {
      if (!obj.swigCMemOwn)
        throw new RuntimeException("Cannot release ownership as memory is not owned");
      ptr = obj.swigCPtr;
      obj.swigCMemOwn = false;
      try {
        obj.delete();
      } catch (MyException e) {
        throw new RuntimeException(e);
      }
    }
    return ptr;
  }
%}


%inline %{
struct NoExceptTest {
  unsigned int noExceptionPlease() { return 123; }
  unsigned int exceptionPlease() { return 456; }
  ~NoExceptTest() {}
};
%}

// Turn global exceptions off (for the implicit destructors/constructors)
%nojavaexception;