|
锁定老贴子 主题:如何取得某个包下面的所有的Class对象
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-03-17
查看了javadoc,google了,没有找到好的答案,有个例子指出了一种方法,给一个入口Class,搜索这个Class所继承,引用的Class,以及这些继承引用的Class所继承引用的Class,如此递归下去,得到一个Class的List。但这种方法不能达到我的目的,我所能知道的只有package name,没有任何相关的class,如何才能或的这个package下的Class对象的list呢?
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-03-18
理论上是不可能的。java的package是开放式的,同一个pacakge的类完全可以放在不同的地方。java并没有提供标准的可移植的方法来遍历class.
但是实际应用中,可以利用一些假设(比如,这个package的类都被deploy到一个jar或者一个目录下,这时候可以用zip或者File来读取这些类。) 具体实现参考Ant的junit task,或者http://www.openqa.org/jtc里面遍历类的方法。 |
|
| 返回顶楼 | |
|
最后更新时间:2007-03-18
yes, I did the same way as ajoo said. ant is a good reference.
|
|
| 返回顶楼 | |
|
最后更新时间:2007-03-18
ajoo 写道 理论上是不可能的。java的package是开放式的,同一个pacakge的类完全可以放在不同的地方。java并没有提供标准的可移植的方法来遍历class.
但是实际应用中,可以利用一些假设(比如,这个package的类都被deploy到一个jar或者一个目录下,这时候可以用zip或者File来读取这些类。) 具体实现参考Ant的junit task,或者http://www.openqa.org/jtc里面遍历类的方法。 very good,thank you very very much. |
|
| 返回顶楼 | |
|
最后更新时间:2007-03-18
public static List<String> getClasses(String packageName, Class clazz) {
List<String> res = new ArrayList<String>();
String pckgname = "test.package.test";
pckgname = packageName;
String name = new String(pckgname);
if (!name.startsWith("/")) {
name = "/" + name;
}
name = name.replace('.', '/');
URL url = ClassTool.class.getResource(name);
if (url == null) {
return res;
}
File directory = new File(url.getFile());
if (directory.exists()) {
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".class")) {
String classname = files[i].substring(0,
files[i].length() - 6);
try {
String clsName = pckgname + "." + classname;
logger.debug("clsName=" + clsName);
Object o = Class.forName(clsName).newInstance();
// if (o instanceof Class) {
// System.out.println(classname);
// }
if (o instanceof Object) {
}
res.add(pckgname + "." + classname);
} catch (ClassNotFoundException cnfex) {
logger.error("getClasses(String)" + cnfex, cnfex);
} catch (InstantiationException iex) {
} catch (IllegalAccessException iaex) {
}
}
}
}
return res;
}
也许用得到 |
|
| 返回顶楼 | |
|
最后更新时间:2007-03-18
搜索 classpath,然后读取所有的 class 文件。仅供参考。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-03-18
classloader有一个搜索class的方式,不知道可不可以用于package的搜索,哪位熟悉的给点拨下,
另,楼上的楼上,有FileUtils, ClassUtils可以让代码更整洁些。 |
|
| 返回顶楼 | |
|
最后更新时间:2007-03-20
String classpath = "bean,spring";
String[] classpaths = StringUtils.split(classpath, ",");
List xmllist = new java.util.ArrayList();
for (int j = 0; j < classpaths.length; j++) {
Enumeration pathEnumeration = SpringHelper.class.getClassLoader()
.getResources(classpaths[j]);
if (pathEnumeration == null) {
throw new RuntimeException("路径[" + classpaths[j] + "]不存在");
}
while (pathEnumeration.hasMoreElements()) {
URL url = (URL) pathEnumeration.nextElement();
String path = url.getPath();
String protocol = url.getProtocol();
if ("file".equals(protocol)) {
File file = new File(url.getPath());
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File xmlFile = files[i];
if (xmlFile.getName().endsWith(".xml")) {
if (!xmllist.contains(classpaths[j] + "/"
+ xmlFile.getName())) {
xmllist.add(classpaths[j] + "/"
+ xmlFile.getName());
}
}
}
}
} else {
if (file.getName().endsWith(".xml")) {
if (!xmllist.contains(classpaths[j] + "/"
+ file.getName())) {
xmllist.add(classpaths[j] + "/"
+ file.getName());
}
}
}
}
if ("jar".equals(protocol)) {
URL jarUrl = new URL(path);
path = jarUrl.getPath();
if (path.endsWith("!/" + classpaths[j])) {
/**
* jar包中
*/
path = path.substring(0, path.lastIndexOf("!/"
+ classpaths[j]));
JarFile jarFile = new JarFile(new File(path));
Enumeration enumeration = jarFile.entries();
while (enumeration.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) enumeration
.nextElement();
if (jarEntry.getName().startsWith(
classpaths[j] + "/")
&& jarEntry.getName().endsWith(".xml")) {
String xmlfileName = jarEntry.getName()
.substring(
jarEntry.getName().lastIndexOf(
"/") + 1,
jarEntry.getName().length());
if (!xmllist.contains(classpaths[j] + "/"
+ xmlfileName)) {
xmllist.add(classpaths[j] + "/"
+ xmlfileName);
}
}
}
}
}
}
if (xmllist.size() == 0) {
throw new RuntimeException("路径[" + classpaths[j]
+ "]下没有spring配置文件");
}
}
String[] xmlFileNames = new String[xmllist.size()];
for (int i = 0; i < xmlFileNames.length; i++) {
xmlFileNames[i] = (String) xmllist.get(i);
System.out.println(xmlFileNames[i]);
}
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
xmlFileNames);
factory = (BeanFactory) appContext;
|
|
| 返回顶楼 | |
|
最后更新时间:2007-03-20
rtm :你的这个解决办法是不错哦 可不可以把里面的代码分割为一个个的方法呢 请您试一试 OK!
|
|
| 返回顶楼 | |
|
最后更新时间:2007-03-20
在您确切知道CLASS文件的路径的前提下是可以实现的,否则,字节码文件甚至可以通过网络下载,所以无法实现。
|
|
| 返回顶楼 | |










