Using nested types
Nested types are the classes defined within other class or struct. Javonet is able to create the instance of nested type, pass nested type as Type argument, set nested type to field or property and use nested types as generic arguments for methods and classes.
More about nested types you can read in MSDN documentation: Nested Types
Assuming we have a custom JAR library with the following classes inside:
public class TestClass {
public TestClass() {
}
public static int MyStaticField;
public int MyInstanceField;
public static String SayHello(String name) {
return "Hello " + name;
}
public static int MethodExpectingPrimitiveInt(int arg) {
return arg * 2;
}
public static int MethodExpectingClassInteger(Integer arg) {
return arg * 2;
}
public int MultiplyByTwo(Integer arg) {
return arg * 2;
}
public <T> T MyGenericMethod(T arg1)
{
return arg1;
}
}
And
To reference nested type with full namespace the name of the nested class should be prefixed with namespace, name of parent class and "+" sign. For example the "Nested" class defined above could be access using following path "TestNamespace.Container+Nested". Following examples show how to initialize and work with nested types using full namespace.
Was this article helpful?