Quantcast

Change the return value of Java proxy class method.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Change the return value of Java proxy class method.

bogdan.ichim

Hello,


Sorry if this is a silly question, but I am new to Swig and currently kinda stuck.


What I am trying to do is wrap a C++ function that returns a std::vector to a Java method that returns an AbstractList. I have successfully done this by using the solution proposed here: http://stackoverflow.com/questions/10596052/stdvector-to-java-util-vector-code-generation-with-swig , so now I have a wrapper object for the std::vector that inherits from  java.util.AbstractList.


So currently my C++ function:


    class A {
    public:
        std::vector<int> testVector() { return std::vector<int>(10 , 1); }
    };


gets wrapped to Java as this:


    public intVector testVector() {
       return new intVector(java_AJNI.A_testVector(swigCPtr, this), true);
    }

where intVector extends java.util.AbstractList<Integer>.



This works fine but my problem is that I don't really want to expose the intVector to the user, so what I am trying to do is to modify the Java proxy class to look something like this:

   

  public AbstractList<Integer> testVector() {
    return ((AbstractList<Integer>) new intVector(java_AJNI.A_testVector(swigCPtr, this), true));
  }

For this I have tried using %feature("shadow") in order to modify the return value of the method and various typemaps, but with no success.

Any suggestions would be greatly appreciated.


Best regards,

Bogdan.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Change the return value of Java proxy class method.

William S Fulton


On 7 April 2017 at 09:23, Bogdan Ichim <[hidden email]> wrote:

Hello,


Sorry if this is a silly question, but I am new to Swig and currently kinda stuck.


What I am trying to do is wrap a C++ function that returns a std::vector to a Java method that returns an AbstractList. I have successfully done this by using the solution proposed here: http://stackoverflow.com/questions/10596052/stdvector-to-java-util-vector-code-generation-with-swig , so now I have a wrapper object for the std::vector that inherits from  java.util.AbstractList.


So currently my C++ function:


    class A {
    public:
        std::vector<int> testVector() { return std::vector<int>(10 , 1); }
    };


gets wrapped to Java as this:


    public intVector testVector() {
       return new intVector(java_AJNI.A_testVector(swigCPtr, this), true);
    }

where intVector extends java.util.AbstractList<Integer>.



This works fine but my problem is that I don't really want to expose the intVector to the user, so what I am trying to do is to modify the Java proxy class to look something like this:

   

  public AbstractList<Integer> testVector() {
    return ((AbstractList<Integer>) new intVector(java_AJNI.A_testVector(swigCPtr, this), true));
  }

For this I have tried using %feature("shadow") in order to modify the return value of the method and various typemaps, but with no success.


This requires typemaps. Use these typemaps before SWIG parses the A class:

 
 /*
// This is the default typemap from java.swg which will be overridden with more specific one below:
%typemap(javaout) SWIGTYPE {
    return new $&javaclassname($jnicall, true);
  }
*/
%typemap(javaout) std::vector<int> {
    return (AbstractList<Integer>)(new $&javaclassname($jnicall, true));
  }
%typemap(jstype) std::vector<int> "AbstractList<Integer>"

William


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...