I need to generate java template code to access the nested object with structure.
for eg:
class Person{
private String name;
private List<Address> address;
}
class Address{
private String doorNo;
private String Address;
private City city;
}
class City{
....
}
So a method is required which will take any object(Nested) and generate the java code for above person object as follows.
//Generated java code should be like this
if(person != null){
String name = StringUtils.isEmpty(Person.getName());
if(person.getAdressList() != null && person.getAddressLit().isNotEmpty()){
for (Address address : person.getAdressList()) {
......
}
}
}
The method should be generic and generate code recursively for dynamic nested object.
Code generation is a complicated task and can't be fully answered in the Q&A Format of StackOverflow.
Keep in mind that while writing a function that "just" creates the method you describe here would take about a day or so. Maybe writing it from hand, though boring, is the more time efficient approach. (http://xkcd.com/1205/)
If you want to generate code you first have to pick a code generation tool that does what you need. There are several (maybe you can use the refactoring functions of your IDE as well). If you want to be able to manipulate the way the code is processed within the original source code, I have found the Java APT to be very useful. It will let you traverse packages
of Java code and generate classes and methods according to the structure found therein.
It is a tool that can interpret Annotations
to be able to determinate what to do with any single method.