diff --git a/Examples/test-suite/java/director_classes_runme.java b/Examples/test-suite/java/director_classes_runme.java new file mode 100644 index 000000000..34552b52a --- /dev/null +++ b/Examples/test-suite/java/director_classes_runme.java @@ -0,0 +1,193 @@ +/* +This test demonstrates director classes when the types are classes. +Shown are virtual function calls which use classes passed by: + - Value + - Reference + - Pointer +as both parameters and return values. +The test also demonstrates directors used with: + - method overloading + - default parameters +Note: Methods with default parameters that call up from C++ cannot call +the overloaded Java methods, see DefaultParms method. + +Expected output if PrintDebug enabled: +------------ Start ------------ +Base - Val(444.555) +Base - Ref(444.555) +Base - Ptr(444.555) +Base - FullyOverloaded(int 10) +Base - FullyOverloaded(bool 1) +Base - SemiOverloaded(int -678) +Base - SemiOverloaded(bool 1) +Base - DefaultParms(10, 2.2) +Base - DefaultParms(10, 1.1) +-------------------------------- +Derived - Val(444.555) +Derived - Ref(444.555) +Derived - Ptr(444.555) +Derived - FullyOverloaded(int 10) +Derived - FullyOverloaded(bool 1) +Derived - SemiOverloaded(int -678) +Base - SemiOverloaded(bool 1) +Derived - DefaultParms(10, 2.2) +Derived - DefaultParms(10, 1.1) +-------------------------------- +JavaDerived - Val(444.555) +JavaDerived - Ref(444.555) +JavaDerived - Ptr(444.555) +JavaDerived - FullyOverloaded(int 10) +JavaDerived - FullyOverloaded(bool True) +JavaDerived - SemiOverloaded(-678) +Base - SemiOverloaded(bool 1) +JavaDerived - DefaultParms(10, 2.2) +JavaDerived - DefaultParms(10, 1.1) +------------ Finish ------------ +*/ + + +import director_classes.*; + +public class director_classes_runme { + + static { + try { + System.loadLibrary("director_classes"); + } catch (UnsatisfiedLinkError e) { + 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); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + director_classes_runme r = new director_classes_runme(); + r.run(); + } + + void run() + { + if (director_classes.getPrintDebug()) System.out.println("------------ Start ------------ "); + + Caller myCaller = new Caller(); + + // test C++ base class + { + Base myBase = new Base(100.0); + makeCalls(myCaller, myBase); + myBase.delete(); + } + + if (director_classes.getPrintDebug()) System.out.println("--------------------------------"); + + // test vanilla C++ wrapped derived class + { + Base myBase = new Derived(200.0); + makeCalls(myCaller, myBase); + myBase.delete(); + } + + if (director_classes.getPrintDebug()) System.out.println("--------------------------------"); + + // test director / Java derived class + { + Base myBase = new JavaDerived(300.0); + makeCalls(myCaller, myBase); + myBase.delete(); + } + + if (director_classes.getPrintDebug()) System.out.println("------------ Finish ------------ "); + } + + void makeCalls(Caller myCaller, Base myBase) + { + myCaller.set(myBase); + + DoubleHolder dh = new DoubleHolder(444.555); + + // Class pointer, reference and pass by value tests + if (myCaller.ValCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed"); + if (myCaller.RefCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed"); + if (myCaller.PtrCall(dh).getVal() != dh.getVal()) throw new RuntimeException("failed"); + + // Fully overloaded method test (all methods in base class are overloaded) + if (!myCaller.FullyOverloadedCall(10).equals(myBase.getClass().getSimpleName() + "::FullyOverloaded(int)")) { + System.out.println(myCaller.FullyOverloadedCall(10) + "----" + (myBase.getClass().getSimpleName() + "::FullyOverloaded(int)")); + throw new RuntimeException("failed"); + } + if (!myCaller.FullyOverloadedCall(true).equals(myBase.getClass().getSimpleName() + "::FullyOverloaded(bool)")) throw new RuntimeException("failed"); + + // Semi overloaded method test (some methods in base class are overloaded) + if (!myCaller.SemiOverloadedCall(-678).equals(myBase.getClass().getSimpleName() + "::SemiOverloaded(int)")) throw new RuntimeException("failed"); + if (!myCaller.SemiOverloadedCall(true).equals("Base" + "::SemiOverloaded(bool)")) throw new RuntimeException("failed"); + + // Default parameters methods test + if (!(myCaller.DefaultParmsCall(10, 2.2)).equals(myBase.getClass().getSimpleName() + "::DefaultParms(int, double)")) throw new RuntimeException("failed"); + if (myBase instanceof JavaDerived) { // special handling for Java derived classes, there is no way to do this any other way + if (!myCaller.DefaultParmsCall(10).equals(myBase.getClass().getSimpleName() + "::DefaultParms(int, double)")) throw new RuntimeException("failed"); + } else { + if (!myCaller.DefaultParmsCall(10).equals(myBase.getClass().getSimpleName() + "::DefaultParms(int)")) throw new RuntimeException("failed"); + } + + myCaller.reset(); + } +} + + +class JavaDerived extends Base +{ + public JavaDerived(double dd) + { + super(dd); + } + + public DoubleHolder Val(DoubleHolder x) + { + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Val(" + x.getVal() + ")"); + return x; + } + public DoubleHolder Ref(DoubleHolder x) + { + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Ref(" + x.getVal() + ")"); + return x; + } + public DoubleHolder Ptr(DoubleHolder x) + { + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - Ptr(" + x.getVal() + ")"); + return x; + } + public String FullyOverloaded(int x) + { + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - FullyOverloaded(int " + x + ")"); + return "JavaDerived::FullyOverloaded(int)"; + } + public String FullyOverloaded(boolean x) + { + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - FullyOverloaded(bool " + x + ")"); + return "JavaDerived::FullyOverloaded(bool)"; + } + // Note no SemiOverloaded(bool x) method + public String SemiOverloaded(int x) + { + String ret = "JavaDerived::SemiOverloaded(int)"; + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - SemiOverloaded(" + x + ")"); + return ret; + } + public String DefaultParms(int x, double y) + { + String ret = "JavaDerived::DefaultParms(int, double)"; + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - DefaultParms(" + x + ", " + y + ")"); + return ret; + } + // Note the following method can never be called from unmanaged code. + // It is here only for code that calls it directly from managed code. + // But should always be defined to ensure behaviour is consistent + // independent of where DefaultParsms is called from (managed or unmanaged code). + // Note this method can never be called from unmanaged code + public String DefaultParms(int x) + { + if (director_classes.getPrintDebug()) System.out.println("JavaDerived - DefaultParms(" + x + ")"); + return DefaultParms(x, 1.1/*use C++ default here*/); + } +} + diff --git a/Examples/test-suite/java/director_primitives_runme.java b/Examples/test-suite/java/director_primitives_runme.java new file mode 100644 index 000000000..8dedfff05 --- /dev/null +++ b/Examples/test-suite/java/director_primitives_runme.java @@ -0,0 +1,138 @@ +/* + This test program shows a C# class JavaDerived inheriting from Base. Three types of classes are created + and the virtual methods called to demonstrate: + 1) Wide variety of primitive types + 2) Calling methods with zero, one or more parameters + 3) Director methods that are not overridden in C# + 4) Director classes that are not overridden at all in C#, ie non-director behaviour is as expected for director classes + 5) Inheritance hierarchy using director methods + 6) Return types working as well as parameters + + The Caller class is a tester class, which calls the virtual functions from C++. +*/ + +import director_primitives.*; + +public class director_primitives_runme { + + static { + try { + System.loadLibrary("director_primitives"); + } catch (UnsatisfiedLinkError e) { + 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); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + director_primitives_runme r = new director_primitives_runme(); + r.run(); + } + + void run() + { + if (director_primitives.getPrintDebug()) System.out.println("------------ Start ------------ "); + + Caller myCaller = new Caller(); + + // test C++ base class + { + Base myBase = new Base(100.0); + makeCalls(myCaller, myBase); + myBase.delete(); + } + + if (director_primitives.getPrintDebug()) System.out.println("--------------------------------"); + + // test vanilla C++ wrapped derived class + { + Base myBase = new Derived(200.0); + makeCalls(myCaller, myBase); + myBase.delete(); + } + + if (director_primitives.getPrintDebug()) System.out.println("--------------------------------"); + + // test director / C# derived class + { + Base myBase = new JavaDerived(300.0); + makeCalls(myCaller, myBase); + myBase.delete(); + } + + if (director_primitives.getPrintDebug()) System.out.println("------------ Finish ------------ "); + } + + void makeCalls(Caller myCaller, Base myBase) + { + myCaller.set(myBase); + + myCaller.NoParmsMethodCall(); + if (myCaller.BoolMethodCall(true) != true) throw new RuntimeException("failed"); + if (myCaller.BoolMethodCall(false) != false) throw new RuntimeException("failed"); + if (myCaller.IntMethodCall(-123) != -123) throw new RuntimeException("failed"); + if (myCaller.UIntMethodCall(123) != 123) throw new RuntimeException("failed"); + if (myCaller.FloatMethodCall((float)-123.456) != (float)-123.456) throw new RuntimeException("failed"); + if (!myCaller.CharPtrMethodCall("test string").equals("test string")) throw new RuntimeException("failed"); + if (!myCaller.ConstCharPtrMethodCall("another string").equals("another string")) throw new RuntimeException("failed"); + if (myCaller.EnumMethodCall(HShadowMode.HShadowHard) != HShadowMode.HShadowHard) throw new RuntimeException("failed"); + myCaller.ManyParmsMethodCall(true, -123, 123, (float)123.456, "test string", "another string", HShadowMode.HShadowHard); + myCaller.NotOverriddenMethodCall(); + + myCaller.reset(); + } +} + +class JavaDerived extends Base +{ + public JavaDerived(double dd) + { + super(dd); + } + + public void NoParmsMethod() + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - NoParmsMethod()"); + } + public boolean BoolMethod(boolean x) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - BoolMethod(" + x + ")"); + return x; + } + public int IntMethod(int x) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - IntMethod(" + x + ")"); + return x; + } + public long UIntMethod(long x) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - UIntMethod(" + x + ")"); + return x; + } + public float FloatMethod(float x) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - FloatMethod(" + x + ")"); + return x; + } + public String CharPtrMethod(String x) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - CharPtrMethod(" + x + ")"); + return x; + } + public String ConstCharPtrMethod(String x) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - ConstCharPtrMethod(" + x + ")"); + return x; + } + public HShadowMode EnumMethod(HShadowMode x) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - EnumMethod(" + x + ")"); + return x; + } + public void ManyParmsMethod(boolean b, int i, long u, float f, String c, String cc, HShadowMode h) + { + if (director_primitives.getPrintDebug()) System.out.println("JavaDerived - ManyParmsMethod(" + b + ", " + i + ", " + u + ", " + f + ", " + c + ", " + cc + ", " + h); + } +} +