SlideShare a Scribd company logo
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

More Related Content

PDF
Java collections
PPT
Spsl v unit - final
PPTX
Basic java, java collection Framework and Date Time API
PDF
4java Basic Syntax
PDF
Java Collection Framework
PPT
Jhtp5 20 Datastructures
PDF
Core java pract_sem iii
Java collections
Spsl v unit - final
Basic java, java collection Framework and Date Time API
4java Basic Syntax
Java Collection Framework
Jhtp5 20 Datastructures
Core java pract_sem iii

What's hot (20)

PPTX
PDF
DCN Practical
PPT
C# Application program UNIT III
DOCX
Java collections notes
PPT
Oop lecture7
PPTX
Slick: Bringing Scala’s Powerful Features to Your Database Access
PDF
ESNext for humans - LvivJS 16 August 2014
PDF
Lecture 4 - Object Interaction and Collections
PDF
Collections in Java Notes
PPTX
Collections
PDF
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
PPTX
collection framework in java
PDF
Laporan Resmi Algoritma dan Struktur Data :
PPT
Xm lparsers
PPT
Collections Framework
DOCX
Java programs
DOCX
Parameterization is nothing but giving multiple input
PDF
Java OOP Programming language (Part 4) - Collection
PPTX
Sequelize
DCN Practical
C# Application program UNIT III
Java collections notes
Oop lecture7
Slick: Bringing Scala’s Powerful Features to Your Database Access
ESNext for humans - LvivJS 16 August 2014
Lecture 4 - Object Interaction and Collections
Collections in Java Notes
Collections
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
collection framework in java
Laporan Resmi Algoritma dan Struktur Data :
Xm lparsers
Collections Framework
Java programs
Parameterization is nothing but giving multiple input
Java OOP Programming language (Part 4) - Collection
Sequelize
Ad

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

PPT
Spsl vi unit final
PPTX
Tutorial on developing a Solr search component plugin
PPTX
11. session 11 functions and objects
PPTX
Developing a new Epsilon EMC driver
PDF
Local data storage for mobile apps
PDF
OOP Adventures with XOOPS
PPSX
Oop features java presentationshow
PDF
PPTX
Static analysis: Around Java in 60 minutes
PDF
oblect oriented programming language in java notes .pdf
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PDF
ZendCon2010 The Doctrine Project
PDF
spring-tutorial
PPT
Extractors & Implicit conversions
PPT
Chapter 7 - Defining Your Own Classes - Part II
DOCX
Android database tutorial
PDF
Metaprogramovanie #1
PPTX
Taxonomy of Scala
PPT
Java căn bản - Chapter7
PPTX
03 object-classes-pbl-4-slots
Spsl vi unit final
Tutorial on developing a Solr search component plugin
11. session 11 functions and objects
Developing a new Epsilon EMC driver
Local data storage for mobile apps
OOP Adventures with XOOPS
Oop features java presentationshow
Static analysis: Around Java in 60 minutes
oblect oriented programming language in java notes .pdf
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
ZendCon2010 The Doctrine Project
spring-tutorial
Extractors & Implicit conversions
Chapter 7 - Defining Your Own Classes - Part II
Android database tutorial
Metaprogramovanie #1
Taxonomy of Scala
Java căn bản - Chapter7
03 object-classes-pbl-4-slots
Ad

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