Java 如何更改swig生成的代理类中的特定方法实现?
我正在尝试使用director功能包装以下抽象类:Java 如何更改swig生成的代理类中的特定方法实现?,java,c++,abstract-class,swig,Java,C++,Abstract Class,Swig,我正在尝试使用director功能包装以下抽象类: Listener.h: class Listener { protected: void onEvent() = 0; } 我的界面文件如下所示: module(directors="1") Listener_Java feature("director") Listener; %{ #include "Listener.h" %} %include "Listener.h" protected void onEvent();
Listener.h:
class Listener {
protected:
void onEvent() = 0;
}
我的界面文件如下所示:
module(directors="1") Listener_Java
feature("director") Listener;
%{
#include "Listener.h"
%}
%include "Listener.h"
protected void onEvent();
生成的Java代理类中的结果方法如下所示:
...
protected void onEvent() {
Listener_JavaJNI.Listener_onEvent();
}
我想做的是将方法生成为无主体的抽象方法,如下所示:
module(directors="1") Listener_Java
feature("director") Listener;
%{
#include "Listener.h"
%}
%include "Listener.h"
protected void onEvent();
有什么简单的方法可以做到这一点吗?我想使方法抽象是强迫任何派生类重写此方法,因为如果它没有被覆盖,当在C++代码中的其他地方调用此事件时,会出现异常(因为它试图调用未实现的纯虚函数)< p>从:
为什么代理类没有声明为抽象类?为什么导演不来
代理类中声明为抽象的upcall方法
声明代理类及其抽象方法将破坏JNI
参数编组和SWIG的下行调用功能(从
java创建一个抽象java子类,继承C++的子类
而是启用了控制器类。使用前面的Foo类示例:
public abstract class UserVisibleFoo extends Foo {
/** Make sure user overrides this method, it's where the upcall
* happens.
*/
public abstract void method_upcall(Foo foo_object);
/// Downcast from Foo to UserVisibleFoo
public static UserVisibleFoo downcastUserVisibleFoo(Foo foo_object)
{
try {
return (foo_object != null ? (FooDerived) Foo.downcastFoo(foo_object) : null);
}
catch (ClassCastException exc) {
// Wasn't a FooDerived object, some other subclass of Foo
return null;
}
}
}
这不会阻止用户创建派生自的子类
然而,UserVisibleFoo提供了一个安全网,提醒
用户重写方法\u upcall()方法
有一个我以前写过的解决方案:您的解决方案看起来很有希望,我将在周日尝试。您在对您的答案的评论中提到“SWIG 3.1可能会大幅改变此答案”,您的意思是什么?我愿意使用3.1,因为它具有doxygen集成功能。我已经在文档中阅读了这一部分。JNI参数编组和SWIG的下拉功能(从java到C++)。-我的类不会使用AS参数,并且方法不会从java调用(它唯一的方法是C++,这就是为什么我需要导演),这就是为什么我要求解决方案。无论如何,谢谢你。