Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Code Reformatting (IDEA, Jalopy)

Thread view: 
stevengarcia@yahoo.com - 23 Mar 2006 07:28 GMT
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?
Chris Smith - 23 Mar 2006 10:44 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

bugbear - 27 Mar 2006 11:28 GMT
>>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
[quoted text clipped - 4 lines]
> do what you want since Java has no keywords with underscores.  
> Otherwise, I'm not aware of a smarter tool to do this.

I've found this helpful over the years, for rather
many purposes.

http://groups.google.com/group/comp.os.linux.misc/browse_frm/thread/a5a884acb511
95f2/69adecc73d642279?lnk=st&q=rfr++%22Recursive+Find+Replace%22&rnum=1&hl=en#69
adecc73d642279


(John Hunter's RFR "Recursive Find Replace")

   BugBear
jeremiah johnson - 23 Mar 2006 12:32 GMT
> My class has an instance field with an underscore prefix, like
>
[quoted text clipped - 9 lines]
> 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.
Roedy Green - 23 Mar 2006 18:44 GMT
On Thu, 23 Mar 2006 11:32:01 GMT, jeremiah johnson
<naikrovek@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>    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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Wibble - 24 Mar 2006 13:24 GMT
> On Thu, 23 Mar 2006 11:32:01 GMT, jeremiah johnson
> <naikrovek@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 4 lines]
> 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.
stevengarcia@yahoo.com - 23 Mar 2006 23:00 GMT
> > My class has an instance field with an underscore prefix, like
> >
[quoted text clipped - 22 lines]
>
> 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...
Oliver Wong - 24 Mar 2006 16:35 GMT
> My class has an instance field with an underscore prefix, like
>
[quoted text clipped - 9 lines]
> 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
Roedy Green - 24 Mar 2006 19:45 GMT
>    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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 24 Mar 2006 21:13 GMT
On Fri, 24 Mar 2006 18:45:42 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Smith - 27 Mar 2006 16:32 GMT
>  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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.