Its a bit tricky and there are a few libraries that can help, but basically...
- Look at your classpath
- If you are dealing with a directory, you can look for all files ending in .class
- If you are dealing with a jar, load the jar up and look for all files ending in .class
- Remove the .class from the end of the file, replace the "\" with "." and then you have the fully qualified classname.
If you have spring in your classpath, you can take advantage of them doing most of this already:
ArrayList<String> retval = new ArrayList<Class<?>>();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resolver);
String basePath = ClassUtils.convertClassNameToResourcePath("com.mypackage.to.search");
Resource[] resources;
try {
resources = resolver.getResources("classpath*:" + basePath + "/**/*.class");
} catch (IOException e) {
throw new AssertionError(e);
}
for (Resource resource : resources) {
MetadataReader reader;
try {
reader = readerFactory.getMetadataReader(resource);
} catch (IOException e) {
throw new AssertionError(e);
}
String className = reader.getClassMetadata().getClassName();
retval.add(className)
}
return retval;