[英]Java, Is index annotated classes at compile time with Annotation Processor a good practice?
I am thinking about building an index (even a simple list stored in a file) of classes annotated with a certain annotation type at compile time with an annotation Processor in order to speed up annotated class retrieval at runtime. 我正在考虑在编译时使用注释处理器构建一个使用某种注释类型注释的类的索引(甚至是存储在文件中的简单列表),以便在运行时加速注释类检索。
So, is it a good practice? 那么,这是一个好习惯吗? Are there any drawbacks?
有什么缺点吗? If it is as good as it seems to me now, why aren't there many libraries to do this in an easy way (the only one I have found is Class Index )?
如果它现在看起来和我一样好,为什么没有很多库以简单的方式做到这一点(我发现的唯一一个是Class Index )? Instead for runtime processing there are so many?
而不是运行时处理有那么多?
As an author of the ClassIndex library I can list several advantages of using annotation processing for annotation indexing, but also a drawbacks which I think hinder its widespread adoption. 作为ClassIndex库的作者,我可以列出使用注释处理进行注释索引的几个优点,但也有一些缺点,我认为这会阻碍其广泛采用。
Advantages: 好处:
Drawbacks: 缺点:
I think the main disadvantages is that it's more complicated. 我认为主要的缺点是它更复杂。 Annotation processing is a whole new API and concept that many developers aren't familiar with.
注释处理是许多开发人员不熟悉的全新API和概念。 The Reflection API is easier and more well known.
Reflection API更容易且更为人所知。 You can usually accomplish the same tasks at runtime.
您通常可以在运行时完成相同的任务。
If better startup performance is crucial (which is rarely the case) then maybe it's worth the added complexity. 如果更好的启动性能是至关重要的(这种情况很少见),那么可能值得增加复杂性。
I wouldn't trust the benchmarks though. 我不相信基准。 They state "classpath size was set to 121MB" - an arbitrary value that makes any comparison to hard coded or compile time processing completely useless.
他们声明“类路径大小设置为121MB” - 一个任意值,使任何比较硬编码或编译时处理完全无用。 Why would you want to scan the whole class path anyway?
你为什么要扫描整个类路径呢? Scanning only the developers classes would be more reasonable in most cases.
在大多数情况下,仅扫描开发人员类会更合理。
Many frameworks use configuration files or have an API to limit the classes or packages that need to be scanned. 许多框架使用配置文件或使用API来限制需要扫描的类或包。 This increases startup time significantly.
这显着增加了启动时间。
why aren't there many libraries to do this
为什么没有很多图书馆可以做到这一点
Many OSGi tools/frameworks do this. 许多OSGi工具/框架都是这样做的。 Annotations are scanned at compile time and meta-data is written to the jar manifest file or they create more sophisticated meta-data files.
在编译时扫描注释,并将元数据写入jar清单文件,或者创建更复杂的元数据文件。 I suspect the main reason for this is to keep compatibility with bnd and similar tools, which have been used to built and compile time analysis of OSGi components before annotations or annotation processing got more popular.
我怀疑这样做的主要原因是为了保持与bnd和类似工具的兼容性,这些工具在注释或注释处理变得越来越流行之前用于构建和编译OSGi组件的时间分析。 Also, OSGi component have their own lifecycle and can come and go at any time.
此外,OSGi组件有自己的生命周期,可以随时进出。 So this is a case where startup time does matter a bit more, as you can't only scan once at application start.
因此,这是一个启动时间确实重要的情况,因为您不仅可以在应用程序启动时扫描一次。 You would need to scan for annotations whenever a component (re-)starts.
每当组件(重新)启动时,您都需要扫描注释。
I wouldn't say it's a good nor bad practice. 我不会说这是一个好的也不是坏的做法。 Use this technique when it fits your needs.
在满足您的需求时使用此技术。 I would avoid adding to much complexity for the sake of a few milliseconds startup time.
为了几毫秒的启动时间,我会避免增加复杂性。
A sub-project of JBoss WildFly, may be interesting to you: Jandex . JBoss WildFly的子项目可能对你很有意思: Jandex 。
It creates annotation indexes at build time (and index file can be added to the JAR) or runtime (annotations are retrieved by examining class files, not through reflection), significantly increasing performance of annotation retrieval as it avoids the need to actually load classes. 它在构建时创建注释索引(并且可以将索引文件添加到JAR)或运行时(通过检查类文件而不是通过反射来检索注释),从而显着提高注释检索的性能,因为它避免了实际加载类的需要。
Jandex sounds pretty much like what you are after. Jandex听起来非常像你所追求的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.