Are there any tools out there to refactor the coding-style of a java code base?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 12:39:11

问题


Normally, when doing some work on an existing project, I would just go with whatever style is already established in the code base. But our team has to maintain multiple small to medium sized projects which all differ slightly in coding style. It would be more efficient and less confusing if we could cleanup these differences.

So I'm looking for a tool that allows me to refactor the existing style. Lots of features are already provided by standard code formatting tools, like changing the indentation style. What I'm missing is a tool that allows me to strip the prefixes of field and parameter names. In some projects all members are prefixed with "m", all parameters are prefixed with "p" and static members are prefixed with "s".

The tool should be able to handle cases like:

void setValue(String pValue) {
  mValue = pValue;
}

Which should become:

void setValue(String value) {
  this.value = value;
}

The tool should generate a warning in a case like this:

void setValue(String pValue) {
  int value = 42
}

I know every major IDE provides the refactor: rename feature. What I'm looking for though is a tool which processes a whole code base without me needing to go over each parameter/field individually.

Edit

A lot of the answers mention tools to re-format source code or check the code base against a set of style rules. I'm aware that those tools exist. What I'm specifically looking for is an advanced tool that allows me to remove scope specific variable name prefixes.


回答1:


I was looking for similar, or even more advanced tool. I was trying to implement design metrics, which are very context dependent. I found some tools, which are commercial and quite expensive like Ndepend and reflectk. I also found some open source tools, which are stalle like Hammurapi. There are also tools like Checkstyle, Findbugs and PMD, which can be used for very simple use cases. The best tool I found was MoDisco. Its based on Eclipse EMF, allows to generate model of your code and than do some querying or transformations (using M2M tools). The only problem I had was that it is quite slow for standard ~20k LOC projects. Give it a try, maybe for your use case it will suffice.




回答2:


Have you tried out Eclipse's Clean Up tool? Granted, it's not going to handle some of your examples shown here, but it does a pretty decent job in my opinion.




回答3:


I'm not aware of an existing project that would do this for you.

Have you considered writing your own? For example, the Eclipse framework does offer solid code formatting tools. Have you investigated extending one of them to do what you suggest? Detecting variables named with the pattern you mention should be fairly straightforward.

I realize it's not an ideal solution. BUT, if you have a large enough code base, it may be more cost effective than doing all the edits by hand.




回答4:


Sounds like something like Jalopy might help.

Jalopy allows you to define a code format style, and then it can read a file, package, or entire project and reformat it. I don't recall if it can do more sophisticated refactoring like changing the style of a local variable to not use Hungarian notation (the mVar crap), but it can change between whatever spacing, bracketing, ordering, and so on formatting you want.

I've used Jalopy in the past and it worked well. I have even used it on teams where people had different personal coding styles and then at commit we all agreed on a certain format that was automatically handled by Jalopy. (When you check it out you format it your way, when it gets checked back in it gets formatted the team agreed upon way, etc.)

One problem though, it looks like it hasn't been maintained for a year or so. But, there is a commercial version too that is much more recent -- seems the efforts went that direction. Still the older release might be fine.

And there are other similar commercial offerings like Jindent.




回答5:


Eclipse IDE is the best option for your problem..




回答6:


FindBugs is a very good tool for code static analysis. It really explores potential bugs and is easy to integrate in IDE and build tools.



来源:https://stackoverflow.com/questions/4768370/are-there-any-tools-out-there-to-refactor-the-coding-style-of-a-java-code-base

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!