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

package com.javaforloop.visitallclass.domain;

public class Member {
	private String id;
	private MemberStatus status;
	private PersonalDetails personalDetails;

	public String getId() {
		return id;

	public void setId(String id) { = id;

	public PersonalDetails getPersonalDetails() {
		return personalDetails;

	public void setPersonalDetails(PersonalDetails personalDetails) {
		this.personalDetails = personalDetails;

	public MemberStatus getStatus() {
		return status;

	public void setStatus(MemberStatus status) {
		this.status = status;

Service Interface

package com.javaforloop.visitallclass.service;

import com.javaforloop.visitallclass.input.CreateMemberInput;
import com.javaforloop.visitallclass.output.CreateMemberOutput;

public interface Service {
	public CreateMemberOutput createMember(CreateMemberInput request);

CreateMemberInput class

package com.javaforloop.visitallclass.input;

import com.javaforloop.visitallclass.domain.Member;
public class CreateMemberInput {
	Member [] members;
	public Member[] getMembers() {
		return members;

	public void setMembers(Member[] members) {
		this.members = members;

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

private Set<String> getAllServiceNames(String serviceClassName) {
		Set<String> classes = Sets.newHashSet();
		Map<String, List<String>> inputFields = new HashMap<String, List<String>>();
		Map<String, List<String>> outputFields = new HashMap<String, List<String>>();
		List<String[]> result = getServiceDetails(serviceClassName, "METHOD");
		// if you have any filter list, in this example we don't have any
		List<String> filterList = Lists.newArrayList("createMember");
		// print(result);
		for (String[] strings : result) {
			String service = strings[0];
			if (!filterList.contains(service)) {
			List<String> inputList = getAllFields(strings[1], false);
			inputFields.put(strings[0], inputList);
			// printList(inputList);
			List<String> inputDomainClassess = getAllFields(strings[1], true);

			List<String> outputList = getAllFields(strings[2], false);
			outputFields.put(strings[0], outputList);
			// printList(outputList);
			List<String> outputDomainClassess = getAllFields(strings[2], true);
		String fileName = "./services.csv";
		writeFile(fileName, getFileContent(inputFields, outputFields));
		return classes;

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

private List<String[]> getServiceDetails(String className, String type) {
		List<String[]> result = new ArrayList<String[]>();
		try {
			Class<?> c = Class.forName(className);
			// System.out.println(className);
			switch (ClassMember.valueOf(type)) {
			case METHOD:
				result = getMemberDetails(c.getMethods());
			case CLASS:
				// TODO
			case ALL:
				// TODO
		} catch (ClassNotFoundException cnfx) {
		return result;

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

	 * @param className
	 * @param domainOnly
	 * @return
	public List<String> visitAllClass(String className, boolean domainOnly) {
		// System.out.println("class name :"+className);
		List<String> list = new ArrayList<String>();
		try {
			Class<?> c = Class.forName(className);
			Field fields[] = null;
			try {
				fields = c.getDeclaredFields();
			} catch (Exception ex) {
			for (Field field : fields) {
				String targetValue = "";
				targetValue = field.getType().getSimpleName() + " "
						+ field.getName();
				if (domainOnly) {
					if (!field.getType().getName().contains("javaforloop")) {
					} else {
						targetValue = field.getType().getName();
						if (field.getType().isArray()) {
							targetValue = targetValue.substring(2,
									targetValue.length() - 1);
						// System.out.println(targetValue);
						if (!targetValue.trim().equals(className)) {
							list.addAll(visitAllClass(targetValue, true));
		} catch (ClassNotFoundException e) {
		return list;

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.


Click here to download this project.

Leave a Reply

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