From 49af1907b82be03ee4848b415564bc1f1e26e6bd Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 12 Jan 2018 07:34:32 +0000 Subject: [PATCH] Add missing Java throws clause for interfaces when using the %interface family of macros. Fixes #1156. --- CHANGES.current | 4 ++++ .../test-suite/java/java_throws_runme.java | 22 +++++++++++++++++++ Examples/test-suite/java_throws.i | 17 ++++++++++++++ Source/Modules/java.cxx | 7 ++++-- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index f39960bc3..0aad216de 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.0.0 (in progress) =========================== +2018-01-12: wsfulton + [Java] Fix issue #1156. Add missing throws clause for interfaces when using the + %interface family of macros. + 2018-01-05: wsfulton Fix default arguments using expressions containing -> syntax error. Problem reported on swig-user mailing list. diff --git a/Examples/test-suite/java/java_throws_runme.java b/Examples/test-suite/java/java_throws_runme.java index 0365b69ed..16eab7cad 100644 --- a/Examples/test-suite/java/java_throws_runme.java +++ b/Examples/test-suite/java/java_throws_runme.java @@ -93,6 +93,28 @@ public class java_throws_runme { if (!pass) throw new RuntimeException("Test 6 failed"); + // Interface function + pass = false; + try { + InterfaceTestImpl iti = new InterfaceTestImpl(); + iti.imethod(true); + } + catch (MyException e) { pass = true; } + + if (!pass) + throw new RuntimeException("Test interface 1 failed"); + + pass = false; + try { + InterfaceTestImpl iti = new InterfaceTestImpl(); + iti.imethod(false); + pass = true; + } + catch (MyException e) { pass = false; } + + if (!pass) + throw new RuntimeException("Test interface 2 failed"); + // Global function pass = false; try { diff --git a/Examples/test-suite/java_throws.i b/Examples/test-suite/java_throws.i index c628a45e6..875bce0ff 100644 --- a/Examples/test-suite/java_throws.i +++ b/Examples/test-suite/java_throws.i @@ -135,6 +135,23 @@ JAVAEXCEPTION(FeatureTest::staticMethod) }; %} +%include +%interface_impl(InterfaceTest); +JAVAEXCEPTION(imethod) + +%inline %{ + struct InterfaceTest { + virtual void imethod(bool raise) = 0; + }; + + 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; diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index f759aaf5d..d2b25426a 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -2544,8 +2544,6 @@ public: Printf(imcall, ")"); Printf(function_code, ")"); - if (is_interface) - Printf(interface_class_code, ");\n"); // Transform return type used in JNI function (in intermediary class) to type used in Java wrapper function (in proxy class) if ((tm = Swig_typemap_lookup("javaout", n, "", 0))) { @@ -2603,6 +2601,11 @@ public: Swig_warning(WARN_JAVA_TYPEMAP_JAVAOUT_UNDEF, input_file, line_number, "No javaout typemap defined for %s\n", SwigType_str(t, 0)); } + if (is_interface) { + Printf(interface_class_code, ")"); + generateThrowsClause(n, interface_class_code); + Printf(interface_class_code, ";\n"); + } generateThrowsClause(n, function_code); Printf(function_code, " %s\n\n", tm ? (const String *) tm : empty_string); Printv(proxy_class_code, function_code, NIL);