PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/java/director_classes_runme.java

#
Java | 207 lines | 121 code | 26 blank | 60 comment | 29 complexity | eed2fa40496b974087c44060b171f4e7 MD5 | raw file
  1/*
  2This test demonstrates director classes when the types are classes.
  3Shown are virtual function calls which use classes passed by:
  4  - Value
  5  - Reference
  6  - Pointer
  7as both parameters and return values.
  8The test also demonstrates directors used with:
  9  - method overloading
 10  - default parameters
 11Note: Methods with default parameters that call up from C++ cannot call
 12the overloaded Java methods, see DefaultParms method.
 13
 14Expected output if PrintDebug enabled:
 15------------ Start ------------
 16Base - Val(444.555)
 17Base - Ref(444.555)
 18Base - Ptr(444.555)
 19Base - FullyOverloaded(int 10)
 20Base - FullyOverloaded(bool 1)
 21Base - SemiOverloaded(int -678)
 22Base - SemiOverloaded(bool 1)
 23Base - DefaultParms(10, 2.2)
 24Base - DefaultParms(10, 1.1)
 25--------------------------------
 26Derived - Val(444.555)
 27Derived - Ref(444.555)
 28Derived - Ptr(444.555)
 29Derived - FullyOverloaded(int 10)
 30Derived - FullyOverloaded(bool 1)
 31Derived - SemiOverloaded(int -678)
 32Base - SemiOverloaded(bool 1)
 33Derived - DefaultParms(10, 2.2)
 34Derived - DefaultParms(10, 1.1)
 35--------------------------------
 36JavaDerived - Val(444.555)
 37JavaDerived - Ref(444.555)
 38JavaDerived - Ptr(444.555)
 39JavaDerived - FullyOverloaded(int 10)
 40JavaDerived - FullyOverloaded(bool True)
 41JavaDerived - SemiOverloaded(-678)
 42Base - SemiOverloaded(bool 1)
 43JavaDerived - DefaultParms(10, 2.2)
 44JavaDerived - DefaultParms(10, 1.1)
 45------------ Finish ------------
 46*/
 47
 48
 49import director_classes.*;
 50
 51public class director_classes_runme {
 52
 53  static {
 54    try {
 55        System.loadLibrary("director_classes");
 56    } catch (UnsatisfiedLinkError e) {
 57      System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
 58      System.exit(1);
 59    }
 60  }
 61
 62  public static void main(String argv[]) throws Throwable
 63  {
 64    director_classes_runme r = new director_classes_runme();
 65    r.run();
 66  }
 67
 68  void run()
 69  {
 70    if (director_classes.getPrintDebug()) System.out.println("------------ Start ------------ ");
 71
 72    Caller myCaller = new Caller();
 73
 74    // test C++ base class
 75    {
 76      Base myBase = new Base(100.0);
 77      makeCalls(myCaller, myBase);
 78      myBase.delete();
 79    }
 80
 81    if (director_classes.getPrintDebug()) System.out.println("--------------------------------");
 82
 83    // test vanilla C++ wrapped derived class
 84    {
 85      Base myBase = new Derived(200.0);
 86      makeCalls(myCaller, myBase);
 87      myBase.delete();
 88    }
 89
 90    if (director_classes.getPrintDebug()) System.out.println("--------------------------------");
 91
 92    // test director / Java derived class
 93    {
 94      Base myBase = new JavaDerived(300.0);
 95      makeCalls(myCaller, myBase);
 96      myBase.delete();
 97    }
 98
 99    if (director_classes.getPrintDebug()) System.out.println("------------ Finish ------------ ");
100  }
101
102  void makeCalls(Caller myCaller, Base myBase)
103  {
104    String baseSimpleName = getSimpleName(myBase.getClass());
105
106    myCaller.set(myBase);
107
108    DoubleHolder dh = new DoubleHolder(444.555);
109
110    // Class pointer, reference and pass by value tests
111    if (myCaller.ValCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed");
112    if (myCaller.RefCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed");
113    if (myCaller.PtrCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed");
114
115    // Fully overloaded method test (all methods in base class are overloaded)
116    if (!myCaller.FullyOverloadedCall(10).equals(baseSimpleName + "::FullyOverloaded(int)")) {
117      System.out.println(myCaller.FullyOverloadedCall(10) + "----" + (baseSimpleName + "::FullyOverloaded(int)"));
118      throw new RuntimeException("failed");
119    }
120    if (!myCaller.FullyOverloadedCall(true).equals(baseSimpleName + "::FullyOverloaded(bool)")) throw new RuntimeException("failed");
121
122    // Semi overloaded method test (some methods in base class are overloaded)
123    if (!myCaller.SemiOverloadedCall(-678).equals(baseSimpleName + "::SemiOverloaded(int)")) throw new RuntimeException("failed");
124    if (!myCaller.SemiOverloadedCall(true).equals("Base" + "::SemiOverloaded(bool)")) throw new RuntimeException("failed");
125
126    // Default parameters methods test
127    if (!(myCaller.DefaultParmsCall(10, 2.2)).equals(baseSimpleName + "::DefaultParms(int, double)")) throw new RuntimeException("failed");
128    if (myBase instanceof JavaDerived) { // special handling for Java derived classes, there is no way to do this any other way
129      if (!myCaller.DefaultParmsCall(10).equals(baseSimpleName + "::DefaultParms(int, double)")) throw new RuntimeException("failed");
130    } else {
131      if (!myCaller.DefaultParmsCall(10).equals(baseSimpleName + "::DefaultParms(int)")) throw new RuntimeException("failed");
132    }
133
134    myCaller.reset();
135  }
136
137  // Same as Class.getSimpleName() which is not present in all jdks
138  static String getSimpleName(Class klass) {
139    String fullName = klass.getName();
140    Package packag = klass.getPackage();
141    String simpleName = null;
142    if (packag != null)
143        simpleName = fullName.replaceAll(packag.getName() + ".", "");
144    else
145        simpleName = fullName;
146    return simpleName;
147  }
148}
149
150
151class JavaDerived extends Base
152{
153  public JavaDerived(double dd)
154  {
155    super(dd);
156  }
157
158  public DoubleHolder Val(DoubleHolder x)
159  {
160    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Val(" + x.getVal() + ")");
161    return x;
162  }
163  public DoubleHolder Ref(DoubleHolder x)
164  {
165    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Ref(" + x.getVal() + ")");
166    return x;
167  }
168  public DoubleHolder Ptr(DoubleHolder x)
169  {
170    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Ptr(" + x.getVal() + ")");
171    return x;
172  }
173  public String FullyOverloaded(int x)
174  {
175    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - FullyOverloaded(int " + x + ")");
176    return "JavaDerived::FullyOverloaded(int)";
177  }
178  public String FullyOverloaded(boolean x)
179  {
180    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - FullyOverloaded(bool " + x + ")");
181    return "JavaDerived::FullyOverloaded(bool)";
182  }
183  // Note no SemiOverloaded(bool x) method
184  public String SemiOverloaded(int x)
185  {
186    String ret = "JavaDerived::SemiOverloaded(int)";
187    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - SemiOverloaded(" + x + ")");
188    return ret;
189  }
190  public String DefaultParms(int x, double y)
191  {
192    String ret = "JavaDerived::DefaultParms(int, double)";
193    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - DefaultParms(" + x + ", " + y + ")");
194    return ret;
195  }
196  // Note the following method can never be called from unmanaged code.
197  // It is here only for code that calls it directly from managed code.
198  // But should always be defined to ensure behaviour is consistent
199  // independent of where DefaultParsms is called from (managed or unmanaged code).
200  // Note this method can never be called from unmanaged code
201  public String DefaultParms(int x)
202  {
203    if (director_classes.getPrintDebug()) System.out.println("JavaDerived - DefaultParms(" + x + ")");
204    return DefaultParms(x, 1.1/*use C++ default here*/);
205  }
206}
207