Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Add new method and position cursor in body
Add new method and position cursor in body [message #758854] Thu, 24 November 2011 18:09 Go to next message
Rupinder  is currently offline Rupinder Friend
Messages: 4
Registered: November 2011
Junior Member
I have a requirement to add a new method to a class and then open the class code and position the cursor in it's body. The approach I have is as under:

1. Use ASTRewrite to add the method at the correct location.
2. Add a //TODO comment in the body of the method. This should create a TaskMarker at that location.
3. Recocile the compilation unit
4. Open the editor at that marker location.

The approach works but I have to add a sleep for about a second after the reconcile operation for it to work.

The following code:
compilationUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); 
connectedCU = compilationUnit;
IType createdType = compilationUnit.getType(className);

 .... use ASTRewrite to create the method here

compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);

Thread.sleep(1000); //marker lookup does not work if I remove this line

connectedCU.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1));
}
finally
{
  if (connectedCU != null)
  {
    connectedCU.discardWorkingCopy();
  }
}

...

IResource resource = compilationUnit.getResource();
IMarker[] markers = resource.findMarkers(IMarker.TASK, true, IResource.DEPTH_INFINITE);


But if I remove the Thread.sleep after the reconcile statement the findMarkers does not find the marker I just added. It makes no sense as the entire code is executing in the same thread. What am I doing wrong here ?
Re: Add new method and position cursor in body [message #758860 is a reply to message #758854] Thu, 24 November 2011 19:19 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
The javadoc for org.eclipse.jdt.core.ICompilationUnit.reconcile(int,
boolean, boolean, WorkingCopyOwner, IProgressMonitor) says
"Compilation problems found in the new contents are notified through the
IProblemRequestor interface which was passed at creation, and no longer
as transient markers. "

Why don't you use this mechanism?

--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ
Re: Add new method and position cursor in body [message #758866 is a reply to message #758860] Thu, 24 November 2011 20:10 Go to previous messageGo to next message
Rupinder  is currently offline Rupinder Friend
Messages: 4
Registered: November 2011
Junior Member
Deepak,

I didn't get your answer. My requirement is not the problems or the markers. The real requirement is to position the cursor at the body of the method and I am trying to use the marker for that.

So how would that be accomplished with your suggestion ?

Re: Add new method and position cursor in body [message #758891 is a reply to message #758854] Fri, 25 November 2011 05:13 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
> compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
I think reconcile method spawns a new thread internally.

> But if I remove the Thread.sleep after the reconcile statement the
> findMarkers does not find the marker I just added. It makes no sense as
> the entire code is executing in the same thread. What am I doing wrong
> here ?



--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ
Re: Add new method and position cursor in body [message #758896 is a reply to message #758891] Fri, 25 November 2011 05:46 Go to previous message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
reconcile happens in the same thread. reconcile doesn't create markers. It is the build that really creates the markers.
Previous Topic:Search for constructor invocations with parameters
Next Topic:Shortcut for "Open Annotated Classes"
Goto Forum:
  


Current Time: Wed Apr 24 17:56:45 GMT 2024

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

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

Back to the top