扫描Jar中或目录下类文件的Annotation
2016-07-14 11:48
218 查看
直接上代码,代码依赖tomcat的tomcat-coyote.jar。
package guojje.tools; import java.io.File; import java.io.FileInputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.jar.JarEntry; import java.util.jar.JarFile; import org.apache.tomcat.util.bcel.classfile.AnnotationEntry; import org.apache.tomcat.util.bcel.classfile.ClassFormatException; import org.apache.tomcat.util.bcel.classfile.ClassParser; import org.apache.tomcat.util.bcel.classfile.JavaClass; public class ScanAnnotations { public static void main(String[] args) throws ClassFormatException, IOException { File dir = null; if (args.length > 0) { dir = new File(args[0]); } else { dir = new File("."); } File[] jarFiles = dir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".jar"); } }); List<File> classFiles = new LinkedList<File>(); Queue<File> fileQueue = new LinkedList<File>(); fileQueue.offer(dir); File curFile = null; while ((curFile = fileQueue.poll()) != null) { File[] subFiles = curFile.listFiles(); for (File sub : subFiles) { if (sub.isDirectory()) { fileQueue.offer(sub); } else if (sub.getName().endsWith(".class")) { classFiles.add(sub); } } } for (File jf : jarFiles) { System.out.println(jf.getName()); JarFile jarFile = new JarFile(jf); scanJarAnnotaiton(jarFile); } for (File cf : classFiles) { System.out.println("===" + cf.getAbsolutePath() + "==================="); scanClassFileAnnotaiton(cf); } } public static void scanJarAnnotaiton(JarFile jarFile) throws IOException { Enumeration<JarEntry> ej = jarFile.entries(); while (ej.hasMoreElements()) { JarEntry je = ej.nextElement(); if (!je.isDirectory() && je.getName().endsWith(".class")) { InputStream classStream = jarFile.getInputStream(je); ClassParser classParser = new ClassParser(classStream); JavaClass jc = classParser.parse(); if (jc.getAnnotationEntries() != null && jc.getAnnotationEntries().length > 0) { System.out.println("===" + jarFile.getName() + "=======" + jc.getClassName() + "========================="); for (AnnotationEntry ae : jc.getAnnotationEntries()) { System.out.println(ae.getAnnotationType()); } } } } } public static void scanClassFileAnnotaiton(File cf) throws IOException { if (!cf.isDirectory() && cf.getName().endsWith(".class")) { InputStream classStream = new FileInputStream(cf); ClassParser classParser = new ClassParser(classStream); JavaClass jc = classParser.parse(); if (jc.getAnnotationEntries() != null && jc.getAnnotationEntries().length > 0) { System.out.println("===" + cf.getAbsolutePath() + "================="); for (AnnotationEntry ae : jc.getAnnotationEntries()) { System.out.println(ae.getAnnotationType()); } } } } }阅读更多
相关文章推荐
- 扫描Jar中或目录下类文件的Annotation
- 扫描Jar中或目录下类文件的Annotation
- 扫描某目录下的所有文件的MD5码并导出文件【可执行jar】
- 获取指定包名下继承或者实现某接口的所有类(扫描文件目录和所有jar)
- SSH所需的jar文件及最后项目目录
- jar文件转换成Maven目录
- Libs文件夹下的Jar文件为什么不会自动放在Android Private Libraries目录下
- 用spring annotation声明的bean,当打包在jar中时,无法被扫描到
- 如何把一个目录中的文件打成一个jar文件
- 扫描一个目录下的所有文件,根据这些文件的创建日期生成一个文件夹,然后把这些文件移入这个文件夹下面
- 可执行jar包中获取jar文件所在目录
- Python实现扫描指定目录下的子目录及文件的方法
- SpringBoot上传文件到本服务器 目录与jar包同级问题
- Socket异步服务器,可以监控客户端的状态,功能有,文字测试,服务端向客户端传输屏幕录像(UDP传输)、监控客户端屏幕(UDP传输),抖动用户窗体、发送文件给用户、扫描客户的C盘目录。
- 扫描目录下的php文件,是否含有木马特征
- 为什么不能把BlackBerry开发项目deliverables目录下面的jar文件给第三方?
- DexClassLoader自定义加载Assets目录下的dex、jar文件
- [Java] 在 jar 文件中读取 resources 目录下的文件
- 获取jar包中的目录及文件
- maven学习系列8----将resources目录下的文件打包到jar包外