PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/java/extend/runme.java

#
Java | 88 lines | 41 code | 19 blank | 28 comment | 0 complexity | d07115e05c365bd6c943e4513aad973e MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. // This file illustrates the cross language polymorphism using directors.
  2. // CEO class, which overrides Employee::getPosition().
  3. class CEO extends Manager {
  4. public CEO(String name) {
  5. super(name);
  6. }
  7. public String getPosition() {
  8. return "CEO";
  9. }
  10. // Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory.
  11. public void disownMemory() {
  12. swigCMemOwn = false;
  13. }
  14. }
  15. public class runme {
  16. static {
  17. try {
  18. System.loadLibrary("example");
  19. } catch (UnsatisfiedLinkError e) {
  20. 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);
  21. System.exit(1);
  22. }
  23. }
  24. public static void main(String argv[])
  25. {
  26. // Create an instance of CEO, a class derived from the Java proxy of the
  27. // underlying C++ class. The calls to getName() and getPosition() are standard,
  28. // the call to getTitle() uses the director wrappers to call CEO.getPosition().
  29. CEO e = new CEO("Alice");
  30. System.out.println( e.getName() + " is a " + e.getPosition() );
  31. System.out.println( "Just call her \"" + e.getTitle() + "\"" );
  32. System.out.println( "----------------------" );
  33. // Create a new EmployeeList instance. This class does not have a C++
  34. // director wrapper, but can be used freely with other classes that do.
  35. EmployeeList list = new EmployeeList();
  36. // EmployeeList owns its items, so we must surrender ownership of objects we add.
  37. e.disownMemory();
  38. list.addEmployee(e);
  39. System.out.println( "----------------------" );
  40. // Now we access the first four items in list (three are C++ objects that
  41. // EmployeeList's constructor adds, the last is our CEO). The virtual
  42. // methods of all these instances are treated the same. For items 0, 1, and
  43. // 2, all methods resolve in C++. For item 3, our CEO, getTitle calls
  44. // getPosition which resolves in Java. The call to getPosition is
  45. // slightly different, however, because of the overidden getPosition() call, since
  46. // now the object reference has been "laundered" by passing through
  47. // EmployeeList as an Employee*. Previously, Java resolved the call
  48. // immediately in CEO, but now Java thinks the object is an instance of
  49. // class Employee. So the call passes through the
  50. // Employee proxy class and on to the C wrappers and C++ director,
  51. // eventually ending up back at the Java CEO implementation of getPosition().
  52. // The call to getTitle() for item 3 runs the C++ Employee::getTitle()
  53. // method, which in turn calls getPosition(). This virtual method call
  54. // passes down through the C++ director class to the Java implementation
  55. // in CEO. All this routing takes place transparently.
  56. System.out.println( "(position, title) for items 0-3:" );
  57. System.out.println( " " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"" );
  58. System.out.println( " " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"" );
  59. System.out.println( " " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"" );
  60. System.out.println( " " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"" );
  61. System.out.println( "----------------------" );
  62. // Time to delete the EmployeeList, which will delete all the Employee*
  63. // items it contains. The last item is our CEO, which gets destroyed as well.
  64. list.delete();
  65. System.out.println( "----------------------" );
  66. // All done.
  67. System.out.println( "java exit" );
  68. }
  69. }