PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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