Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Recommended way to use AST to do template-based code gen?
Recommended way to use AST to do template-based code gen? [message #242811] Thu, 12 April 2007 19:12 Go to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

I'm trying to use AST to perform some code generation. The code generation
is going to be based off of a template file, as this will be far easier for
developers to modify that if they had to go in and hand code a bunch of AST
newXXX calls.

So I have a template file, which has legal Java source that I want to apply
some transformations to, and a target file, which is empty.

What's the recommended way to proceed here?

I've parsed the template, and have an AST and a CompilationUnit. I've been
able to modify it in memory, and it appears successful. But now, I don't
want to rewrite this back to the template file -- I want to write the
modified code into the (currently empty) target file.

Do I want to some sort of copySubTree operation here?

I'm wondering if there is a way to get a Document that is associated with
the target file, so when I invoke these lines.

I also have a problem where I get 'Invalid thread access' thrown when I try
to apply edits, but perhaps once I understand the right way to do this, that
issue will disappear. If not, I'll post code and a stack trace in another
post later.

If I have
Re: Recommended way to use AST to do template-based code gen? [message #242816 is a reply to message #242811] Thu, 12 April 2007 19:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

Sorry, that got sent prematurely, while still in the middle of editing.

But I think the basic question made it through; I'm just wondering what the
right idiom is for read from a file - modify the source - write it back to a
different file. I'm not sure if I should be working at the AST level (copy
nodes from one tree to another), or at the IFile level (file.setContents),
or something else entirely.

Thanks,
Mike


"Mike Yawn" <myawn@ebay.com> wrote in message
news:evmecg$24b$1@build.eclipse.org...
> I'm trying to use AST to perform some code generation. The code
> generation is going to be based off of a template file, as this will be
> far easier for developers to modify that if they had to go in and hand
> code a bunch of AST newXXX calls.
>
> So I have a template file, which has legal Java source that I want to
> apply some transformations to, and a target file, which is empty.
>
> What's the recommended way to proceed here?
>
> I've parsed the template, and have an AST and a CompilationUnit. I've
> been able to modify it in memory, and it appears successful. But now, I
> don't want to rewrite this back to the template file -- I want to write
> the modified code into the (currently empty) target file.
>
> Do I want to some sort of copySubTree operation here?
>
> I'm wondering if there is a way to get a Document that is associated with
> the target file, so when I invoke these lines.
>
> I also have a problem where I get 'Invalid thread access' thrown when I
> try to apply edits, but perhaps once I understand the right way to do
> this, that issue will disappear. If not, I'll post code and a stack trace
> in another post later.
>
> If I have
>
Re: Recommended way to use AST to do template-based code gen? [message #242838 is a reply to message #242816] Fri, 13 April 2007 07:31 Go to previous message
Eclipse UserFriend
Originally posted by: schmidts.iese.fraunhofer.de

Hi again, Mike

first of all, this InvalidThreadAccessException is thrown, if you try to
change some content of a UI element, i.e. a view or editor content, when
your code is run by a background thread. You have to wrap the content
changing code in a Runnable and use Display.synchExec(Runnable) or
Display.asynchExec(Runnable). The difference between these two is that the
first one waits for the Runnable to return and the second one goes on
immediately.

If I got you right, you read the AST of the template and change it with the
AST API. So I would create a new Document with the Content of the template
before you do the changes, then get the TextEdit by using rewrite(Document)
and apply the edit to the document. At this point you just changed the
Content of the Document and not the template. Now, instead of calling

template.getBuffer().setContent(document.get());

you just call:

newUnit.getBuffer().setContent(documen.get());
newUnit.getBuffer().save();

Cu, Seb
Previous Topic:Debugging java code invoked from a C++ application
Next Topic:Debugger gets disconnected
Goto Forum:
  


Current Time: Wed May 07 05:13:52 EDT 2025

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

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

Back to the top