重构我的Java代码,将属性访问替换为使用getter和setter方法。

huangapple go评论96阅读模式
英文:

Refactor my Java code to replace attribute access to getters and setters

问题

我刚刚重构了一个旧的Java库,并使用setter和getter替换了所有的公共属性。
现在,有大量的代码在周围直接访问那些旧属性。

有没有人知道是否有一个工具可以遍历所有的数百个类,并重构它们以替换对setter或getter的直接访问?

例如:

    old.attr ="Hello!";
    String c = old.attr;

到:

    old.setAttr("Hello!");
    String c = old.getAttr();

有没有类似的工具可以实现这个功能?

提前谢谢,

Juan

英文:

I just refactored an old Java library and replaced all public attributes with setters and getters.
Now, there are tons of code around that is accessing those old attributes directly.

Does anyone know of a tool that can go through all the hundreds of classes and refactor them to replace the direct access to the setter or getter ?

For example:

old.attr = "Hello!";
String c = old.attr;

To:

old.setAttr("Hello!");
String c = old.getAttr();

Is there something around that will do that magic?

Thanks in advance,

Juan

答案1

得分: 1

如果您正在使用IntelliJ作为您的集成开发环境(IDE),您可以执行以下操作:

右键单击公共属性 -> 重构 -> 封装字段

英文:

If you are using IntelliJ as your IDE, you can do this:
>right click the public attribute -> refactor -> encapsulate fields

huangapple
  • 本文由 发表于 2020年8月19日 09:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63478955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定