Java classes of a project by reflection

Java classes by reflection

Today I am gonna show you all java classes of a project. Some times we get a requirement that we have to show all java classes that are using in a project. These classes those are written by developer of project. Basically this post is showing a recursion java example to get all java classes of a project. Using javadoc we can see in html page but this post java classes will give you a spread sheet file which contains of all java classes. In this example there is a interface, called service, it has method that’s input/output class contains different class. In these input/output class contains some domain class.  Here I am using Member, PersonalDetails, Address etc class as domain class. Here is one example of Member class

Service Interface

CreateMemberInput class

Our target output like

all java classes of output

all java classes of domain

To get all java classes

Step of to get all java classes:

Step 1: getAllServiceNames(): read service interface

Step 2: getServiceDetails(): Make a list of service input and output

Step 3: getAllFields(): read a class and return a list of attributes. Please see ReflectionHelper class. You will see download link at bottom.
Step 4: writeSpredSheet : write all results to spread sheet file. Please see ReflectionHelper class.

Now lets see visitAllClass() method of ReflectionHelper class, this method is visiting recursively to all child java classes according to all parent class. Here is this method

In top method, I am using java reflection at line 11 and getting class details. In deed, this we are passing parent class name as a parameter of this method. At line 14, we are taking all attributes of this class. At 19, we are checking all fields so that we can take a decision is it java.lang object or our domain object. If it is our domain object then it calls itself see line 34.

If any class has same class reference then you will get java.lang.reflect.InvocationTargetException , to rid off from this error you should check, see line 33, the same class name should not pass as argument of visitAllClass method.

I am using google guava collection framework though you can remove this jar from project. It is a maven project you can remove from decency from pom file. To know maven please visit here. Another post of this blog is describing about maven.

Download

Click here to download this project.

Leave a Reply

Your email address will not be published. Required fields are marked *