javajava-native-interfaceswig

How to set SWIG generated java proxy class as final


I am using SWIG to generate a Java wrapper for some C code. This C code has a structure that SWIG represents with the below Java proxy class. As one can see, this class can be extended given that it is not declared as final. How can one declare this class as final using SWIG (i.e. public final class Test)?

/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 3.0.12
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */

package abc;

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

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

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

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        TestJNI.delete_Test(swigCPtr);
      }
      swigCPtr = 0;
    }
  }

  public Test() {
        this(TestJNI.new_Test(), true);
        TestJNI.initialize(swigCPtr, this);
    }

}

Solution

  • You can control this using a SWIG typemap:

    %typemap(javaclassmodifiers) struct Test "public final class"
    

    (or just Test if you enabled C++ processing with -c++)

    You can also pass SWIGTYPE instead of struct Test to do this for all classes.