Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » method's first line of code identification
method's first line of code identification [message #221092] Fri, 23 December 2005 13:31 Go to next message
Eclipse UserFriend
Originally posted by: jedi_il.hotmail.com

Hi,

I have an IMethod object, and i wish to identify the first line of code in
that method.
One solution i thought of was using method.getSource() and start
manipulating the source code in the string level, i can do it but before i
do, i wanted to know if perhaps the JDT already offers some more high
level way of doing that, somehow breaking lines and interpreting them.

Thanks
- Liran.
Re: method's first line of code identification [message #221100 is a reply to message #221092] Fri, 23 December 2005 19:44 Go to previous messageGo to next message
Eclipse UserFriend
Yes, you can using the AST model to do that, please reference to the
following code sample.

IMethod method;
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(method.getCompilationUnit());
MethodDeclaration methodDeclaration = (MethodDeclaration)
NodeFinder.perform(parser.createAST(null), lMethod.getSourceRange());
System.out.println(methodDeclaration.getBody().statements(). get(0));



Liran wrote:
> Hi,
>
> I have an IMethod object, and i wish to identify the first line of code
> in that method.
> One solution i thought of was using method.getSource() and start
> manipulating the source code in the string level, i can do it but before
> i do, i wanted to know if perhaps the JDT already offers some more high
> level way of doing that, somehow breaking lines and interpreting them.
>
> Thanks
> - Liran.
>
Re: method's first line of code identification [message #221108 is a reply to message #221100] Sat, 24 December 2005 13:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jedi_il.hotmail.com

Hi,
Thanks for the reply !

I've tired the code you showed me, and i did some reading about the AST -
this was just was i was looking for but i just have 2 problems which looks
like they are connected:

1. in your code you've used NodeFinder, it looks like its a part of the
jdt internal classes and eclipse warns me about using it, but it did work,
is it ok using it externally ?

2. How do i traverse on AST ? i didnt see any get children or something
like that, even in ASTNode. I did see the class ASTVisitor but it looks
like its for marking which nodes you've already traversed, im intresting
in more like enumerating the nodes, getting them and reading them. How is
it done ? (just via NodeFinder ?)

Thanks !

eddie wrote:

> Yes, you can using the AST model to do that, please reference to the
> following code sample.

> IMethod method;
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(method.getCompilationUnit());
> MethodDeclaration methodDeclaration = (MethodDeclaration)
> NodeFinder.perform(parser.createAST(null), lMethod.getSourceRange());
> System.out.println(methodDeclaration.getBody().statements(). get(0));



> Liran wrote:
>> Hi,
>>
>> I have an IMethod object, and i wish to identify the first line of code
>> in that method.
>> One solution i thought of was using method.getSource() and start
>> manipulating the source code in the string level, i can do it but before
>> i do, i wanted to know if perhaps the JDT already offers some more high
>> level way of doing that, somehow breaking lines and interpreting them.
>>
>> Thanks
>> - Liran.
>>
Re: method's first line of code identification [message #221152 is a reply to message #221108] Tue, 27 December 2005 22:25 Go to previous message
Eclipse UserFriend
You can traverse the ASTNode by casting the ASTNode to the specify
class. For example: TypeDeclaration , MethodDeclaration ...

Liran wrote:
> Hi, Thanks for the reply !
>
> I've tired the code you showed me, and i did some reading about the AST
> - this was just was i was looking for but i just have 2 problems which
> looks like they are connected:
>
> 1. in your code you've used NodeFinder, it looks like its a part of the
> jdt internal classes and eclipse warns me about using it, but it did
> work, is it ok using it externally ?
>
> 2. How do i traverse on AST ? i didnt see any get children or something
> like that, even in ASTNode. I did see the class ASTVisitor but it looks
> like its for marking which nodes you've already traversed, im intresting
> in more like enumerating the nodes, getting them and reading them. How
> is it done ? (just via NodeFinder ?)
>
> Thanks !
>
> eddie wrote:
>
>> Yes, you can using the AST model to do that, please reference to the
>> following code sample.
>
>
>> IMethod method;
>> ASTParser parser = ASTParser.newParser(AST.JLS3);
>> parser.setSource(method.getCompilationUnit());
>> MethodDeclaration methodDeclaration = (MethodDeclaration)
>> NodeFinder.perform(parser.createAST(null), lMethod.getSourceRange());
>> System.out.println(methodDeclaration.getBody().statements(). get(0));
>
>
>
>
>> Liran wrote:
>>
>>> Hi,
>>>
>>> I have an IMethod object, and i wish to identify the first line of
>>> code in that method.
>>> One solution i thought of was using method.getSource() and start
>>> manipulating the source code in the string level, i can do it but
>>> before i do, i wanted to know if perhaps the JDT already offers some
>>> more high level way of doing that, somehow breaking lines and
>>> interpreting them.
>>>
>>> Thanks
>>> - Liran.
>>>
>
>
>
Previous Topic:How to extend JavaProjectWizard?
Next Topic:Extension points or something for custom refactorings?
Goto Forum:
  


Current Time: Sun Nov 09 18:30:54 EST 2025

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

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

Back to the top