c12
c12

Reputation: 9827

Remove SWIGTYPE from Generated Class name

Is there anyway to remove the SWIGTYPE part from the generated class names and replace with another string literal?

i.e. change SWIGTYPE_p_ex_session.java to ex_session.java (strip off generated "SWIGTYPE_p_")

SWIG .i file:

%module Example
%{
#include "ExampleApi.h"
struct ex_session{};
%}

%include "ExampleApi.h"

ExampleApi.h contains the below:

typedef struct ex_session session_t;

Upvotes: 1

Views: 2880

Answers (1)

Assuming the real issue here is the "ugly name" then I'd solve this one by making sure SWIG has at least an empty definition (not just a declaration) visible when it's generating the wrapper, for example given:

%module test

class ExampleNiceName;

typedef ExampleNiceName example_t;

void func(example_t);

SWIG generates SWIGTYPE_p_ExampleNiceName.java

We can make this a whole lot better just by doing something like:

class ExampleNiceName {};

instead of

class ExampleNiceName;

in this example, which causes SWIG to generate ExampleNiceName.java as a wrapped type instead. It doesn't expose anything more than previously exposed and is perfectly legal/sane. We're telling SWIG not just "this type exists", but "this type exists and we'd like you to wrap nothing beyond it's name" by doing this.

You can also use %rename to make the name on the Java side different from the C++ class name, for example:

%rename(JavaClassName) ExampleNiceName;
class ExampleNiceName {};

with the previous example would cause JavaClassName to be used in place of ExampleNiceName in the generated Java.

Upvotes: 2

Related Questions