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