Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » AST & Java Model
AST & Java Model [message #259826] Tue, 28 April 2009 07:03 Go to next message
Eclipse UserFriend
Hello,

I would like to know how i can go from AST nodes to using the Java Model
The reason is this:

QUESTIONS

1)
Like in my past posts i have a method which returns the "ICompilationUnit"
of a class(a source file). So when i get this "ICompilationUnit" how do i
access the fields and methods using the Java Model in place of using the
AST classes like FieldDeclaration or MethodDeclaration. I want to use the
Java Model but how do i use it after getting the ICompilationUnit of a
class.

1. ICompilationUnit icomp = this.findProject();
2. CompilationUnit comp = this.parse(icomp);

3. IMethod[] iff = ((TypeDeclaration)comp.types().get(0)).getFields();

Line 3 gives error because it can't convert from MethodDeclaration to
IMethod since they are two different thing.

So i want to know the correct answer.

2)
I want to have a method's signature but the AST nodes "MethodDeclaration"
don't have a method like that but IMethod has a method getSignature()
which i can use. If i know the answer to the first question then this is
solved automatically.


Thank you.
Re: AST & Java Model [message #259831 is a reply to message #259826] Tue, 28 April 2009 07:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: benno_baumgartner.ch.ibm.com

Hi Eddy
> I want to use the Java Model

Yeah!

> but how do i use it after getting the ICompilationUnit of a class.

See the code snippet I've provided in the answer to your very first post;-)

HTH
Benno
Re: AST & Java Model [message #259834 is a reply to message #259826] Tue, 28 April 2009 07:35 Go to previous messageGo to next message
Eclipse UserFriend
Eddy Freeman a écrit :
> Hello,
>
> I would like to know how i can go from AST nodes to using the Java
> Model The reason is this:
> QUESTIONS
>
> 1)
> Like in my past posts i have a method which returns the
> "ICompilationUnit" of a class(a source file). So when i get this
> "ICompilationUnit" how do i access the fields and methods using the Java
> Model in place of using the AST classes like FieldDeclaration or
> MethodDeclaration. I want to use the Java Model but how do i use it
> after getting the ICompilationUnit of a class.
>
> 1. ICompilationUnit icomp = this.findProject();
> 2. CompilationUnit comp = this.parse(icomp);
>
> 3. IMethod[] iff = ((TypeDeclaration)comp.types().get(0)).getFields();
>
> Line 3 gives error because it can't convert from MethodDeclaration to
> IMethod since they are two different thing.
>
> So i want to know the correct answer.
>
> 2)
> I want to have a method's signature but the AST nodes
> "MethodDeclaration" don't have a method like that but IMethod has a
> method getSignature() which i can use. If i know the answer to the first
> question then this is solved automatically.
>
>
> Thank you.
>

MethodDeclaration.resolveBinding().getJavaElement() -> give a IMethod
Re: AST & Java Model [message #259842 is a reply to message #259834] Tue, 28 April 2009 08:15 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I have used your code

IMethod im = MethodDeclaration.resolveBinding().getJavaElement();

and get an error that:
"can't make a static reference to a non -static method resolveBinding()."

so i changed it to

IMethod im = mf[2].resolveBinding().getJavaElement();

mf[2] is the third method in the source file

but i get an error which says :

"can't convert from IJavaElement to IMethod"

Can you please show me what i should do to fix it.

Thank you.
Re: AST & Java Model [message #259846 is a reply to message #259831] Tue, 28 April 2009 08:24 Go to previous messageGo to next message
Eclipse UserFriend
Hi Benno,

Thanks for your replies. This is the code snippet you provided in the
first post:

public void visit(IJavaElement element) {
//is the element the one?
if (element instanceof IParent) {
IJavaElement[] children= element.getChildren();
for (IJavaElement child: children) {
visit(child);
}
}
}

What i need now is what i should do before i can use this code snippet.
The question is, how can i get the IJavaElement from a source file before
i can use it as an attribute in the visit() method.

I need something like the following snippet(though it says that it can't
convert form IJavaElement to IMethod)

IMethod im = mf[2].resolveBinding().getJavaElement();

mf[2] is the third method in the source file

or a similar snippet which will show me how to get the IJavaElement from a
source file and after that i can check whether its an IMethod or IField
etc...


Thanks for all your replies.
Re: AST & Java Model [message #259851 is a reply to message #259842] Tue, 28 April 2009 08:28 Go to previous messageGo to next message
Eclipse UserFriend
Eddy Freeman a écrit :
> Hi,
>
> I have used your code
> IMethod im = MethodDeclaration.resolveBinding().getJavaElement();
>
> and get an error that:
> "can't make a static reference to a non -static method resolveBinding()."
>
> so i changed it to
> IMethod im = mf[2].resolveBinding().getJavaElement();
>
> mf[2] is the third method in the source file
>
> but i get an error which says :
>
> "can't convert from IJavaElement to IMethod"
>
> Can you please show me what i should do to fix it.
>
> Thank you.
>

getJavaElement() returns a IJavaElement.
You must cast it to IMethod.

Something like this i guess :
IMethod im = (IMethod) mf[2].resolveBinding().getJavaElement();
Re: AST & Java Model [message #259856 is a reply to message #259846] Tue, 28 April 2009 08:54 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: benno_baumgartner.ch.ibm.com

Eddy Freeman wrote:
> Hi Benno,
>
> Thanks for your replies. This is the code snippet you provided in the
> first post:
>
> public void visit(IJavaElement element) {
> //is the element the one?
> if (element instanceof IParent) {
> IJavaElement[] children= element.getChildren();
> for (IJavaElement child: children) {
> visit(child);
> }
> }
> }
>
> What i need now is what i should do before i can use this code snippet.
> The question is, how can i get the IJavaElement from a source file
> before i can use it as an attribute in the visit() method.

Like this:

void foo() {
visit(findProject());
}

public IJavaProject findProject(){
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// "yourplugins" is a project name
IProject project = root.getProject("yourplugins");
if (project.exists() && !project.isOpen())
try {
project.open(null /* IProgressMonitor */);
} catch (CoreException e1) {
e1.printStackTrace();
}

return JavaCore.create(project);
}

Benno
Re: AST & Java Model [message #259870 is a reply to message #259826] Tue, 28 April 2009 12:49 Go to previous messageGo to next message
Eclipse UserFriend
"Eddy Freeman" <win1for@yahoo.com> wrote in message
news:bb646f54b29b08dda6073325ee1c173c$1@www.eclipse.org...
> Hello,
>
> I would like to know how i can go from AST nodes to using the Java Model
> The reason is this:
> QUESTIONS
>
> 1)
> Like in my past posts i have a method which returns the "ICompilationUnit"
> of a class(a source file). So when i get this "ICompilationUnit" how do i
> access the fields and methods using the Java Model in place of using the
> AST classes like FieldDeclaration or MethodDeclaration. I want to use the
> Java Model but how do i use it after getting the ICompilationUnit of a
> class.

ICompilationUnit extends ITypeRoot. ITypeRoot extends IJavaElement. So,
ICompilationUnit *IS* part of the Java Model. You don't have to do anything
to get from it to the Java model - you just use it.

For instance:

process(ICompilationUnit cu) {
IType primary = cu.findPrimaryType(); // remember, a compilation unit
can contain secondary types also
IField[] fields = primary.fields();
}

I think it will help you if you start looking at the inheritance hierarchy
of these classes - you will start to understand the connections better.
Basically, anything that extends IJavaElement is part of the Java Model, and
anything that extends ASTNode is part of the AST.

Anyway, you do not need to use the AST at all for your project, from what I
can tell; and therefore if you find yourself calling parse() you are
probably doing something wrong.
Re: AST & Java Model [message #259881 is a reply to message #259870] Wed, 29 April 2009 05:44 Go to previous messageGo to next message
Eclipse UserFriend
What is the correct way to get AST for given IMember ?

Regards,
Chris
Re: AST & Java Model [message #259894 is a reply to message #259881] Wed, 29 April 2009 11:05 Go to previous message
Eclipse UserFriend
"Krzysztof Kowalczyk" <kowalczyk.krzysztof@gmail.com> wrote in message
news:gt97ht$ueo$1@build.eclipse.org...
> What is the correct way to get AST for given IMember ?

Get its ICompilationUnit (if it has one), and then parse it with an
ASTParser. There have been plenty of examples of this in recent threads.
Previous Topic:Can a Java project access an eclipse plug-in API?
Next Topic:CREATING NEW JAVA MODEL
Goto Forum:
  


Current Time: Fri May 09 03:41:12 EDT 2025

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

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

Back to the top