Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Jet2] Get CompilationUnit from newly generated file
[Jet2] Get CompilationUnit from newly generated file [message #20856] Thu, 07 June 2007 13:14 Go to next message
Aleks Dworak is currently offline Aleks DworakFriend
Messages: 8
Registered: July 2009
Junior Member
Hello,

I'm pretty new to Jet2 and I have the following problem to resolve :
I generate a java file (using ws:file, not java: because of several
reasons) in a Java project. For further treatment, I need to get the
CompilationUnit associated to this file.

I use the following java code for doing that :
IPath path = new Path(filePath);
IFile file = root.getFile(path);
IJavaElement jfile = JavaCore.create(file);
JavaCore.createCompilationUnitFrom(file);
Object[] elems = new Object[] { jfile };
IStructuredSelection structuredSelection = new StructuredSelection(elems);

I need the selection for the following treatments.
When the file is generated for the first time, I get an error message "the
selection contains no CompilationUnit".

First times, I called this code after ws:file, then I tried to put it at
the end of the main.jet template with an iteration on every java file I
generate.

My last try was to create another Jet transformation which only aim is to
run this java code on every javafile I generate, and to chain it after a
first Jet transformation in a "master" transformation as explained in
another thread.

I still get this error message, do you have some idea ?

Thanks.

Regards,
Aleks
Re: [Jet2] Get CompilationUnit from newly generated file [message #20883 is a reply to message #20856] Thu, 07 June 2007 15:24 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Aleks:

JET accumulates all file operations until main.jet has completed. It then
starts a single workspace operation, does appropriate validate edits (to
play well with the Team environment), and the writes the template results.

So, your code is executing too soon. And, on subsequent executions, it is
executing on the old version of the file rather than the new one.

It is possible to create a JET custom tag that would both do your operation
at the 'right' time. There are number of places where you can hook into
JET's content finalization process.

1) You can queue up your own 'action' that is performed during the above
workspace operation. In your tag's doAction event, it do something like:

WorkspaceContextExtender.getInstance(context).addAction(new
MyDeferedAction());

where MyDeferedAction implements IWorkspaceAction (there is an abstract
implementation that may be useful: AbstractWorkspaceAction). This is the
mechanism used by the ws:... tags. Look at the class FileAction for
inspiration.

2) You can participate in 'finalization' of the content to be written by an
workspace action. This is what tags such as <java:merge>, <c:userRegion>
and <java:importsLocation> use. Typically you use a custom JET tag in a
template which does two things. First, it places a 'marker' on the buffer
around content that needs finalization. Second, it registers a listener for
content finalization (which happens sometime after the template completes,
but before the buffer is written). When the listener is invoked, it finds
the markers on the buffer, and does its work. The 'markers' are implemented
by creating org.eclipse.jface.text.Position objects that are added to an
org.eclipse.jface.text.IDocument adapted from the JET writer). Take a look
at <java:importsLocation> (ImportsLocationTag) an examples. Tag handler
could would look something like:

if(out instanceof BufferedContentWriter) {
BufferedContentWriter bOut = (BufferedContentWriter)out;
IDocument document = (IDocument) bOut.getAdapter(IDocument.class);
... add any positions, position categories, position updaters to
document...

bOut.addEventListener(listenerID, new MyEventListener());
} else {
// tag can't operate - throw an exception - In JET 0.8, this never
happens, but it may happen later
}

MyEventListener implements IWriterListener, and does its work in
finalizeContent. It is passed the JET writer and the handle (currently
theIFile) to which the content will be written.

3) Lastly, you can receive 'post commit' notification that a workspace. This
is actually the same process as 2, as IWriterListener has two methods. For
post commit processing, implement IWriterListener.postCommitContent() - the
method receives the final content, and the handle (currently the IFile) to
which the content was written. As an example, look at <c:marker>
(MarkerTag), which puts a Task marker on text in the buffer.

I can't quite tell what you code is trying to do, but I'd guess that #2, or
perhaps #1 are the best choices for you.

Paul


"adworak" <aleks_dworak@fr.ibm.com> wrote in message
news:a89e0bbf1fa5e6fba6954ec7979b580a$1@www.eclipse.org...
> Hello,
>
> I'm pretty new to Jet2 and I have the following problem to resolve :
> I generate a java file (using ws:file, not java: because of several
> reasons) in a Java project. For further treatment, I need to get the
> CompilationUnit associated to this file.
>
> I use the following java code for doing that :
> IPath path = new Path(filePath);
> IFile file = root.getFile(path);
> IJavaElement jfile = JavaCore.create(file);
> JavaCore.createCompilationUnitFrom(file);
> Object[] elems = new Object[] { jfile };
> IStructuredSelection structuredSelection = new StructuredSelection(elems);
>
> I need the selection for the following treatments.
> When the file is generated for the first time, I get an error message "the
> selection contains no CompilationUnit".
>
> First times, I called this code after ws:file, then I tried to put it at
> the end of the main.jet template with an iteration on every java file I
> generate.
>
> My last try was to create another Jet transformation which only aim is to
> run this java code on every javafile I generate, and to chain it after a
> first Jet transformation in a "master" transformation as explained in
> another thread.
>
> I still get this error message, do you have some idea ?
>
> Thanks.
>
> Regards,
> Aleks
>
Re: [Jet2] Get CompilationUnit from newly generated file [message #21813 is a reply to message #20883] Fri, 08 June 2007 14:30 Go to previous message
Aleks Dworak is currently offline Aleks DworakFriend
Messages: 8
Registered: July 2009
Junior Member
Hi Paul,

Thank you so much for your help, I tried with your first solution (a
custom tag which queues my own IWorkspaceAction). It's working very well.

Aleks.
Previous Topic:generation of java code
Next Topic:[JET2] Run a JET transformation with a customed JET2Writer
Goto Forum:
  


Current Time: Tue Nov 29 22:22:31 GMT 2022

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

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

Back to the top