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