Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ASTVisitor - geting line number of MethodDeclaration
ASTVisitor - geting line number of MethodDeclaration [message #758386] Wed, 23 November 2011 00:42 Go to next message
Eiji Adachi is currently offline Eiji AdachiFriend
Messages: 8
Registered: November 2011
Junior Member
I am implementing a parser with ASTVisitor and I want to store the initial and last line number (or offset from the initial line) of a method declaration.

i get the initial position:

final int startPosition = methodDeclaration.getStartPosition();

Then i get the offset:

final int offSet = methodDeclaration.getLength();

Then I try to retrieve this snippet by reading it directly from the source code:

final Reader in = new FileReader( "/workspace/Teste/src/Main.java" );
final BufferedReader reader = new BufferedReader( in );

reader.skip( startPosition );

final char[] buf = new char[3000];
reader.read( buf, 0, offSet );

The problem seems to be with my offset. Does anybody knows what am I doing wrong?

[Updated on: Wed, 23 November 2011 00:43]

Report message to a moderator

Re: ASTVisitor - geting line number of MethodDeclaration [message #758401 is a reply to message #758386] Wed, 23 November 2011 06:53 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
What is the problem you are running into?
Re: ASTVisitor - geting line number of MethodDeclaration [message #758446 is a reply to message #758386] Wed, 23 November 2011 10:28 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 11/23/2011 6:12 AM, eijiadachi wrote:
> I am implementing a parser with ASTVisitor and I want to store the
> initial and last line number (or offset from the initial line) of a
> method declaration.
>
> the initial line I get like this:
>
> final int startPosition = methodDeclaration.getStartPosition();
> final int lineNumber = compilationUnit.getLineNumber( startPosition );
>
> Then i get the offset:
>
> final int offSet = methodDeclaration.getLength();
>
> Then I try to retrieve this snippet by reading it directly from the
> source code:
>
> final Reader in = new FileReader( "/workspace/Teste/src/Main.java" );
> final BufferedReader reader = new BufferedReader( in );
>
> reader.skip( lineNumber );
Don't think you can skip lines via the skip() method.


> final char[] buf = new char[3000];
> reader.read( buf, 0, offSet );
>
> The problem seems to be with my offset. Does anybody knows what am I
> doing wrong?

You could use something like..
IBuffer buffer= compilationUnit.getBuffer();
String source= buffer.getText(method.getStartPosition(),
method.getLength());

(compilationUnit is of type org.eclipse.jdt.core.ICompilationUnit)
--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ
Re: ASTVisitor - geting line number of MethodDeclaration [message #758626 is a reply to message #758446] Wed, 23 November 2011 23:29 Go to previous messageGo to next message
Eiji Adachi is currently offline Eiji AdachiFriend
Messages: 8
Registered: November 2011
Junior Member
@Satyam Kandula my problem is that char [] buf does not contain the correct string corresponding to the source code of the methodDeclaration that I want to retrieve. It gets the initial part correctly, but it truncates the final part. That's why I think my problem is the way I'm getting the offset.

@Deepak Azad How do I get a reference to ICompilationUnit?
Re: ASTVisitor - geting line number of MethodDeclaration [message #758839 is a reply to message #758626] Thu, 24 November 2011 16:53 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
> @Deepak Azad How do I get a reference to ICompilationUnit?
Is your java file in an Eclipse project? Do you have an IJavaProject or
IProject object? Or are you using JDT/Core standalone ?

--
Deepak Azad
http://wiki.eclipse.org/JDT/FAQ
Re: ASTVisitor - geting line number of MethodDeclaration [message #758858 is a reply to message #758839] Thu, 24 November 2011 19:03 Go to previous messageGo to next message
Eiji Adachi is currently offline Eiji AdachiFriend
Messages: 8
Registered: November 2011
Junior Member
JDT/Core standalone.
Re: ASTVisitor - geting line number of MethodDeclaration [message #759359 is a reply to message #758858] Mon, 28 November 2011 13:33 Go to previous message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
You cannot get the ICompilationUnit in standalone.
The offset should be correct. How much truncation do you see?
Previous Topic:-----------------------
Next Topic:Is running 32bit/64bit Eclipse fine under 64bit Linux?
Goto Forum:
  


Current Time: Fri Mar 29 08:12:00 GMT 2024

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

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

Back to the top