简体   繁体   中英

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.

I want to write an utility that would read the annotation value from all the java file and update it if necessary.

I had thought about using File read/write and matching the annotation with some Regex pattern.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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