This time its OSGi.
For my new assignment only I was and is the team; this time requirement was
Many times naive developers code in Java and leaves
- code un-formatted
- does not remove unnecessary imports
- does not remove unused variables
Can we correct such code up to certain extent pro-grammatically?
As I gradually started looking into Sonar, SonarQube etc I find that all these tools gives problem with our code but not the solution to those problems. Then I searched for such APIs/Frameworks which could give me both - Problems in my code and Solution to them.
I finally landed at Eclipse JDT world. On further digging I find that I need to make one headless OSGi plugin/application to achieve what I want.
After making headless eclipse plugin and certain RnD, I was not getting workbench object which is only available once Eclipse IDE opens.
- So this is the case where IDE needs to be opened to run code/application.
- Once IDE is opened then project is loaded which needs to be examined that means you need eclipse project.
- Then warnings/errors that we can configure in Eclipse gives proposals for problems (errors and warnings). These proposals contains priority & changes based on context which can be applied to certain problem as a solution.
Ex. If there are two or more imports which are not used by your program and when program is parsed by Eclipse it will check for first unused import and gives two proposals which are - (1) To remove that import only (2) Organize import which will remove all such unused imports.
So what I did programmatically as my initial approach (Yes there is one more approach which I am using now):
- Used APIs to open project first
- From Eclipse Project convert it to Java project
- Collect names of specific files (called Documents in Eclipse APIs)
- Open file (it will load it in IDE)
- Parse each file for problems
- For each problem get JavaProposals
- Pick best suited proposals or if you do not want to address certain problem skip it
- Apply proposal
- Save Document
- Close Document (So that too many documents does not get open)
- Close Project
- This is how you can go for each file.
After some more RnD I changed my approach which is more performant:
- In Eclipse APIs certain Action classes are available to execute certain menu actions such as FormatAllAction
- There is one menu item in Eclipse which is Java -> Code Style -> Clean Up
- This Clean Up can be used for
- Format a file (formatting options can be configured)
- Organize imports
- Remove unused variables
- Add serialize UID
- and few more....
- So, I created my own profile and applied required settings
- Run Action class for CleanUp
- And I am done.
In this approach you can only resolve problems which are available in Code Clean Up option while in #1 you can do anything which is available as proposal.
This way we can correct code up to certain extent. There are more action classes which are available and can be used. If I made any further changes then I will certainly update here.
Although now I am on my way of giving a thought if I can do same without opening IDE :)