简体   繁体   English

通过SWIG处理Java中的C ++异常

[英]Handling C++ exceptions in Java via SWIG

I'm attempting to use SWIG to wrap a C++ class into a Java class. 我正在尝试使用SWIG将C ++类包装到Java类中。 This C++ class has a method that throws an exception. 这个C ++类有一个抛出异常的方法。

I have three goals, none of which are currently happening although I have followed the manual as I understand it: 我有三个目标,虽然我按照我的理解遵循了手册,但目前都没有这个目标:

  • To get the Java class to declare throws <exceptiontype> on the method that throws in C++ 要让Java类声明对引发C ++的方法throws <exceptiontype>
  • To get the SWIG generated Exception class to extend java.lang.Exception 获取SWIG生成的Exception类以扩展java.lang.Exception
  • To override Exception.getMessage() in the generated SWIG class. 在生成的SWIG类中重写Exception.getMessage()

It seems that the root of the problem seems that my typemap s are not being applied, as none of the above happens. 似乎问题的根源似乎没有应用我的typemap ,因为上述情况都不会发生。 What have I done wrong? 我做错了什么?

Minimal example is below. 最小的例子如下。 The C++ does not have to compile, I'm only interested in the generated Java. C ++不需要编译,我只对生成的Java感兴趣。 The class of the exception is irrelevant, and the code below uses IOException just because the documentation uses it. 异常的类是无关紧要的,下面的代码使用IOException只是因为文档使用它。 All code is adapted from examples here: 所有代码都是根据以下示例改编的:

C++ header file (test.h): C ++头文件(test.h):

#include <string>

class CustomException {
private:
  std::string message;
public:
  CustomException(const std::string& message) : message(msg) {}
  ~CustomException() {}
  std::string what() {
    return message;
  }
};

class Test {
public:
  Test() {}
  ~Test() {}
  void something() throw(CustomException) {};
};

SWIG .i file: SWIG .i文件:

%module TestModule
%{
#include "test.h"
%}

%include "std_string.i" // for std::string typemaps
%include "test.h"

// Allow C++ exceptions to be handled in Java
%typemap(throws, throws="java.io.IOException") CustomException {
  jclass excep = jenv->FindClass("java/io/IOException");
  if (excep)
    jenv->ThrowNew(excep, $1.what());
  return $null;
}

// Force the CustomException Java class to extend java.lang.Exception
%typemap(javabase) CustomException "java.lang.Exception";

// Override getMessage()
%typemap(javacode) CustomException %{
  public String getMessage() {
    return what();
  }
%}

When running this with swig -c++ -verbose -java test.i with SWIG 2.0.4, the exception class does not extend java.lang.Exception and none of the Java methods have a throws declaration. 当使用swig -c++ -verbose -java test.i和SWIG 2.0.4运行它时,异常类不会扩展java.lang.Exception并且没有任何Java方法具有throws声明。

You're going to kick yourself when you see this. 当你看到这个时,你会踢自己。 A fixed version of your SWIG interface is: SWIG界面的固定版本是:

%module TestModule
%{
#include "test.h"
%}

%include "std_string.i" // for std::string typemaps

// Allow C++ exceptions to be handled in Java
%typemap(throws, throws="java.io.IOException") CustomException {
  jclass excep = jenv->FindClass("java/io/IOException");
  if (excep)
    jenv->ThrowNew(excep, $1.what());
  return $null;
}

// Force the CustomException Java class to extend java.lang.Exception
%typemap(javabase) CustomException "java.lang.Exception";

// Override getMessage()
%typemap(javacode) CustomException %{
  public String getMessage() {
    return what();
  }
%}

%include "test.h"

Ie %include "test.h" comes after the typemaps you wish to apply to the classes in test.h, not before. %include "test.h" 您希望应用于test.h中的类的类型映射之后,而不是之前。 The typemaps need to have been seen by SWIG at the time the classes they apply to are first seen. SWIG在他们申请的课程首次出现时需要看到这些类型地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM