1. Getting started
- 1.1. Installing Javonet
- 1.2. Activating Javonet
- 1.3. Adding References to .NET Libraries
- 1.4. XML Configuration File
- 1.5. Using the Javonet Fluent Interface
- 1.7. Introduction to Using .NET Back-end and UI Components in Java
2. Calling methods
- 2.1. Invoking Static Methods
- 2.2. Creating Instance and Calling Instance Methods
- 2.3. Calling Generic Methods
3. Working with .NET Objects
- 3.1. Creating Instance Of Generic Object
- 3.2. Extending the .NET Class in Java and Wrapping .NET Methods
4. Fields and Properties
- 4.1. Get/Set Values for Static Fields and Properties
- 4.2. Get/Set Values for Instance Fields and Properties
5. Methods Arguments
- 5.1. Passing Reference-Type Arguments
- 5.2. Passing Arguments by Reference with “ref” and “out” Keywords
- 5.3. Passing typeof(Type) as Method Argument
- 5.4. Calling Overloaded Method Passing Null Argument
6. Nested Types
7. Enums
8. Arrays and Collections
- 8.1. Arrays: Using Value-Type and Reference-Type Arrays
- 8.2. Working with .NET arrays and collections from Java with Javonet
9. Embeding UI controls
10. Referencing libraries
11. Off-line activation
12. Events and Delegates
13. Disposing and Garabage Collection
14. .NET Configuration Files (AppConfig, WebConfig)
15. Exceptions, Debugging and Testing
- 15.. Handling Activation Issues
- 15.1. Handling .NET Exceptions
- 15.2. How to debug .NET code called from Java
- 15.3. Debugging Javonet Enabled Application
16. Strongly-Typed Wrappers
17. Advanced Activation and Licensing
- 17.1. Runtime vs. Offline vs. Compile Time Activation
- 17.2. Project Activation Tool
- 17.6. Delegating Activation Server
18 Other usage scenarios
Passing Arguments by Reference with “ref” and “out” Keywords
Microsoft .NET allows you to create methods that expect arguments to be passed by reference. Passing by reference is introduced by explicitly setting ref or out keywords before argument type within the method definition. For example, void MyMethod(ref int arg1).
Note: Passing arguments by reference should not be confused with passing reference-type arguments described in the previous section. If you need just to pass another .NET object in method arguments, please see the previous section.
JavOnet allows you to pass by reference both .NET objects (NObject) or primitive types (String, Integer, Float, Boolean etc..). You can also pass arrays of these types like NObject[] or String[].
While passing variables by reference, the value of that variable might be modified within the method body. Read more about .NET ref and out keywords here:
- http://msdn.microsoft.com/en-us/library/14akc2c7.aspx
- http://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx
JavOnet allows you to invoke methods expecting “ref” or “out” arguments by providing two dedicated types: NRef and NOut. To pass the variable by reference, you must wrap the variable with the NRef or NOut class. For NObject and NObject[], you can wrap them with NRef or NOut directly. However because Java does not support passing by reference primitive types, both variables (like String, Integer, Boolean etc.) or arrays of them should be wrapped with the “AtomicReference<?>” class, and then wrapped with NRef or NOut object.
Example .NET object with expecting “ref” argument method
class RefExample { public void Method(ref int i) { i = i + 44; } }
Example of a “ref” argument method from Java using Javonet
NObject refEx = Javonet.New("RefExample"); //Wrap Java integer in AtomicReference to allow passing by reference AtomicReference<Integer> myInt = new AtomicReference<Integer>(10); refEx.invoke("Method",new NRef(myInt)); System.out.println(myInt.get()); //Output will display number "55" because int passed by reference has been modified within the method body.
Example of a Int32.TryParse (string arg, out int number) method from Java
String strNumber = "4"; AtomicReference<Integer> myInt=new AtomicReference<Integer>(); //NOut constructor with argument type is used because myInt has NULL value. Without specifying explicitly //argument type .NET would not be able to locate proper method to execute. if (Javonet.getType("Int32").invoke("TryParse",strNumber,new NOut(myInt,"System.Int32"))) { System.out.println(myInt.get()); }
Example of an “out” argument method to populate an array of custom .NET objects
class Item { public String ItemName { get; set; } } class PopulateItems { public void Populate(out Item[] items) { items = new Item[5]; for (int i=0; i<5; i++) { items[i]=new Item(); items[i].ItemName="Item"+i.ToString(); } } }
Example of a populate method passing items in an array with an “out” argument
The populate method expects to pass an items array with an “out” argument. Therefore the items array does not need to be initialized before calling the method, and after execution, the passed variable will be filled with items.
NObject populator = Javonet.New("PopulateItems"); //Wrap null Java array in atomic reference to pass the reference as out argument AtomicReference<NObject[]> items = new AtomicReference<NObject[]>(null); populator.invoke("Populate", new NOut(items,"Item[]")); //After execution of this method local items array variable will contain five items generated within Populate method body.