java遍历一个包中所有的类,转存,不知好不好用。
来源:互联网 发布:熊猫tv抢竹子软件 编辑:程序博客网 时间:2024/04/29 18:46
/*
* @(#)PackageUtil.java 1.00 2006-11-27
*
* Copyright (c) 2005 Shenzhen Surfilter Network Technology Co.,Ltd. All rights reserved.
*/
package org.rut.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
* @since 2006-11-27
* @author wushugen
*
* Modified History:
*
*/
public class PackageUtil {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
List <String> cls = getClassInPackage( "java.util ");
for(String s:cls){
System.out.println(s);
}
}
public static List <String> getClassInPackage(String pkgName) {
List <String> ret = new ArrayList <String> ();
String rPath = pkgName.replace( '. ', '/ ') + "/ ";
try {
for (File classPath : CLASS_PATH_ARRAY) {
if(!classPath.exists()) continue;
if (classPath.isDirectory()) {
File dir = new File(classPath, rPath);
if(!dir.exists()) continue;
for (File file : dir.listFiles()) {
if (file.isFile()) {
String clsName = file.getName();
clsName = pkgName+ ". " +clsName.substring(0, clsName.length() - 6);
ret.add(clsName);
}
}
} else {
FileInputStream fis = new FileInputStream(classPath);
JarInputStream jis = new JarInputStream(fis, false);
JarEntry e = null;
while ((e = jis.getNextJarEntry()) != null) {
String eName = e.getName();
if (eName.startsWith(rPath) && !eName.endsWith( "/ ")) {
ret.add(eName.replace( '/ ', '. ').substring(0,eName.length()-6));
}
jis.closeEntry();
}
jis.close();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return ret;
}
private static String[] CLASS_PATH_PROP = { "java.class.path ", "java.ext.dirs ",
"sun.boot.class.path " };
private static List <File> CLASS_PATH_ARRAY = getClassPath();
private static List <File> getClassPath() {
List <File> ret = new ArrayList <File> ();
String delim = ": ";
if (System.getProperty( "os.name ").indexOf( "Windows ") !=-1)
delim = "; ";
for (String pro : CLASS_PATH_PROP) {
String[] pathes = System.getProperty(pro).split(delim);
for (String path : pathes)
ret.add(new File(path));
}
return ret;
}
}
* @(#)PackageUtil.java 1.00 2006-11-27
*
* Copyright (c) 2005 Shenzhen Surfilter Network Technology Co.,Ltd. All rights reserved.
*/
package org.rut.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
* @since 2006-11-27
* @author wushugen
*
* Modified History:
*
*/
public class PackageUtil {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
List <String> cls = getClassInPackage( "java.util ");
for(String s:cls){
System.out.println(s);
}
}
public static List <String> getClassInPackage(String pkgName) {
List <String> ret = new ArrayList <String> ();
String rPath = pkgName.replace( '. ', '/ ') + "/ ";
try {
for (File classPath : CLASS_PATH_ARRAY) {
if(!classPath.exists()) continue;
if (classPath.isDirectory()) {
File dir = new File(classPath, rPath);
if(!dir.exists()) continue;
for (File file : dir.listFiles()) {
if (file.isFile()) {
String clsName = file.getName();
clsName = pkgName+ ". " +clsName.substring(0, clsName.length() - 6);
ret.add(clsName);
}
}
} else {
FileInputStream fis = new FileInputStream(classPath);
JarInputStream jis = new JarInputStream(fis, false);
JarEntry e = null;
while ((e = jis.getNextJarEntry()) != null) {
String eName = e.getName();
if (eName.startsWith(rPath) && !eName.endsWith( "/ ")) {
ret.add(eName.replace( '/ ', '. ').substring(0,eName.length()-6));
}
jis.closeEntry();
}
jis.close();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return ret;
}
private static String[] CLASS_PATH_PROP = { "java.class.path ", "java.ext.dirs ",
"sun.boot.class.path " };
private static List <File> CLASS_PATH_ARRAY = getClassPath();
private static List <File> getClassPath() {
List <File> ret = new ArrayList <File> ();
String delim = ": ";
if (System.getProperty( "os.name ").indexOf( "Windows ") !=-1)
delim = "; ";
for (String pro : CLASS_PATH_PROP) {
String[] pathes = System.getProperty(pro).split(delim);
for (String path : pathes)
ret.add(new File(path));
}
return ret;
}
}
- java遍历一个包中所有的类,转存,不知好不好用。
- Java遍历包中所有类
- Java遍历包中所有类-终续
- Java遍历包中所有类
- Java遍历包中所有类
- Java遍历包中所有类(完整转载:-))
- java得到一个包的所有类
- java中遍历一个对象的所有属性
- [Java] 遍历指定包名下所有的类(支持jar)
- Java遍历jar包所有类-后续
- Java遍历一个类的所有属性和值
- Java中出现了一个不知原因的NullPointerException
- 【正完成】java 反射一个包下的所有类
- Java中遍历一个文件夹下所有文件及目录的操作
- java遍历一个目录中不包括当前目录的其他所有父级目录
- 王立平--android中遍历一个文件夹下的所有文件
- java遍历一个文件夹下面的所有文件
- java 如何递归遍历一个文件夹下的所有文件
- windows平台下vlc编译之十二:用GDB调试vlc
- Android有效解决加载大图片时内存溢出的问题
- Android中使用OpenCV
- 在Android手机上实现阅读器的翻页效果
- linux下 find与cp命令的结合
- java遍历一个包中所有的类,转存,不知好不好用。
- 打印信息
- 【转】只能在同一IE窗口打开网页的基本解决方法
- Android实现图书的翻页效果
- 用PHP制作简单的图片验证码
- ASP.NET 基础(5)
- android酷炫翻页效果+图形分析
- ASP与SQL基础
- 怎么改变Activity中的TextView内容