简体   繁体   English

大规模Eclipse AST Java重构

[英]Massive Eclipse AST Java Refactoring

Dear stackoverflow community! 亲爱的stackoverflow社区!

I have to refactor a huge amount of java classes and want to do it automatized. 我必须重构大量的Java类,并希望将其自动化。 I want to use Java JDT and write an eclipse plugin for this purpose. 我想使用Java JDT并为此编写一个eclipse插件。

Following 'problems' should be solved: 以下“问题”应解决:

  • Methods should not throw generic Exceptions in their definition. 方法不应在其定义中引发泛型异常。 I was thinking about deleting the Exception throws block and let eclipse auto-generate the throws declaration for me. 我当时正在考虑删除Exception throws块,然后让eclipse为我自动生成throws声明。 Is this possible? 这可能吗? I haven't found any resources about that. 我还没有找到任何资源。 I am already able to get all relevant methods as JDT models, but don't know how to delete the throws block and invoke the auto-correct feature of eclipse. 我已经能够将所有相关方法用作JDT模型,但是不知道如何删除throws块并调用eclipse的自动更正功能。

     // So instead of public int foo() throws Exception { // do some SQL-stuff } // Should be public int foo() throws SQLException, NoResultException { // do some SQL-stuff } 
  • Protected fields should be rewritten to private and protected accessors should be provided. 受保护的字段应重写为私有字段,并应提供受保护的访问者。 I think this is self explainable, but how to edit all references outside the Project to acess the getter? 我认为这是可以自我解释的,但是如何在项目外部编辑所有引用以获取吸气剂? (There are no setter, and if there are, i could edit them by hand). (没有设置器,如果有的话,我可以手动编辑它们)。

Thank you in advance and sorry for my English. 在此先感谢您,对不起我的英语。

If you don't know how to start, here is a good tutorial on how to use the AST and the Java Model in JDT: http://www.vogella.com/tutorials/EclipseJDT/article.html 如果您不知道如何开始,这里有一个很好的教程,介绍了如何在JDT中使用AST和Java模型: http : //www.vogella.com/tutorials/EclipseJDT/article.html

If you do your refactoring in several steps, you could remove all throw declarations with generic exceptions first using your own plug-in leaving touched sources with compile errors. 如果您分几个步骤进行重构,则可以首先使用自己的插件删除所有带有通用异常的throw声明,而使接触的源代码具有编译错误。 You can then start the quick fix manualy in the IDE. 然后,您可以在IDE中手动启动快速修复。 If you have huge amounts of changes, I would not recommend this approach. 如果您进行了大量更改,则不建议使用此方法。

It will be difficult to find a good entry point into the API itself, since you will probably want to do all the code changes in one "transaction". 在API本身中很难找到一个好的入口点,因为您可能想在一个“事务”中进行所有代码更改。 I have tried similar stuff in the past and always ended up coding the relevant parts myself, although reading the eclipse code realy helped alot! 过去,我尝试过类似的东西,但总是自己编写相关部分,尽管阅读Eclipse代码确实有很大帮助! A good way to start is to use the "Plugin Spy" to find the relevant classes. 一个好的开始方法是使用“插件间谍”来找到相关的类。 (Shift+Alt+F1 or Shift+Option+F1 on Mac). (在Mac上为Shift + Alt + F1或Shift + Option + F1)。 It shows information for whatever is currently selected. 它显示当前所选内容的信息。

To create the throws block yourself you can inspect the code using the visitor pattern in AST and collect all thrown exceptions, if they are not included in a suitable catch-block. 要自己创建throws块,您可以使用AST中的访问者模式检查代码,并收集所有抛出的异常(如果它们未包含在合适的catch块中)。

The second requirement can be implemented straight forward with AST. 第二个要求可以通过AST直接实现。 You can have a look at the "Encapsulate Field" Refactoring of eclipse on how to do this (or use this feature if possible). 您可以查看Eclipse的“封装字段”重构,了解如何执行此操作(或在可能的情况下使用此功能)。

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

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