Its a bit complicated, and there are several libraries that can help, but mostly ...
- Look at your class path
- If you are dealing with a directory, you can search for all files ending with .class
- If you are dealing with a jar, load the jar up and find all files ending with .class
- Remove .class from the end of the file, replace "\" with ".". and then you have the full class name.
If you have spring in your classpath, you can take advantage of them by 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;
source share