/tags/rel-1-3-25/SWIG/Examples/java/variables/main.java
Java | 97 lines | 78 code | 16 blank | 3 comment | 0 complexity | 7e9f27740951b933b122010054478ffb MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1// This example illustrates global variable access from Java. 2 3import java.lang.reflect.*; 4 5public class main { 6 static { 7 try { 8 System.loadLibrary("example"); 9 } catch (UnsatisfiedLinkError e) { 10 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); 11 System.exit(1); 12 } 13 } 14 15 public static void main(String argv[]) { 16 17// Try to set the values of some global variables 18 19 example.setIvar(42); 20 example.setSvar((short)-31000); 21 example.setLvar(65537); 22 example.setUivar(123456); 23 example.setUsvar(61000); 24 example.setUlvar(654321); 25 example.setScvar((byte)-13); 26 example.setUcvar((short)251); 27 example.setCvar('S'); 28 example.setFvar((float)3.14159); 29 example.setDvar(2.1828); 30 example.setStrvar("Hello World"); 31 example.setIptrvar(example.new_int(37)); 32 example.setPtptr(example.new_Point(37,42)); 33 example.setName("Bill"); 34 35 // Now print out the values of the variables 36 37 System.out.println( "Variables (values printed from Java)" ); 38 39 System.out.println( "ivar =" + example.getIvar() ); 40 System.out.println( "svar =" + example.getSvar() ); 41 System.out.println( "lvar =" + example.getLvar() ); 42 System.out.println( "uivar =" + example.getUivar() ); 43 System.out.println( "usvar =" + example.getUsvar() ); 44 System.out.println( "ulvar =" + example.getUlvar() ); 45 System.out.println( "scvar =" + example.getScvar() ); 46 System.out.println( "ucvar =" + example.getUcvar() ); 47 System.out.println( "fvar =" + example.getFvar() ); 48 System.out.println( "dvar =" + example.getDvar() ); 49 System.out.println( "cvar =" + (char)example.getCvar() ); 50 System.out.println( "strvar =" + example.getStrvar() ); 51 System.out.println( "cstrvar =" + example.getCstrvar() ); 52 System.out.println( "iptrvar =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(example.getIptrvar())) ); 53 System.out.println( "name =" + example.getName() ); 54 System.out.println( "ptptr =" + Long.toHexString(SWIGTYPE_p_Point.getCPtr(example.getPtptr())) + example.Point_print(example.getPtptr()) ); 55 System.out.println( "pt =" + Long.toHexString(SWIGTYPE_p_Point.getCPtr(example.getPt())) + example.Point_print(example.getPt()) ); 56 57 System.out.println( "\nVariables (values printed from C)" ); 58 59 example.print_vars(); 60 61 System.out.println( "\nNow I'm going to try and modify some read only variables" ); 62 63 System.out.println( " Trying to set 'path'" ); 64 try { 65 Method m = example.class.getDeclaredMethod("setPath", new Class[] {String.class}); 66 m.invoke(example.class, new Object[] {"Whoa!"} ); 67 System.out.println( "Hey, what's going on?!?! This shouldn't work" ); 68 } 69 catch (NoSuchMethodException e) { 70 System.out.println( "Good." ); 71 } 72 catch (Throwable t) { 73 System.out.println( "You shouldn't see this!" ); 74 } 75 76 System.out.println( " Trying to set 'status'" ); 77 try { 78 Method m = example.class.getDeclaredMethod("setStatus", new Class[] {Integer.class}); 79 m.invoke(example.class, new Object[] {new Integer(0)} ); 80 System.out.println( "Hey, what's going on?!?! This shouldn't work" ); 81 } 82 catch (NoSuchMethodException e) { 83 System.out.println( "Good." ); 84 } 85 catch (Throwable t) { 86 System.out.println( "You shouldn't see this!" ); 87 } 88 89 System.out.println( "\nI'm going to try and update a structure variable.\n" ); 90 91 example.setPt(example.getPtptr()); 92 93 System.out.println( "The new value is" ); 94 example.pt_print(); 95 System.out.println( "You should see the value" + example.Point_print(example.getPtptr()) ); 96 } 97}