简体   繁体   English

使用模式获取Javassist的所有类

[英]Getting all classes with Javassist using a pattern

How can I get all classes using a pattern like "com.stackoverflow.*" with Javassist? 如何使用Javassist中的“com.stackoverflow。*”等模式获取所有类?

i found only 2 methods : 我发现只有2种方法:

1/ Find a class by full name 1 /按姓名查找课程

CtClass ClassPool.getDefault().getCtClass("com.stackoverflow.user.name")

2/ Find a list of classes with fullnames : 2 /查找具有全名的类列表:

CtClass[] ClassPool.getDefault().get(String [] arg0)

You could use some library like : https://github.com/ronmamo/reflections 你可以使用一些库: https//github.com/ronmamo/reflections

I don't think you can do that just with JRE classes. 我认为你不能只用JRE课程来做到这一点。

Example from the doc : 来自doc的示例:

Reflections reflections = new Reflections("my.project.prefix");

Set<Class<? extends SomeType>> subTypes = 
           reflections.getSubTypesOf(SomeType.class);

Set<Class<?>> annotated = 
           reflections.getTypesAnnotatedWith(SomeAnnotation.class);

Michael Laffargue 's suggestion is the best way to go. 迈克尔拉法格的建议是最好的方法。 The Reflections library uses javassist under the covers. Reflections库使用了javassist Basically, javassist provides a means of reading raw byte code from class or jar files and extracting the class meta-data from it without actually class loading it, where as Reflections provides a richer API around locating (via classpath specifications) and filtering the set of classes you're looking for. 基本上,javassist提供了一种从类或jar文件中读取原始字节代码并从中提取类元数据而无需实际加载类的方法,其中Reflection提供了更丰富的API来定位(通过类路径规范)并过滤集合你正在寻找的课程。

You can do the same thing yourself using javassist only, but you will be recreating some portion of the Reflections library. 您可以仅使用javassist自己做同样的事情,但是您将重新创建Reflections库的某些部分。 You could look at Reflections' source code to see how it works, but very generally speaking, it looks like this: 您可以查看Reflections的源代码以了解它是如何工作的,但一般来说,它看起来像这样:

  1. Locate the classpath you want to scan. 找到要扫描的类路径。 This will usually be a group of directories with a tree of class files, or a group of Jar files, but could also include more complex structures like WARs or EARs (which Reflections supports quite nicely). 这通常是一组带有类文件树或一组Jar文件的目录,但也可能包含更复杂的结构,如WAR或EAR(Reflections支持相当不错)。

  2. Add the root of the file system where the class files live, or the JAR file reference to your ClassPool instance. 添加类文件所在的文件系统的根目录,或者将JAR文件引用添加到ClassPool实例。

  3. Using a file system iteration or a JarInputStream, iterate through each class file or JarEntry. 使用文件系统迭代或JarInputStream,遍历每个类文件或JarEntry。 You can filter out any files or entries that do not match "com/stackoverflow/**.class" 您可以过滤掉与“com / stackoverflow / **。class”不匹配的任何文件或条目
  4. For the remaining, using the name of the file or entry, derrive the class name and load it from the javassist class pool. 对于剩下的,使用文件或条目的名称,驱动类名并从javassist类池加载它。
  5. Use the loaded CtClass to apply any further search criteria. 使用加载的CtClass应用任何进一步的搜索条件。
  6. Now you have your class reference list, release the ClassPool for garbage collection. 现在您有了类引用列表,释放了ClassPool以进行垃圾回收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM