Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community ForumsASTVisitor - geting line number of MethodDeclaration
https://www.eclipse.org/forums/index.php/mv/msg/262579/758386/#msg_758386
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?]]>Eiji Adachi2011-11-23T00:42:19-00:00Re: ASTVisitor - geting line number of MethodDeclaration
https://www.eclipse.org/forums/index.php/mv/msg/262579/758401/#msg_758401
Satyam Kandula2011-11-23T06:53:50-00:00Re: ASTVisitor - geting line number of MethodDeclaration
https://www.eclipse.org/forums/index.php/mv/msg/262579/758446/#msg_758446
> 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]]>Deepak Azad2011-11-23T10:28:57-00:00Re: ASTVisitor - geting line number of MethodDeclaration
https://www.eclipse.org/forums/index.php/mv/msg/262579/758626/#msg_758626
@Deepak Azad How do I get a reference to ICompilationUnit?]]>Eiji Adachi2011-11-23T23:29:01-00:00Re: ASTVisitor - geting line number of MethodDeclaration
https://www.eclipse.org/forums/index.php/mv/msg/262579/758839/#msg_758839
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]]>Deepak Azad2011-11-24T16:53:46-00:00Re: ASTVisitor - geting line number of MethodDeclaration
https://www.eclipse.org/forums/index.php/mv/msg/262579/758858/#msg_758858
Eiji Adachi2011-11-24T19:03:11-00:00Re: ASTVisitor - geting line number of MethodDeclaration
https://www.eclipse.org/forums/index.php/mv/msg/262579/759359/#msg_759359
The offset should be correct. How much truncation do you see? ]]>Satyam Kandula2011-11-28T13:33:21-00:00