Counting Classes and Methods in a package
Posted by Uday Shankar on Friday, October 3, 2014 Under: Java
This is just a utility i created joining these blog posts:
How can I count the number of methods in a Java class ?
and
Get All Classes Within A Package
How can I count the number of methods in a Java class ?
and
Get All Classes Within A Package
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* Counts the number of classes and methods in a package.
*/
public class CountPackageMethods {
/**
* Scans all classes accessible from the context class loader which belong to the given package and subpackages.
*
* @param packageName The base package
* @return The classes
* @throws ClassNotFoundException
* @throws java.io.IOException
*/
private static Class[] getClasses(String packageName)
throws ClassNotFoundException, IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
ArrayList<Class> classes = new ArrayList<Class>();
for (File directory : dirs) {
classes.addAll(findClasses(directory, packageName));
}
return classes.toArray(new Class[classes.size()]);
}
/**
* Recursive method used to find all classes in a given directory and subdirs.
*
* @param directory The base directory
* @param packageName The package name for classes found inside the base directory
* @return The classes
* @throws ClassNotFoundException
*/
private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>();
if (!directory.exists()) {
return classes;
}
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
assert !file.getName().contains(".");
classes.addAll(findClasses(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
}
}
return classes;
}
public static void main(String args[]){
int noOfClasses= 0, noOfMethods = 0;
try{
Class[] classes = getClasses(args[0]);
noOfClasses = classes.length;
for ( Class className : classes) {
Method[] methods= className.getMethods();
System.out.println("Number of methods in "+className+" = "+methods.length);
noOfMethods += methods.length;
}
}catch(ClassNotFoundException classNotFoundException){
System.out.println("Class could not be found!");
} catch (IOException ioe) {
System.out.println("Package could not be found!");
} catch (ArrayIndexOutOfBoundsException aiob) {
System.out.println("Please provide a package name in the arguments");
}
System.out.println("No of Classes" + noOfClasses);
System.out.println("No of Methods" + noOfMethods);
}
}
In : Java
Tags: reflexion class method count java
div id="disqus_thread">
A curious software designer and developer.