通过SWIG在Java中处理C ++异常 [英] Handling C++ exceptions in Java via SWIG

查看:137
本文介绍了通过SWIG在Java中处理C ++异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用SWIG将C ++类包装到Java类中。这个C ++类有一个抛出异常的方法。



我有三个目标,没有一个目前正在发生,虽然我已经按照我的理解遵循手册: p>


  • 要让Java类在抛出的方法上声明 throws< exceptiontype> 在C ++中

  • 要使SWIG生成的Exception类扩展 java.lang.Exception

  • 在所生成的SWIG类中覆盖 Exception.getMessage()



似乎问题的根源似乎是我的 typemap s没有应用,因为没有上述发生。我做错了什么?



最少的例子如下。 C ++不需要编译,我只对生成的Java感兴趣。异常的类是不相关的,下面的代码使用IOException只是因为文档使用它。所有代码均来自此处的示例:





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

  #include< string> ; 

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

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

SWIG .i档案:

 %module TestModule 
%{
#includetest.h
%}

%includestd_string.i / for std :: string typemaps
%includetest.h

//允许在Java中处理C ++异常
%typemap(throws,throws =java。 io.IOException)CustomException {
jclass excep = jenv-> FindClass(java / io / IOException);
if(excep)
jenv-> ThrowNew(excep,$ 1.what());
return $ null;
}

//强制CustomException Java类扩展java.lang.Exception
%typemap(javabase)CustomExceptionjava.lang.Exception;

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



< swig -c ++ -verbose -java test.i 与SWIG 2.0.4,异常类不扩展 java.lang.Exception ,没有Java方法有一个 throws 声明。

解决方案

当你看到这一点时踢自己。您的SWIG界面的固定版本是:

 %module TestModule 
%{
#includetest .h
%}

%includestd_string.i// for std :: string typemaps

//允许在Java中处理C ++异常
%typemap(throws,throws =java.io.IOException)CustomException {
jclass excep = jenv-> FindClass(java / io / IOException);
if(excep)
jenv-> ThrowNew(excep,$ 1.what());
return $ null;
}

//强制CustomException Java类扩展java.lang.Exception
%typemap(javabase)CustomExceptionjava.lang.Exception;

//覆盖getMessage()
%typemap(javacode)CustomException%{
public String getMessage(){
return what
}
%}

%includetest.h

Ie %includetest.h来自之后您希望应用于test.h中类的类型映射,而不是之前。在他们申请的课程首次出现时,SWIG需要看到类型图。


I'm attempting to use SWIG to wrap a C++ class into a Java class. This C++ class has a method that throws an exception.

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++
  • To get the SWIG generated Exception class to extend java.lang.Exception
  • To override Exception.getMessage() in the generated SWIG class.

It seems that the root of the problem seems that my typemaps are not being applied, as none of the above happens. What have I done wrong?

Minimal example is below. The C++ does not have to compile, I'm only interested in the generated Java. The class of the exception is irrelevant, and the code below uses IOException just because the documentation uses it. All code is adapted from examples here:

C++ header file (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:

%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.

解决方案

You're going to kick yourself when you see this. A fixed version of your SWIG interface is:

%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"

I.e. %include "test.h" comes after the typemaps you wish to apply to the classes in test.h, not before. The typemaps need to have been seen by SWIG at the time the classes they apply to are first seen.

这篇关于通过SWIG在Java中处理C ++异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆