英文:
How to modify code automatically in multiple java classes?
问题
我有一个对象层次结构,或者树形结构,我想做两件事:
- 对于每个节点,将每个公共字段变为私有,并生成getter和setter。
- 将树的所有状态保存在一个<String,Object>映射中,以便每次对节点之一的setter或getter进行调用时都会访问映射,并且不会从节点本身保存或检索状态。我会这样做是将对映射的引用注入到树中的每一个节点中,并让setter和getter操作映射。
我想知道是否有一种方法可以自动修改我的代码来实现这些。我知道IntelliJ可以将类中的所有公共字段变为私有,并为一个文件生成getter和setter,但是否可能对多个文件进行这样的操作?是否可能自动进行我所概述的第二次修改?
英文:
I have an object hierarchy, or tree, for which I'd like to do two things:
- For every node make every public field private and generate getters and setters.
- Save all state of the tree in a <String, Object> map so every time a setter or getter is invoked on one of the nodes it goes to the map and doesn't save or retrieve the state from the node itself. The way I would do this is to inject a reference to the map into every one of the nodes in the tree and have the setters and getters operate on the map.
I'm wondering if there's a way to modify my code automatically to do this. I know IntelliJ can make all public fields in a class private and generate getters and setters for one file, but is it possible to do this for many? Is it possible to do the second modification I outlined automatically?
答案1
得分: 2
你可以使用IntelliJ的getter/setter模板来实现这一点。
这是一个相当清晰的伪代码工具,用于创建自定义代码生成机制。
参考这个问题以更清楚地了解。
英文:
You could do this using IntelliJ-template for getter/setter.
It's a pretty clear pseudo-code tool to create a custom code-generation mechanic.
See this question to understand more clear
答案2
得分: 0
你可以使用ByteBuddy来完成这个任务。
英文:
You could use ByteBuddy to accomplish this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论