SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
APPLICATION TO DOCUMENT ALL THE
NECESSARY DETAILS OF JAVA CLASSES
OF A PROJECT AT ONCE INTO AN EXCEL
FILE
I hope ,you all have liked my earlier contribution ”MICROSOFT OUTLOOK CHECKER”. Now coming to
this one, let’s have a look on the following
Problem Definition:
 When I joined a new project, I saw that my teammates have almost completed the coding work.
It was the time for testing and documentation.
 Then I came to know that this documentation work required ample amount of time . No matter
how much you are skilled, you need to go through each and every java class of a project and
then need to find all the methods, their return types, all the variables used, their data types. All
this constitutes a hectic and time consuming work.
Solution Details:
To resolve all these problems, I have developed a JAVA PROGRAM that can document all the details
of java classes (i.e. class name , class methods and their return type, class variables and their data
type)of all the packages of a project at once into an excel file.
When you run this program, a single excel file is created in the same folder(package which stores this
java program) , with multiple worksheets and each sheet represents the single class and holds the name
of its class itself. This worksheet comprises of all the above mentioned details of a class.
On a single click, you can get the details of all the files at once. I hope that it will ease the task of
documentation and will save your precious time too.
My Sample Package includes:
Jar files :
 dom4j-1.6.1.jar
 poi-3.9-20121203.jar
 poi-ooxml-3.9-20121203.jar
 poi-ooxml-schemas-3.9-20121203.jar
 stax-api-1.0.1.jar
 xmlbeans-2.3.0.jar
Java programs:
 PlaceEncoder.java
 FoodEncoder.java
 MainProgram.java
The files which hold the details are
1. PlaceEncoder.java
2. FoodEncoder.java
The MainProgram.java contains the main code to fetch all the details from both files and it creates an
excel file “DetailsOfTheClassses.xlsx”
PlaceEncoder.java
package projectstests;
public class PlaceEncoder {
private String paris;
private int pariscode;
private float flightCharges;
public static void main(String[] args) {
}
public float getFlightCharges() {
return flightCharges;
}
public String getParis() {
return paris;
}
public int getPariscode() {
return pariscode;
}
public void setFlightCharges(float f) {
flightCharges = f;
}
public void setParis(String string) {
paris = string;
}
public void setPariscode(int i) {
pariscode = i;
}
}
FoodEncoder.java
package projectstests;
public class FoodEncoder {
private String biryani;
private String shahiPanir;
private int menuCode;
public static void main(String[] args) {
}
public String getBiryani() {
return biryani;
}
public int getMenuCode() {
return menuCode;
}
public String getShahiPanir() {
return shahiPanir;
}
public void setBiryani(String string) {
biryani = string;
}
public void setMenuCode(int i) {
menuCode = i;
}
public void setShahiPanir(String string) {
shahiPanir = string;
}
}
MainProgram.java
package projectstests;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class MainProgram {
/**
* @param args
* @throws IOException
* @throws InvalidFormatException
*/
private static void replace(Class secretClass, HSSFWorkbook workbook,
FileOutputStream out) throws InvalidFormatException, IOException {
// TODO Auto-generated method stub
// Obtaining the class name
String fileName = secretClass.getName();
// Assigning the class name to the worksheet
HSSFSheet sheet = workbook.createSheet(fileName);
// This data needs to be written (Object[])
Map<String, Object[]> data = new TreeMap<String, Object[]>();
data.put("1", new Object[] { "", "VARIABLE NAME", "DATA TYPE", "",
"METHOD NAME", "RETURN TYPE" });
// Obtaining all the details of the class
Method methods[] = secretClass.getDeclaredMethods();
Field fields[] = secretClass.getDeclaredFields();
int noOfFields = fields.length;
int noOfMethods = methods.length;
if (noOfMethods >= noOfFields) {
for (int i = 0; i < methods.length; i++)
{
String returntypes = methods[i].getReturnType().toString();
methods[i].setAccessible(true);
if ((noOfFields > 0)) {
fields[i].setAccessible(true);
String varReturntypes = fields[i].getType().toString();
data.put(String.valueOf(i + 2),
new Object[] { "", fields[i].getName(),
varReturntypes, "", methods[i].getName(),
returntypes });
} else {
data.put(String.valueOf(i + 2), new Object[] { "", "", "",
"", methods[i].getName(), returntypes });
}
noOfFields--;
}
}
else {
for (int i = 0; i < fields.length; i++) {
String varReturntypes = fields[i].getType().toString();
fields[i].setAccessible(true);
if (noOfMethods > 0) {
methods[i].setAccessible(true);
String returntypes = methods[i].getReturnType().toString();
data.put(String.valueOf(i + 2),
new Object[] { "", fields[i].getName(),
varReturntypes, "", methods[i].getName(),
returntypes });
} else {
data.put(String.valueOf(i + 2), new Object[] { "",
fields[i].getName(), varReturntypes, "", "", "" });
}
noOfMethods--;
}
}
// Iterate over data and write to sheet
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Integer)
cell.setCellValue((Integer) obj);
}
}
}
public static void main(String[] args)
{
try {
MainProgram mainProgram = new MainProgram();
// Creating the xls file with worksheet
FileOutputStream out = new FileOutputStream(new File(
"DetailsOfTheClassses.xls"));
HSSFWorkbook workbook = new HSSFWorkbook();
// Classname is PlaceEncoder
PlaceEncoderinstance = new PlaceEncoder ();
Class secretClass = instance.getClass();
mainProgram.replace(secretClass, workbook, out);
// Classname is FoodEncoder
FoodEncoderinstance1 = new FoodEncoder ();
secretClass = instance1.getClass();
mainProgram.replace(secretClass, workbook, out);
workbook.write(out);
out.close();
System.out
.println("DetailsOfTheClassses.xls written successfully on disk.");
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation of MainProgram.java
 To access an excel file, Apache POI provides certain easy-to-use APIs.
 I have used HSSFWorkbook and HSSFSheet class in this example in order to make it
work with .xls files.
 The method is coded in such a way that I have used only six columns ,namely
VARIABLE NAME DATA TYPE METHOD NAME RETURN TYPE
with two columns to represent spaces.
 Here the excel sheet will be initialized only once . The same instance of
workbook(worksheet) class is passed alongwith same excel sheet object and the
object of the class(which holds the details)everytime when the replace method is
called. createSheet method will create new worksheet everytime when the replace
method is called.
In the replace method,
 The classname is assigned to the worksheet
 Data variable is created with TREE MAP which holds the key values ,method name
and its return type, variable name and its data type. In the simpler terms, data
variable represents a row.
 From getDeclaredMethods() and getDeclaredFields(), we can get the details of
methods and variables used in the classes.
 After that , rows are created in the excel sheet, and the data is set in the cells using
setCellValue(String) and setCellValue(Integer).
If we want to find out the details of the multiple classes, we can add the code in the way as
we have done with these two following classes
// Classname is PlaceEncoder
PlaceEncoder instance = new PlaceEncoder();
Class secretClass = instance.getClass();
mainProgram.replace(secretClass, workbook, out);
// Classname is FoodEncoder
FoodEncoder instance1 = new FoodEncoder();
secretClass = instance1.getClass();
mainProgram.replace(secretClass, workbook, out);
Outputs (1 of 2)
Outputs (2 of 2)
FROM THE WRITER
It has been great that I am here again to share
something with you .I am looking forward for your
responses. Your feedback means a world to me. I
am so grateful to all those blogwriters whose blogs
have helped me . To think that I've been of some
help to others, is humbling and deeply gratifying.
Thank you!
DEEPANSHU GUPTA
deepanshu.a.gupta@accenture.com

Contenu connexe

Tendances

C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT IIIMinu Rajasekaran
 
Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access Rebecca Grenier
 
ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014Jan Jongboom
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsSyed Afaq Shah MACS CP
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Edureka!
 
collection framework in java
collection framework in javacollection framework in java
collection framework in javaMANOJ KUMAR
 
Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data : Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data : Siska Amelia
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
Java programs
Java programsJava programs
Java programsjojeph
 
Parameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple inputParameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple inputuanna
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionOUM SAOKOSAL
 

Tendances (20)

JDK 8
JDK 8JDK 8
JDK 8
 
DCN Practical
DCN PracticalDCN Practical
DCN Practical
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT III
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access
 
ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Collections
CollectionsCollections
Collections
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data : Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data :
 
Xm lparsers
Xm lparsersXm lparsers
Xm lparsers
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Java programs
Java programsJava programs
Java programs
 
Parameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple inputParameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple input
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
 
Sequelize
SequelizeSequelize
Sequelize
 

Similaire à APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE INTO AN EXCEL FILE

Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginsearchbox-com
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile appsIvano Malavolta
 
OOP Adventures with XOOPS
OOP Adventures with XOOPSOOP Adventures with XOOPS
OOP Adventures with XOOPSxoopsproject
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesStatic analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesAndrey Karpov
 
ZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine ProjectZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine ProjectJonathan Wage
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversionsKnoldus Inc.
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorialinfo_zybotech
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1Jano Suchal
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 

Similaire à APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE INTO AN EXCEL FILE (20)

Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component plugin
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Developing a new Epsilon EMC driver
Developing a new Epsilon EMC driverDeveloping a new Epsilon EMC driver
Developing a new Epsilon EMC driver
 
Local data storage for mobile apps
Local data storage for mobile appsLocal data storage for mobile apps
Local data storage for mobile apps
 
OOP Adventures with XOOPS
OOP Adventures with XOOPSOOP Adventures with XOOPS
OOP Adventures with XOOPS
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Specs2
Specs2Specs2
Specs2
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesStatic analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutes
 
ZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine ProjectZendCon2010 The Doctrine Project
ZendCon2010 The Doctrine Project
 
spring-tutorial
spring-tutorialspring-tutorial
spring-tutorial
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversions
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorial
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 

APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE INTO AN EXCEL FILE

  • 1. APPLICATION TO DOCUMENT ALL THE NECESSARY DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE INTO AN EXCEL FILE I hope ,you all have liked my earlier contribution ”MICROSOFT OUTLOOK CHECKER”. Now coming to this one, let’s have a look on the following Problem Definition:  When I joined a new project, I saw that my teammates have almost completed the coding work. It was the time for testing and documentation.  Then I came to know that this documentation work required ample amount of time . No matter how much you are skilled, you need to go through each and every java class of a project and then need to find all the methods, their return types, all the variables used, their data types. All this constitutes a hectic and time consuming work. Solution Details: To resolve all these problems, I have developed a JAVA PROGRAM that can document all the details of java classes (i.e. class name , class methods and their return type, class variables and their data type)of all the packages of a project at once into an excel file. When you run this program, a single excel file is created in the same folder(package which stores this java program) , with multiple worksheets and each sheet represents the single class and holds the name of its class itself. This worksheet comprises of all the above mentioned details of a class. On a single click, you can get the details of all the files at once. I hope that it will ease the task of documentation and will save your precious time too.
  • 2. My Sample Package includes: Jar files :  dom4j-1.6.1.jar  poi-3.9-20121203.jar  poi-ooxml-3.9-20121203.jar  poi-ooxml-schemas-3.9-20121203.jar  stax-api-1.0.1.jar  xmlbeans-2.3.0.jar Java programs:  PlaceEncoder.java  FoodEncoder.java  MainProgram.java The files which hold the details are 1. PlaceEncoder.java 2. FoodEncoder.java The MainProgram.java contains the main code to fetch all the details from both files and it creates an excel file “DetailsOfTheClassses.xlsx”
  • 3. PlaceEncoder.java package projectstests; public class PlaceEncoder { private String paris; private int pariscode; private float flightCharges; public static void main(String[] args) { } public float getFlightCharges() { return flightCharges; } public String getParis() { return paris; } public int getPariscode() { return pariscode; } public void setFlightCharges(float f) { flightCharges = f; } public void setParis(String string) { paris = string; } public void setPariscode(int i) { pariscode = i; } }
  • 4. FoodEncoder.java package projectstests; public class FoodEncoder { private String biryani; private String shahiPanir; private int menuCode; public static void main(String[] args) { } public String getBiryani() { return biryani; } public int getMenuCode() { return menuCode; } public String getShahiPanir() { return shahiPanir; } public void setBiryani(String string) { biryani = string; } public void setMenuCode(int i) { menuCode = i; } public void setShahiPanir(String string) { shahiPanir = string; } }
  • 5. MainProgram.java package projectstests; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public class MainProgram { /** * @param args * @throws IOException * @throws InvalidFormatException */ private static void replace(Class secretClass, HSSFWorkbook workbook, FileOutputStream out) throws InvalidFormatException, IOException { // TODO Auto-generated method stub // Obtaining the class name String fileName = secretClass.getName(); // Assigning the class name to the worksheet HSSFSheet sheet = workbook.createSheet(fileName); // This data needs to be written (Object[]) Map<String, Object[]> data = new TreeMap<String, Object[]>(); data.put("1", new Object[] { "", "VARIABLE NAME", "DATA TYPE", "", "METHOD NAME", "RETURN TYPE" }); // Obtaining all the details of the class Method methods[] = secretClass.getDeclaredMethods(); Field fields[] = secretClass.getDeclaredFields(); int noOfFields = fields.length; int noOfMethods = methods.length; if (noOfMethods >= noOfFields) { for (int i = 0; i < methods.length; i++) {
  • 6. String returntypes = methods[i].getReturnType().toString(); methods[i].setAccessible(true); if ((noOfFields > 0)) { fields[i].setAccessible(true); String varReturntypes = fields[i].getType().toString(); data.put(String.valueOf(i + 2), new Object[] { "", fields[i].getName(), varReturntypes, "", methods[i].getName(), returntypes }); } else { data.put(String.valueOf(i + 2), new Object[] { "", "", "", "", methods[i].getName(), returntypes }); } noOfFields--; } } else { for (int i = 0; i < fields.length; i++) { String varReturntypes = fields[i].getType().toString(); fields[i].setAccessible(true); if (noOfMethods > 0) { methods[i].setAccessible(true); String returntypes = methods[i].getReturnType().toString(); data.put(String.valueOf(i + 2), new Object[] { "", fields[i].getName(), varReturntypes, "", methods[i].getName(), returntypes }); } else { data.put(String.valueOf(i + 2), new Object[] { "", fields[i].getName(), varReturntypes, "", "", "" }); } noOfMethods--; } } // Iterate over data and write to sheet Set<String> keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object[] objArr = data.get(key); int cellnum = 0;
  • 7. for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); if (obj instanceof String) cell.setCellValue((String) obj); else if (obj instanceof Integer) cell.setCellValue((Integer) obj); } } } public static void main(String[] args) { try { MainProgram mainProgram = new MainProgram(); // Creating the xls file with worksheet FileOutputStream out = new FileOutputStream(new File( "DetailsOfTheClassses.xls")); HSSFWorkbook workbook = new HSSFWorkbook(); // Classname is PlaceEncoder PlaceEncoderinstance = new PlaceEncoder (); Class secretClass = instance.getClass(); mainProgram.replace(secretClass, workbook, out); // Classname is FoodEncoder FoodEncoderinstance1 = new FoodEncoder (); secretClass = instance1.getClass(); mainProgram.replace(secretClass, workbook, out); workbook.write(out); out.close(); System.out .println("DetailsOfTheClassses.xls written successfully on disk."); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
  • 8. Explanation of MainProgram.java  To access an excel file, Apache POI provides certain easy-to-use APIs.  I have used HSSFWorkbook and HSSFSheet class in this example in order to make it work with .xls files.  The method is coded in such a way that I have used only six columns ,namely VARIABLE NAME DATA TYPE METHOD NAME RETURN TYPE with two columns to represent spaces.  Here the excel sheet will be initialized only once . The same instance of workbook(worksheet) class is passed alongwith same excel sheet object and the object of the class(which holds the details)everytime when the replace method is called. createSheet method will create new worksheet everytime when the replace method is called. In the replace method,  The classname is assigned to the worksheet  Data variable is created with TREE MAP which holds the key values ,method name and its return type, variable name and its data type. In the simpler terms, data variable represents a row.  From getDeclaredMethods() and getDeclaredFields(), we can get the details of methods and variables used in the classes.  After that , rows are created in the excel sheet, and the data is set in the cells using setCellValue(String) and setCellValue(Integer). If we want to find out the details of the multiple classes, we can add the code in the way as we have done with these two following classes // Classname is PlaceEncoder PlaceEncoder instance = new PlaceEncoder(); Class secretClass = instance.getClass(); mainProgram.replace(secretClass, workbook, out); // Classname is FoodEncoder FoodEncoder instance1 = new FoodEncoder(); secretClass = instance1.getClass(); mainProgram.replace(secretClass, workbook, out);
  • 11. FROM THE WRITER It has been great that I am here again to share something with you .I am looking forward for your responses. Your feedback means a world to me. I am so grateful to all those blogwriters whose blogs have helped me . To think that I've been of some help to others, is humbling and deeply gratifying. Thank you! DEEPANSHU GUPTA deepanshu.a.gupta@accenture.com