本文介绍了在Java中的包中获取类文件的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个包含在我的源代码包中的所有类文件的Class []。
我找不到标准方法它在一枪。如果有人可以编写一个函数来获取该列表,将会非常有帮助。
Class [] myClasses = yourfunction //返回当前工作项目中源代码包中的类的列表$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
我能够使用正常的文件I / O和搜索机制解决这个问题。您可以检查此处发布的答案。
private static List< Class> getClassesForPackage(Package pkg){
String pkgname = pkg.getName();
List< Class> classes = new ArrayList< Class>();
//获取包的文件对象
文件目录= null;
String fullPath;
String relPath = pkgname.replace('。','/');
//System.out.println(\"ClassDiscovery:Package:+ pkgname +变成Path:+ relPath);
URL resource = ClassLoader.getSystemClassLoader()。getResource(relPath);
//System.out.println(\"ClassDiscovery:Resource =+ resource);
if(resource == null){
throw new RuntimeException(No resource for+ relPath);
}
fullPath = resource.getFile();
//System.out.println(\"ClassDiscovery:FullPath =+ resource);
try {
directory = new File(resource.toURI());
} catch(URISyntaxException e){
throw new RuntimeException(pkgname +(+ resource +)看起来不是有效的URL / URI。 ..,e);
} catch(IllegalArgumentException e){
directory = null;
}
//System.out.println(\"ClassDiscovery:Directory =+ directory);
if(directory!= null&& directory.exists()){
//获取包中包含的文件的列表
String [] files = directory.list();
for(int i = 0; i< files.length; i ++){
//我们只对.class文件感兴趣
if(files [i]。 endsWith(。class)){
//删除.class扩展
String className = pkgname +'。'+ files [i] .substring(0,files [i] .length() - 6);
//System.out.println(\"ClassDiscovery:className =+ className);
Try {
classes.add(Class.forName(className));
} catch(ClassNotFoundException e){
throw new RuntimeException(ClassNotFoundException loading+ className);
}
}
}
} else {
try {
String jarPath = fullPath.replaceFirst([。] jar [!]。* ,.jar)。replaceFirst(file:,);
JarFile jarFile = new JarFile(jarPath);
枚举< JarEntry> entries = jarFile.entries();
while(entries.hasMoreElements()){
JarEntry entry = entries.nextElement();
String entryName = entry.getName();
if(entryName.startsWith(relPath)&& entryName.length()>(relPath.length()+/。length())){
System.out.println(ClassDiscovery:JarEntry:+ entryName);
String className = entryName.replace('/','。')。replace('\\','。')。replace(。class,);
//System.out.println(\"ClassDiscovery:className =+ className);
try {
classes.add(Class.forName(className));
} catch(ClassNotFoundException e){
throw new RuntimeException(ClassNotFoundException loading+ className);
}
}
}
} catch(IOException e){
throw new RuntimeException(pkgname +(+ directory +)有效包,e);
}
}
返回类;
}
I need a Class[] of all the class files contained in one of my source packages in Java.
I could n't find a standard method to do it in one shot. If someone can write a function to fetch that list would be really helpful.
Class[] myClasses = yourfunction(); // Return a list of class inside a source package in the currently working project in java
解决方案
I am able to solve this problem using normal file I/O and search mechanism. You can check the answer as posted here.
private static List<Class> getClassesForPackage(Package pkg) {
String pkgname = pkg.getName();
List<Class> classes = new ArrayList<Class>();
// Get a File object for the package
File directory = null;
String fullPath;
String relPath = pkgname.replace('.', '/');
//System.out.println("ClassDiscovery: Package: " + pkgname + " becomes Path:" + relPath);
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
//System.out.println("ClassDiscovery: Resource = " + resource);
if (resource == null) {
throw new RuntimeException("No resource for " + relPath);
}
fullPath = resource.getFile();
//System.out.println("ClassDiscovery: FullPath = " + resource);
try {
directory = new File(resource.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(pkgname + " (" + resource + ") does not appear to be a valid URL / URI. Strange, since we got it from the system...", e);
} catch (IllegalArgumentException e) {
directory = null;
}
//System.out.println("ClassDiscovery: Directory = " + directory);
if (directory != null && directory.exists()) {
// Get the list of the files contained in the package
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
// we are only interested in .class files
if (files[i].endsWith(".class")) {
// removes the .class extension
String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
//System.out.println("ClassDiscovery: className = " + className);
try {
classes.add(Class.forName(className));
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + className);
}
}
}
} else {
try {
String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
JarFile jarFile = new JarFile(jarPath);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String entryName = entry.getName();
if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
//System.out.println("ClassDiscovery: JarEntry: " + entryName);
String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
//System.out.println("ClassDiscovery: className = " + className);
try {
classes.add(Class.forName(className));
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + className);
}
}
}
} catch (IOException e) {
throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
}
}
return classes;
}
这篇关于在Java中的包中获取类文件的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!