Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » A zany refactoring question
A zany refactoring question [message #153860] Thu, 06 November 2003 22:21 Go to next message
Eclipse UserFriend
Originally posted by: johnhkc.earthlink.net

I'd like to know if the following is possible using Eclipse.
This is for use in a code generator.

Imagine I have a piece of Java code that stretches over many lines and
builds a String like this:
String mystr = "the " +
"cow " +
"said " +
"moo " +
"moo " +
"moo " +
"! ";

I want to write a refactoring routine where the user hilights a line that
will have a condition added. For example, I could highlight the last
"moo" and the refactoring routine would add an IF so that the code changes
to this:
String mystr = "the " +
"cow " +
"said " +
"moo " +
"moo ";
if ( someFlag == true )
{
mystr += "moo ";
}
mystr += "!";

Is there a way to programmatically parse through the String declaration,
see the last "moo", end the statement before it, surround the last "moo"
with a condition and append the trailing "!" as in the code above?
Re: A zany refactoring question [message #153926 is a reply to message #153860] Fri, 07 November 2003 04:53 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: akiezun.cuthis.mit.edu.andthis

the jdt group would be better for your question
look into ASTRewrite and its friends
it's internal stuff for now but there's work on making it API for 3.0

a.
Re: A zany refactoring question [message #153965 is a reply to message #153860] Fri, 07 November 2003 08:04 Go to previous message
Ilja Preuss is currently offline Ilja PreussFriend
Messages: 105
Registered: July 2009
Senior Member
John H wrote:
> I want to write a refactoring routine where the user hilights a line
> that will have a condition added. For example, I could highlight the
> last "moo" and the refactoring routine would add an IF so that the
> code changes to this:
> String mystr = "the " +
> "cow " +
> "said " +
> "moo " +
> "moo ";
> if ( someFlag == true )
> {
> mystr += "moo ";
> }
> mystr += "!";

If the code

String mystr = "the " +
"cow " +
"said " +
"moo " +
"moo " +
(someFlag ? "moo " : "") +
"!";

would be ok, too (it should be equivalent), this could be easily done using
code templates:

(${condition} ? ${word_selection} : "")

Take care, Ilja
Previous Topic:Using external JARs in a PlugIn
Next Topic:Add Block Comments to Text Editor
Goto Forum:
  


Current Time: Sat Dec 21 13:20:21 GMT 2024

Powered by FUDForum. Page generated in 0.03777 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top