1. Getting started
2. Working with Types
3. Working with Instances
4. Working with Fields
5. Advanced Activation and Licensing
To call Java static methods from .NET you need to retrieve the Java type first. For that purpose you call Javonet.getType(…) and pass the class name with namespace as argument. That method will return JType as result which is a handle to the Java type. Now you can work on that type to access static members in example invoking any Java static method:
Sample Java code:
public class SampleJavaClass { public static String SayHello(String name) { return "Hello " + name; } }
Example how to use it from .NET:
static void Main(string[] args) { // Todo: activate Javonet and add reference to the JAR file first //Get Java type and call static method JType sampleType = Javonet.getType("SampleJavaClass"); //Calling static methods String res = sampleType.Invoke<String>("SayHello", "Student"); Console.WriteLine("Java static method 'SayHello' returned: " + res); }
You can also chain your execution into a single operation:
static void Main(string[] args) { // Todo: activate Javonet and add reference to the JAR file first //Get Java type and call static method String res = Javonet.getType("SampleJavaClass").Invoke<String>("SayHello", "Student"); Console.WriteLine("Java static method 'SayHello' returned: " + res); }
Passing Arguments
Javonet allows you to call any Java static method from .NET with or without arguments. If method has no arguments you just call it using …Invoke(“methodName”). If your Java method expects arguments you can pass them as arguments to Invoke method.
Any value-type argument like string, integer, long etc.. can be passed directly using .NET types which will be automatically converted into the Java counterpart. However you need to understand that Java differentiates value types and their class representation.
If Java method expects “int” or “Integer” it is different type of argument, whereas in .NET method with argument “int” and “Int32” is the same. Short lowercase arguments are primitive types transferring only value. Types written with Pascal casing are class representations of those types delivering also some functionality to operate on those types.
Therefore if your Java method expects class representation you can pass regular .NET variable, however if the method expects primitive argument you need to wrap your .NET variable in “JPrimitive” class.
Sample Java code:
public class SampleJavaClass { public static int MethodExpectingPrimitiveInt(int arg) { return arg*2; } public static int MethodExpectingClassInteger(Integer arg) { return arg*2; } }
Example how to use it from .NET:
static void Main(string[] args) { // Todo: activate Javonet and add reference to the JAR file first //Get Java type and call static method JType sampleType = Javonet.getType("SampleJavaClass"); int argValue=5; //Calling static method with primitive representation of value type argument int res = sampleType.Invoke<String>("MethodExpectingPrimitiveInt", new JPrimitive(argValue)); Console.WriteLine("Java static method 'MethodExpectingPrimitiveInt' returned: " + res); //Calling static method with class representation of value type argument int res = sampleType.Invoke<String>("MethodExpectingClassInteger", argValue); Console.WriteLine("Java static method 'MethodExpectingClassInteger' returned: " + res); }
If your Java method expects another Java class as argument, you just need to create instance of that class using Javonet or retrieve if from other field or method from Java side, store in JObject variable and pass as argument.
Retrieving Method Result
Javonet allows you to retrieve any result from Java static method. You can retrieve both value-types and complex reference types. Any value-type either defined as primitive type (i.e. int) or class representation of primitive type (i.e. Integer) will be auto converted into .NET counterpart and returned.
For class types you should expect that “…>Invoke(..)” will return the JObject variable which is representation of Java object handle.
To invoke method which returns result you should pass expected result as generic argument for “Invoke(…)” method.