Code Reformatting (IDEA, Jalopy)

S

stevengarcia

My class has an instance field with an underscore prefix, like

private String _message;

I want to reformat this class so it turns '_message' into 'message'.

In fact, I want to apply this logic to a whole directory/package of
java files. Is there a code reformatter that can do this?

I've looked into Jalopy (I don't think it can do this) and I also use
IDEA as IDE. I can invoke the "rename" function but only on a case by
case field. I simply want to remove all leading underscores on all
fields. Can Jalopy or IDEA do this? Can any other tool do this?
 
C

Chris Smith

I've looked into Jalopy (I don't think it can do this) and I also use
IDEA as IDE. I can invoke the "rename" function but only on a case by
case field. I simply want to remove all leading underscores on all
fields. Can Jalopy or IDEA do this? Can any other tool do this?

If "all identifiers" would work instead of "all fields", then sed will
do what you want since Java has no keywords with underscores.
Otherwise, I'm not aware of a smarter tool to do this.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

jeremiah johnson

My class has an instance field with an underscore prefix, like

private String _message;

I want to reformat this class so it turns '_message' into 'message'.

In fact, I want to apply this logic to a whole directory/package of
java files. Is there a code reformatter that can do this?

I've looked into Jalopy (I don't think it can do this) and I also use
IDEA as IDE. I can invoke the "rename" function but only on a case by
case field. I simply want to remove all leading underscores on all
fields. Can Jalopy or IDEA do this? Can any other tool do this?

That is called refactoring.

Eclipse can do it but you have to rename them one-by-one. Eclipse will
fix all the references to that variable across your classes, but you
still have to tell it what you want to change.

if you've stuck to a naming convention that won't cause collisions if
you just removed all the underscores, you can just do a search and replace.

replace " _" with " "

google for refactoring, there may be tools that do this for you.
 
R

Roedy Green

replace " _" with " "

The catch is going to be if there are already variables with those
names without the _. You want to check first and keep a backup.
 
S

stevengarcia

jeremiah said:
That is called refactoring.

Eclipse can do it but you have to rename them one-by-one. Eclipse will
fix all the references to that variable across your classes, but you
still have to tell it what you want to change.

if you've stuck to a naming convention that won't cause collisions if
you just removed all the underscores, you can just do a search and replace.

replace " _" with " "

google for refactoring, there may be tools that do this for you.

Yea I know this is refactoring...I basically thought of the same way
you suggested. Do a search for \s_[a-z] and replace all underscores
with nothing. And obviously use discretion when doing the replacing.
Not sure how to do a "replace with nothing" with regex. Hmm...
 
W

Wibble

Roedy said:
The catch is going to be if there are already variables with those
names without the _. You want to check first and keep a backup.

The other gotcha is when a stack and instance variable
have the same names, aside from the underscore. Thats
usually why people stick the '_' in front. You'll be
left with an ambiguity.
 
O

Oliver Wong

My class has an instance field with an underscore prefix, like

private String _message;

I want to reformat this class so it turns '_message' into 'message'.

In fact, I want to apply this logic to a whole directory/package of
java files. Is there a code reformatter that can do this?

I've looked into Jalopy (I don't think it can do this) and I also use
IDEA as IDE. I can invoke the "rename" function but only on a case by
case field. I simply want to remove all leading underscores on all
fields. Can Jalopy or IDEA do this? Can any other tool do this?

If you're familiar with parser generators (e.g. SableCC, JavaCC, ANTLR,
etc.) it's not too hard to write a custom tool to do this. You can download
a grammar for Java, then use your parser generator to generate a Java
parser. Run the java parser on the source code you want to refactor. You'll
end up with an AST representing your code. Now write a walker that descends
through the tree, and at every field declaration and field occurrence,
modify the AST to reflect the new name.

This is better than a simple blind search/blind or regular expression,
as it won't touch underscores that appear inside of Strings for example, but
it's not perfect in that it won't avoid name collisions. To handle those,
you'd have to start building a symbol table, and then you're really starting
to re-invent the wheel.

If you were using Eclipse, you could write an Eclipse plugin to handle
this. The Eclipse does constant compilation in the background, so it always
has the AST, symbol table, and all that compiler stuff ready for querying
all the time; you'd just have to learn the JDT (Java Development Tools) API
for Eclipse to hook into it.

I'm not familiar with any other IDE to know if they have a similar API
for you to use.

- Oliver
 
R

Roedy Green

If you're familiar with parser generators (e.g. SableCC, JavaCC, ANTLR,
etc.) it's not too hard to write a custom tool to do this.

A one-shot tool for this might be more easily done this way.

Collect a set of potential variable names to refactor by scanning for
_ then scanning for something outside the set 0-9a-zA-Z. IF you want
to be very clever, don't count hits inside "..." but there probably
won't be enough to make the coding effort worthwhile.

do a plain text search for these strings WITHOUT the _ and display the
context. Ideally you would find nothing. Generated Funduc
Search/Replace scripts are often how I do this. See
http://mindprod.com/jgloss/searchreplace.html


You will find some variable that are being using both in _xxx and xxx
form. Use Eclipse to manually globally rename both variables or assure
yourself the collision is harmless and manually rename the _ variable
to record that.

Repeat until there are no more collisions.

Now run your blanket _ remover from the strings you found.
 
R

Roedy Green

Collect a set of potential variable names to refactor by scanning for
_ then scanning for something outside the set 0-9a-zA-Z. IF you want
to be very clever, don't count hits inside "..." but there probably
won't be enough to make the coding effort worthwhile.
you can find these with a Funduc regex search.

On my todo list .is a Java Regex search replace utility like Funduc
but with Java style regexes.
 
C

Chris Smith

Roedy Green said:
you can find these with a Funduc regex search.

On my todo list .is a Java Regex search replace utility like Funduc
but with Java style regexes.

Funduc is very odd. I wouldn't recommend it to anyone. Their
proprietary syntax IS equivalent to the power of regular expressions, in
that it's capable of recognizing exactly the set of regular languages.
However, the fact is there's a very widespread general syntax (plus
nonportable extensions) for regular expressions, which funduc ignores
for no explicable reason. That standard syntax is what the Funduc
documentation page I'm looking at refers to "UNIX grep notation" and you
call "Java style regexes" above, but could equally be called "sed
notation" or "Perl notation" or a hundred other things, keeping in mind
that each environment provides relatively small extensions to the
industry-wide standard syntax. Most people just call it "regular
expressions", and this funduc tool is in that sense misrepresenting its
functionality.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top