【问题标题】:can't figure out how to make SWIG/Java force a proxy class to implement an interface不知道如何让 SWIG/Java 强制代理类实现接口
【发布时间】:2011-03-29 19:20:52
【问题描述】:

我正在使用 SWIG 将 c++ 类导出到 Java,但在尝试强制代理对象实现接口时遇到了问题。

我浏览了 SWIG 文档,发现您可以使用“%pragma(java) jniclassinterfaces=x”让 JNI 类实现给定接口,并使用“%pragma(java) moduleinterfaces=x”让模块实现任何给定的接口,但实际代理对象没有相应的编译指示。

我宁愿让 SWIG 生成“实现 X”代码,因为稍后尝试添加该实现很困难。例如,如果我尝试子类化 SWIG 代理然后实现接口,我会遇到问题,因为我也在使用泛型:

interface IVector<VectorType> {
   VectorType add(VectorType other);
   ...
}

所以这样的事情失败了:

class MyVector extends MyProxyVector implements IVector<MyVector> {
    MyVector add(MyVector other) {
        return (MyVector) super.add(other);
    }
}

因为它需要将父类强制转换为子类。

我能想出解决此问题的唯一其他方法是创建包装类或使用复制构造函数。两者似乎都有些低效,因为它们的全部目的是实现一个接口。

【问题讨论】:

    标签: java interface proxy swig


    【解决方案1】:

    这应该通过typemaps mechanism 处理。 以下代码:

    %module test
    
    %typemap(javainterfaces) Foo "SomeInterface"
    %typemap(javabase) Foo "SomeBase"
    
    struct Foo {
    };
    

    Foo 设置基础和接口,如下所示:

    public class Foo extends SomeBase implements SomeInterface {
    
    //...
    

    在生成的 Java 代理类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 2016-06-10
      • 1970-01-01
      相关资源
      最近更新 更多