Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Getting a previous ASTNode
Getting a previous ASTNode [message #210993] Wed, 27 July 2005 09:45 Go to next message
Eclipse UserFriend
Originally posted by: prasadmpatil.gmail.com

Hi,

Given a handle to a ASTNode I am trying to get its predecessor ASTNode
in the CompilationUnit. Is this possible and if yes how can it be done?

---------------------------
For eg:-

public class B extends C
{
@foobar()
public int foo()
{
//some code here
}

}

Given a handle to ASTNode for 'method foo()', I want to get the ASTNode
corresponding to 'annotation foobar'.

----------------------------

Somebody please help me out.
Thanks in advance

~Prasad
Re: Getting a previous ASTNode [message #211001 is a reply to message #210993] Wed, 27 July 2005 10:00 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomann.NOca.ibm.comSPAM

Prasad Patil a écrit :
> Given a handle to ASTNode for 'method foo()', I want to get the ASTNode
> corresponding to 'annotation foobar'.
The node corresponding to the annotation foobar is part of the method
declaration. It is located in its modifiers list. So not sure this
correspond to the "previous" node. I understand "previous" node as the
previous sibling of the current node. This is not the case.
Why do you want it?
--
Olivier
Re: Getting a previous ASTNode [message #211026 is a reply to message #210993] Wed, 27 July 2005 10:32 Go to previous messageGo to next message
Eclipse UserFriend
Hello Prasad,

In this case your ast node is a MethodDeclaration, yes? You can then call
methodDeclaration.modifiers() and iterate through the modifiers looking for
an Annotation. If you are looking to get the actual previous statement I
believe you have to iterate through the declarations for that

//get the first type in the compilation unit, this is your class
TypeDeclaration type = (TypeDeclaration) CompilationUnit.types().get(0);
List bodyDeclarations = type.bodyDeclarations();

This gives you a list of all the BodyDeclarations which covers. You can
search this for the declaration you are looking for and then examine the
previous declaration in the list. You will have to check the modifiers for
fields and methods to determine if they are annotated and revise your code
accordingly.

I hope this helps,

Dylan


>
> Given a handle to a ASTNode I am trying to get its predecessor ASTNode
> in the CompilationUnit. Is this possible and if yes how can it be
> done?
>
> ---------------------------
> For eg:-
> public class B extends C
> {
> @foobar()
> public int foo()
> {
> //some code here
> }
> }
>
> Given a handle to ASTNode for 'method foo()', I want to get the
> ASTNode corresponding to 'annotation foobar'.
>
> ----------------------------
>
> Somebody please help me out.
> Thanks in advance
> ~Prasad
>
Re: Getting a previous ASTNode [message #211032 is a reply to message #211026] Wed, 27 July 2005 10:36 Go to previous messageGo to next message
Eclipse UserFriend
Ignore the 'which covers' in the previous post. Check the source for BodyDeclaration:
http://docjar.com/docs/api/org/eclipse/jdt/core/dom/BodyDecl aration.html
to get a list of the covered classes.

Dylan
Re: Getting a previous ASTNode [message #211113 is a reply to message #210993] Wed, 27 July 2005 17:29 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Prasad Patil" <prasadmpatil@gmail.com> wrote in message
news:69625599b5b8ee62f6ecb5f9d5cb2823$1@www.eclipse.org...
> [...]
> Given a handle to ASTNode for 'method foo()', I want to get the ASTNode
> corresponding to 'annotation foobar'.

Depending on what you are doing, you might want to take note of the work
being done in the APT branch of JDT; we are working to add apt-style
annotation processing to Eclipse, supporting annotation processors which
talk to the com.sun.mirror.apt API. There's a modified version of the
o.e.jdt.core plugin on the APT branch, and there are o.e.jdt.apt.* plugins
on the HEAD. No binary distribution yet.
Re: Getting a previous ASTNode [message #211210 is a reply to message #211026] Fri, 29 July 2005 08:11 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: prasadmpatil.gmail.com

Thanks Olivier & Dylan. Using MethodDeclarations.modifiers() I was able to
get the Annotations list :).

However as I understand(correct me if I am wrong) modifiers() method is
not available on some other Java elements such as Class(CompilationUnit)
which can also be annotated using annotations. Is there any other way by
which I can get annotations on these Java elements.

TIA
~Prasad
Re: Getting a previous ASTNode [message #211218 is a reply to message #211210] Fri, 29 July 2005 10:27 Go to previous messageGo to next message
Eclipse UserFriend
Hello Prasad,

The CompilationUnit contains a number of AbstractTypeDeclarations available
under CompilationUnit.types. The first one should be your class. AbstractTypeDeclaration
extends BodyDeclaration, which declares the modifiers() method, so that should
pull up the list of class level modifiers, if I am not mistaken. The unit
test is not cooperating with me this morning. Let me know if this works out.

Hope this helps,

Dylan

> Thanks Olivier & Dylan. Using MethodDeclarations.modifiers() I was
> able to get the Annotations list :).
>
> However as I understand(correct me if I am wrong) modifiers() method
> is not available on some other Java elements such as
> Class(CompilationUnit) which can also be annotated using annotations.
> Is there any other way by which I can get annotations on these Java
> elements.
>
> TIA ~Prasad
>
Re: Getting a previous ASTNode [message #211268 is a reply to message #211218] Mon, 01 August 2005 05:08 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: prasadmpatil.gmail.com

Thanks Dylan. It worked for me :).

This is how I got the list of modifiers on a CompilationUnit.

List l
=((TypeDeclaration)((CompilationUnit)astNode).types().get(0) ).modifiers();

~Prasad
Re: Getting a previous ASTNode [message #211314 is a reply to message #211268] Mon, 01 August 2005 12:56 Go to previous message
Eclipse UserFriend
Glad to hear it :)

Dylan
Previous Topic:column select or block select
Next Topic:excluding directories
Goto Forum:
  


Current Time: Tue Jul 08 03:09:24 EDT 2025

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

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

Back to the top