/** * Recursively add all .class files from the directory to the list. * * @param directory Directory from which we are adding files * @param files List we add files to */ private void addAllClassFiles(File directory, List<File> files) { for (File file : directory.listFiles()) { if (file.isDirectory()) { addAllClassFiles(file, files); } else if (file.getName().endsWith(".class")) { files.add(file); } } }
/** * @param packageName Package through which to iterate * @return Iterator through the classes of this jar file (if executed from * jar) or through the classes of org package (if .class file is * executed) */ public static Iterator<Class<?>> getClassesIterator(String packageName) { if (isExecutedFromJar()) { try { return new JarClassesIterator(packageName); } catch (IOException e) { throw new RuntimeException(e); } } else { return new GeneralClassesIterator(packageName); } }
/** * @param packageName Package through which to iterate */ public GeneralClassesIterator(String packageName) { String mainPath = AnnotationUtils.class.getProtectionDomain() .getCodeSource().getLocation().getFile(); String subPath = packageName.replace(".", File.separator); File directory = new File(mainPath + subPath); stringPosition = directory.getPath().length() - packageName.length(); List<File> files = new ArrayList<File>(); addAllClassFiles(directory, files); iterator = files.iterator(); }