How to extract all classes names and it's methods names from java program? | Core Java Forum
A
Asim Toor Posted on 25/12/2021

how to extract all classes names and it's methods names from java program?


Y
Yogesh Chawla Replied on 28/12/2021

Check this:

import java.lang.reflect.Method;

public class ExtractClassNameAndMethodName {

	public static void main(String[] args) {

		System.out.println("Class Name using getName() - "
				+ ExtractClassNameAndMethodName.class.getName());
		System.out.println("Class Name using getTypeName() - "
				+ ExtractClassNameAndMethodName.class.getTypeName());
		System.out.println("Class Name using getCanonicalName() -"
				+ ExtractClassNameAndMethodName.class.getCanonicalName());
		System.out.println("Inner Class Name - using getCanonicalName() -"
				+ ExtractClassNameAndMethodName.ABC.class.getCanonicalName());

		retrieveMethodNames();
	}

	// Inner Class
	public class ABC {
	}

	static void retrieveMethodNames() {

		Class<ExtractClassNameAndMethodName> obj = ExtractClassNameAndMethodName.class;

		// get list of all methods
		Method[] classMethods = obj.getMethods();

		// get name of every method present in the above list
		for (Method method : classMethods) {

			String methodName = method.getName();
			//System.out.println(methodName); //Will give Object class methods also
			String checkMethod = "sampleMethod";
			if (methodName.equals(checkMethod)) {
				System.out
						.println("Class ExtractClassNameAndMethodName contains"
								+ " method - " + methodName);
			}
		}

	}

	public String sampleMethod() {
		return "Sample Method";
	}

}