简体   繁体   English

是否可以在Java文件中读取和写入注释?

[英]Is it possible to read and write an annotation into a java file?

I have around 1000+ Java files, all have some annotation written on it at class level. 我大约有1000多个Java文件,所有文件都在类级别上写了一些注释。

I want to write an utility that would read the annotation value from all the java file and update it if necessary. 我想编写一个实用程序,该实用程序将从所有Java文件中读取注释值,并在必要时进行更新。

I had thought about using File read/write and matching the annotation with some Regex pattern. 我曾考虑过使用文件读/写并将批注与某些Regex模式匹配。

Would there be any better way of doing it ? 会有更好的方法吗?

A lot depends on whether you want to do it once, or developing a system to do it repeatedly in the future. 在很大程度上取决于您是否想执行一次,还是要开发一个系统以在将来重复执行。

The first case is much simpler, you could use a smart IDE (like Eclipse) to search for all uses of an annotation in your project and then do things manually. 第一种情况要简单得多,您可以使用智能IDE(例如Eclipse)来搜索项目中注释的所有用途,然后手动执行操作。 Or you can execute a regexp-based search/replace. 或者,您可以执行基于正则表达式的搜索/替换。 I often add a dummy comment to each line of use to make the required regexp simpler. 我经常在使用的每一行中添加一个虚拟注释,以简化所需的正则表达式。

The second case is much harder. 第二种情况要困难得多。 A custom Java parser would likely be required. 可能需要自定义Java解析器。

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

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