Is there a quick way to refactor the entire codebase to adopt a different code convention?

StackOverflow https://stackoverflow.com/questions/20355558

  •  28-08-2022
  •  | 
  •  

Domanda

I have received ownership of a code base that, although very well written, uses a rather bizarre convention:

public void someMethod(String pName, Integer pAge, Context pContext) 
{ 
    ... 
}

I'd like to make the following two changes to the entire code:

public void someMethod(String name, Integer age, Context context) { ... }
  1. Opening bracket in the same line of the method declaration
  2. Use a camelCase name for all parameters of the method, without this weird "p" prefix

Can checkstyle help me here? I'm looking but I can't find a way to rename all parameters in all method signatures to something more pleasant.

È stato utile?

Soluzione

If you are willing to use the eclipse IDE it'd offer a very handy feature for auto-formatting code:

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcodestyle%2Fref-preferences-formatter.htm

It is pretty self-explanatory and straight-forward in my opinion.


Eclipse allows for regex based search and replace operations.

Just open Search > File... there enter the following regex for Containing text:

\b[p]([A-Z][a-z]+)\b

And tick both Case sensitive and Regular expression. Then press Replace...

In the newly popped up window enter

\1

in the With: field and tick Regular expression.

Edit: Sadly in its current version Eclipse does not support the \L flag for content groups so you are still stuck with an uppercase leading letter.


To answer your question about checkstyle: No, checkstyle is a tool used for analyzing code not for changing.

Using checkstyle to format code (Question from Oct'12)


Also did some research, here's another stackoverflow question aiming at the practically same. The solution offered there is similarly work intensive.

Can I automatically refactor an entire java project and rename uppercase method parameters to lowercase? (Question from Oct'10)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top