Type mapping for int[] to List<int>

I managed to generate a Java class from my C++ implementation. For this I have a SubwordEncoder.i:

/* File : example.i */
%module encoder

#include "SubwordEncoder.h"

/* Let's just grab the original header file here */
%include "SubwordEncoder.h"

Where interface looks like this:

class SubwordEncoder {
    int* encode(char* decoded);
    char* decode(int* encoded);

The generated code comes our like this:

public class SubwordEncoder {
  private transient long swigCPtr;
  protected transient boolean swigCMemOwn;

  protected SubwordEncoder(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;

  protected static long getCPtr(SubwordEncoder obj) {
    return (obj == null) ? 0 : obj.swigCPtr;

  protected void finalize() {

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
      swigCPtr = 0;

  public SWIGTYPE_p_int encode(String decoded) {
    long cPtr = encoderJNI.SubwordEncoder_encode(swigCPtr, this, decoded);
    return (cPtr == 0) ? null : new SWIGTYPE_p_int(cPtr, false);

  public String decode(SWIGTYPE_p_int encoded) {
    return encoderJNI.SubwordEncoder_decode(swigCPtr, this, SWIGTYPE_p_int.getCPtr(encoded));

  public SubwordEncoder() {
    this(encoderJNI.new_SubwordEncoder(), true);


But is is possible to also get List<Integer>, ArrayList<int>, Iterable<int> or something similar from SWIG?

char* already gets converted to a Java String (from the docs) but what’s the simplest way to extend these mappings?

SWIG version is 4.0.0 (Ubuntu)

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

I’d change this interface and make use of C++ containers (or itereators/ranges, but that’s a little less well supported in SWIG). As of SWIG 3.1 (or maybe 4.x?) both std::vector and std::list should implement sensible Java interfaces and autobox primitives correctly. So your interface could become this: class SubwordEncoder { public: std::vector<int> encode(const std::vector<char>& decoded); std::vector<char> decode(const std::vector<int>& encoded); }; Which you can then wrap with this: /* File : example.i */ %module encoder %include <std_vector.i> %{ #include "SubwordEncoder.h" %} %template(IntVector) std::vector<int>; %template(CharVector) std::vector<char>; /* Let's just grab the original header file here */ %include "SubwordEncoder.h" This does two things.… Read more »