支持包名下的子包名遍历,并使用Annotation(内注)来过滤一些不必要的内部类,提高命中精度。

通过Thread.currentThread().getContextClassLoader()获取ClassLoader实例
将包名转为路径名后,做为参数传给CloassLoader.getResources(),以得到该路径下所有资源的URL;
通过URL.getProtocol()方法,判断资源是在本地(file:)或是第三方jar包(jar:)内;
在本地的类直接文件遍历即可;
第三方jar则通过URL.openConnection()得到JarURLConnection,再通过JarURLConnection.getJarFile()获得JarFile,最后遍历该JarFile的item即可。

package lab.sodino.clazz;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* Annotation:见http://blog.csdn.net/sodino/article/details/7987888
* */
@Target(ElementType.TYPE)//ElementType.TYPE用于标识类、接口(包括内注自身)、枚举
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface author {
//修饰符仅可为public, protected, private & static的组合
public static enum AppEnum {
Web, Client, Service, Undesignated
}; //public & abstract的组合或默认
AppEnum type() default AppEnum.Undesignated; String name() default "unknown"; String webSite() default "N/A";
}
package lab.sodino.clazz;
/**
* @author Sodino E-mail:sodino@qq.com
* @version Time:2014年2月10日 下午9:06:55
*/
@author(name="sodino", webSite="sodino.com")
public class ClassTestDemo { }
package lab.sodino.clazz;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile; /**
* 用于获取指定包名下的所有类名.<br/>
* 并可设置是否遍历该包名下的子包的类名.<br/>
* 并可通过Annotation(内注)来过滤,避免一些内部类的干扰.<br/>
*
* @author Sodino E-mail:sodino@qq.com
* @version Time:2014年2月10日 下午3:55:59
*/
public class ClassUtil {
public static void main(String []args){
// 标识是否要遍历该包路径下子包的类名
// boolean recursive = false;
boolean recursive = true;
// 指定的包名
// String pkg = "javax.crypto.spec";// 为java/jre6/lib/jce.jar,普通的java工程默认已引用
// String pkg = "javax.crypto";
// String pkg = "lab.sodino";
String pkg = "lab.sodino.clazz";
List list = null;
// list = getClassList(pkg, recursive, null);
// 增加 author.class的过滤项,即可只选出ClassTestDemo
list = getClassList(pkg, recursive, author.class); for(int i = 0;i < list.size(); i ++){
System.out.println(i +":"+list.get(i));
}
} public static List<Class<?>> getClassList(String pkgName , boolean isRecursive, Class<? extends Annotation> annotation) {
List<Class<?>> classList = new ArrayList<Class<?>>();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
// 按文件的形式去查找
String strFile = pkgName.replaceAll("\\.", "/");
Enumeration<URL> urls = loader.getResources(strFile);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
if (url != null) {
String protocol = url.getProtocol();
String pkgPath = url.getPath();
System.out.println("protocol:" + protocol +" path:" + pkgPath);
if ("file".equals(protocol)) {
// 本地自己可见的代码
findClassName(classList, pkgName, pkgPath, isRecursive, annotation);
} else if ("jar".equals(protocol)) {
// 引用第三方jar的代码
findClassName(classList, pkgName, url, isRecursive, annotation);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} return classList;
} public static void findClassName(List<Class<?>> clazzList, String pkgName, String pkgPath, boolean isRecursive, Class<? extends Annotation> annotation) {
if(clazzList == null){
return;
}
File[] files = filterClassFiles(pkgPath);// 过滤出.class文件及文件夹
System.out.println("files:" +((files == null)?"null" : "length=" + files.length));
if(files != null){
for (File f : files) {
String fileName = f.getName();
if (f.isFile()) {
// .class 文件的情况
String clazzName = getClassName(pkgName, fileName);
addClassName(clazzList, clazzName, annotation);
} else {
// 文件夹的情况
if(isRecursive){
// 需要继续查找该文件夹/包名下的类
String subPkgName = pkgName +"."+ fileName;
String subPkgPath = pkgPath +"/"+ fileName;
findClassName(clazzList, subPkgName, subPkgPath, true, annotation);
}
}
}
}
} /**
* 第三方Jar类库的引用。<br/>
* @throws IOException
* */
public static void findClassName(List<Class<?>> clazzList, String pkgName, URL url, boolean isRecursive, Class<? extends Annotation> annotation) throws IOException {
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
JarFile jarFile = jarURLConnection.getJarFile();
System.out.println("jarFile:" + jarFile.getName());
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String jarEntryName = jarEntry.getName(); // 类似:sun/security/internal/interfaces/TlsMasterSecret.class
String clazzName = jarEntryName.replace("/", ".");
int endIndex = clazzName.lastIndexOf(".");
String prefix = null;
if (endIndex > 0) {
String prefix_name = clazzName.substring(0, endIndex);
endIndex = prefix_name.lastIndexOf(".");
if(endIndex > 0){
prefix = prefix_name.substring(0, endIndex);
}
}
if (prefix != null && jarEntryName.endsWith(".class")) {
// System.out.println("prefix:" + prefix +" pkgName:" + pkgName);
if(prefix.equals(pkgName)){
System.out.println("jar entryName:" + jarEntryName);
addClassName(clazzList, clazzName, annotation);
} else if(isRecursive && prefix.startsWith(pkgName)){
// 遍历子包名:子类
System.out.println("jar entryName:" + jarEntryName +" isRecursive:" + isRecursive);
addClassName(clazzList, clazzName, annotation);
}
}
}
} private static File[] filterClassFiles(String pkgPath) {
if(pkgPath == null){
return null;
}
// 接收 .class 文件 或 类文件夹
return new File(pkgPath).listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory();
}
});
} private static String getClassName(String pkgName, String fileName) {
int endIndex = fileName.lastIndexOf(".");
String clazz = null;
if (endIndex >= 0) {
clazz = fileName.substring(0, endIndex);
}
String clazzName = null;
if (clazz != null) {
clazzName = pkgName + "." + clazz;
}
return clazzName;
} private static void addClassName(List<Class<?>> clazzList, String clazzName, Class<? extends Annotation> annotation) {
if (clazzList != null && clazzName != null) {
Class<?> clazz = null;
try {
clazz = Class.forName(clazzName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// System.out.println("isAnnotation=" + clazz.isAnnotation() +" author:" + clazz.isAnnotationPresent(author.class)); if (clazz != null) {
if(annotation == null){
clazzList.add(clazz);
System.out.println("add:" + clazz);
} else if (clazz.isAnnotationPresent(annotation)){
clazzList.add(clazz);
System.out.println("add annotation:" + clazz);
}
}
}
}
}

遍历指定包名下所有的类(支持jar)(转)的更多相关文章

  1. Java获取指定包名下的所有类的全类名的解决方案

        最近有个需求需要获取一个指定包下的所有类的全类名,因此特意写了个获取指定包下所有类的全类名的工具类.在此记录一下,方便后续查阅 一.思路         通过ClassLoader来查找指定包 ...

  2. Android支持Split Apks后,如何获得指定包名下的所有类

    从Android5.0以后,支持多个apk动态部署,这导致以前通过单一apk获取包路径下的所有类的方法失效,不过稍微修改一下原先的代码就可以,代码如下 public static final List ...

  3. 获取指定包名下继承或者实现某接口的所有类(扫描文件目录和所有jar)

    import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.net.JarURLCo ...

  4. Java 获取指定包下的所有类

    package com.s.rest.util; import java.io.File; import java.io.FileFilter; import java.io.IOException; ...

  5. Java反射 - 1(得到类对象的几种方法,调用方法,得到包下的所有类)

    通过反射获得对象的方法 准备工作: 有一个User类如下 package o1; /** * Created by yesiming on 16-11-19. */ public class User ...

  6. java动态载入指定的类或者jar包反射调用其方法

    序言 有时候.项目中会用到java动态载入指定的类或者jar包反射调用其方法来达到模块的分离,使各个功能之间耦合性大大减少,更加的模块化.代码利用率更高.模式中的代理模式就用到java的这一机制. 下 ...

  7. java 查找指定包下的类

    package com.jason.test; import java.io.File; import java.io.IOException; import java.io.UnsupportedE ...

  8. 黑马程序员——【Java基础】——File类、Properties集合、IO包中的其他类

    ---------- android培训.java培训.期待与您交流! ---------- 一.File类 (一)概述 1.File类:文件和目录路径名的抽象表现形式 2.作用: (1)用来将文件或 ...

  9. 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “知识的工作者必须成为自己时间的首席执行官.” 前言 这次泥瓦匠带来的是一个好玩的基于 JDK ...

随机推荐

  1. Google 开源项目风格指南阅读笔记(C++版)

    虽说是编程风格指南,可是干货也不少,非常多C++的有用技术在里面. 头文件 通常每一个.cpp文件都相应一个.h文件:#define保护全部头文件都应该使用#define防止头文件被多重包括,为保证唯 ...

  2. ASP.NET - 记住滚动条的位置

    MaintainScrollPositionOnPostback ="true" 如果是滚动条在最下面,那么如果刷新页面,滚动条回到最上面. 使用这个属性之后,滚动条会在刷新之前的 ...

  3. 类似QtiPlot的veusz,sigmaplot,pymol

    qtiplot在win下没那么好编译 依赖很多外部包的 scidavis 和 labplot是从他fork出来的 比较接近Origin 可以用这两个 FreeBSD 的 ports 里有直接 cd / ...

  4. Delphi中关于Rtti的一些操作(一)

    function TForm1.ShowMethodAddress(aObj: TDerived; const sData: String) : Pointer;var  aPtr : Pointer ...

  5. OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)

    这是一个再普通不过的Button1Click执行体: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage('I am B ...

  6. 组件状态(TComponentState)11种和组件状态(TComponentStyle)4种

    TOperation = (opInsert, opRemove); TComponentState = set of ( csAncestor The component was introduce ...

  7. Unity 3D 文件导入出错

    Unity 3D 文件导入出错 安装unity 时我选择了free版的,打开已有项目时出现例如以下错误提示 解决的方法: 先把要导入的文件先复制到unity3d安装文件夹下相应的文件夹内,之后再返回u ...

  8. ABAP常用字符串处理

    1.SEARCH搜索指定字符串 REPORT Z_CHAR. ). MOVE 'Welcom to sap world!' to str. SEARCH str for 'sap'. 如果查找成功sy ...

  9. 多校 4686 Arc of Dream hdu 矩阵解

    构造矩阵如下: Ai*bi AX*BX AX*BY AY*BX AY*BY 0 a(i-1)*b(i-1) Ai 0 AX 0 AY 0 a(i-1) Bi 0 0 BX BY 0 b(i-1) 1 ...

  10. 使用Material Design 创建App翻译系列----材料主题的使用(Using Material Theme)

    上一篇是使用Material Design 创建App翻译系列--開始学习篇,进入正题: 新的材料主题提供了下面内容: 1. 提供了同意设置颜色板的系统部件组件. 2. 为这些系统组件提供了触摸反馈动 ...