[java]
package com.itkt.mtravel.hotel.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PackageUtil {
public static void main(String[] args) {
String packageName = "com.itkt.mtravel.hotel";
List
for (String className : classNames) {
System.out.println(className);
}
}
public static List
String filePath = ClassLoader.getSystemResource("").getPath() + packageName.replace(".", "\\");
List
}
private static List
List
File file = new File(filePath);
File[] childFiles = file.listFiles();
for (File childFile : childFiles) {
if (childFile.isDirectory()) {
myClassName.addAll(getClassName(childFile.getPath(), myClassName));
} else {
String childFilePath = childFile.getPath();
childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath.lastIndexOf("."));
childFilePath = childFilePath.replace("\\", ".");
myClassName.add(childFilePath);
}
}
return myClassName;
}
}
没什么特别复杂的,看看基本上就能懂的。给入任意包的命名空间,就能返回该包下的所有类。自我感觉还不错,简单易用,通用和扩展性也不错。其实写代码,在有熟练的基本功之后,剩下的就是组装的思路了。