Convert camel-lowercase to camel-upperCase automatically

1.7k Views Asked by At

I'm looking for an easy way to convert all variables and function names from lower_case to upperCase in a java code. There's really a lot of code, refactoring the names one by one isn't a good idea.

It means that this function

public void create_table(String table_name)
{
    int useless_var = 0;

    useless_function("string_param");
    // This comment should not be altered, even_if_I_use_some_underscores
}

should become

public void createTable(String tableName)
{
    int uselessVar = 0;

    uselessFunction("string_param"); // "string_param" must not become "stringParam"
    // Comments should be left untouched, even_if_I_use_some_underscores
}

and so on.

I know I can do it with a Python script, but it will take some time and I can easily make a mistake like converting something which is not an identifier. That's why I want to know if there's a tool or something to do this.

3

There are 3 best solutions below

1
On BEST ANSWER

Most Java IDEs can cascade the changes you make to one variable where ever it is used. I believe tools like PMD can find these cases but might not help in fixing them.

If this is a working application, you might want to make corrections as you perform maintenance. Instead of updating the entire project at once.

2
On

Use the refactoring tool in an IDE.

I use Eclipse, where refactoring is right-click on the method or variable, Refactor > Rename. Type the new name in place of the old name, hit 'return', and it will be renamed everywhere in your code. Note, it won't work on your string "string_param" though, you'll have to do that manually.

Hope that helps!

0
On

You might be able to do this with WildEdit.