简体   繁体   中英

Massive Eclipse AST Java Refactoring

Dear stackoverflow community!

I have to refactor a huge amount of java classes and want to do it automatized. I want to use Java JDT and write an eclipse plugin for this purpose.

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

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

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. You can then start the quick fix manualy in the 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". 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! 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). 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.

The second requirement can be implemented straight forward with AST. You can have a look at the "Encapsulate Field" Refactoring of eclipse on how to do this (or use this feature if possible).

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