Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » ASTParser Help
ASTParser Help [message #240639] Sun, 11 February 2007 09:32 Go to next message
Eclipse UserFriend
Originally posted by: nitrousfiz.googlemail.com

Hello,

Can I use Eclipse's ASTParser to parse java source code from a standalone
application (i.e. without the null pointed exception)?

If yes, which version should I use?
If not, what alternatives are there for parsing java code?

Here is what I need to do-
Use a parser to create an AST and extract the names of the methods, its
parameters & its parameter types. I also need to extract variable names &
variable types from a given set of statements.

Please help me out to get this done.

Thanks for your help in advance.

Regards,

Fayezin
Re: ASTParser Help [message #240643 is a reply to message #240639] Sun, 11 February 2007 09:39 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Fayezin,

I believe this should be possible. Perhaps if you show what you've
tried and show the exception stack trace someone, will know what you
need to do to avoid it.


Fayezin Islam wrote:
> Hello,
>
> Can I use Eclipse's ASTParser to parse java source code from a standalone
> application (i.e. without the null pointed exception)?
>
> If yes, which version should I use?
> If not, what alternatives are there for parsing java code?
>
> Here is what I need to do-
> Use a parser to create an AST and extract the names of the methods, its
> parameters & its parameter types. I also need to extract variable names &
> variable types from a given set of statements.
>
> Please help me out to get this done.
>
> Thanks for your help in advance.
>
> Regards,
>
> Fayezin
>
>
>
>
Re: ASTParser Help [message #240648 is a reply to message #240643] Sun, 11 February 2007 14:06 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nitrousfiz.googlemail.com

Hello,

This is the program I am trying to run:

import java.io.*;
import org.eclipse.jdt.core.dom.*;

public class ParseJava {

public static void main(String[] args) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(code.toCharArray());
parser.setKind(ASTParser.K_EXPRESSION);
ASTNode node = parser.createAST(null);

if (node.getNodeType() == ASTNode.METHOD_INVOCATION) {
MethodInvocation methodInvocation = (MethodInvocation) node;
System.out.println("Invoking method : "+methodInvocation.getName());
}
}
}

I get this error during compilation (maybe its asking for the runtime?)-
The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is
indirectly referenced from required .class files

I am using the org.eclipse.jdt.core_3.2.1.v_677_r32x.jar in my buildpath.
If its the wrong one, please advise which one to use?

Can you please help me solve this problem?

Thanks,

Fayezin

"Ed Merks" <merks@ca.ibm.com> wrote in message
news:eqn9rs$ql$1@utils.eclipse.org...
> Fayezin,
>
> I believe this should be possible. Perhaps if you show what you've tried
> and show the exception stack trace someone, will know what you need to do
> to avoid it.
>
>
> Fayezin Islam wrote:
>> Hello,
>>
>> Can I use Eclipse's ASTParser to parse java source code from a standalone
>> application (i.e. without the null pointed exception)?
>>
>> If yes, which version should I use?
>> If not, what alternatives are there for parsing java code?
>>
>> Here is what I need to do-
>> Use a parser to create an AST and extract the names of the methods, its
>> parameters & its parameter types. I also need to extract variable names &
>> variable types from a given set of statements.
>>
>> Please help me out to get this done.
>>
>> Thanks for your help in advance.
>>
>> Regards,
>>
>> Fayezin
>>
>>
>>
>>
Re: ASTParser Help [message #240663 is a reply to message #240648] Sun, 11 February 2007 16:43 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------070606070704050207030907
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Fayezin,

It sounds like you still need to put all of jdt's dependencies on the
classpath. E.g., IProgressMonitor is in org.eclips.equinox.common_*.jar.


Fayezin Islam wrote:
> Hello,
>
> This is the program I am trying to run:
>
> import java.io.*;
> import org.eclipse.jdt.core.dom.*;
>
> public class ParseJava {
>
> public static void main(String[] args) {
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(code.toCharArray());
> parser.setKind(ASTParser.K_EXPRESSION);
> ASTNode node = parser.createAST(null);
>
> if (node.getNodeType() == ASTNode.METHOD_INVOCATION) {
> MethodInvocation methodInvocation = (MethodInvocation) node;
> System.out.println("Invoking method : "+methodInvocation.getName());
> }
> }
> }
>
> I get this error during compilation (maybe its asking for the runtime?)-
> The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is
> indirectly referenced from required .class files
>
> I am using the org.eclipse.jdt.core_3.2.1.v_677_r32x.jar in my buildpath.
> If its the wrong one, please advise which one to use?
>
> Can you please help me solve this problem?
>
> Thanks,
>
> Fayezin
>
> "Ed Merks" <merks@ca.ibm.com> wrote in message
> news:eqn9rs$ql$1@utils.eclipse.org...
>
>> Fayezin,
>>
>> I believe this should be possible. Perhaps if you show what you've tried
>> and show the exception stack trace someone, will know what you need to do
>> to avoid it.
>>
>>
>> Fayezin Islam wrote:
>>
>>> Hello,
>>>
>>> Can I use Eclipse's ASTParser to parse java source code from a standalone
>>> application (i.e. without the null pointed exception)?
>>>
>>> If yes, which version should I use?
>>> If not, what alternatives are there for parsing java code?
>>>
>>> Here is what I need to do-
>>> Use a parser to create an AST and extract the names of the methods, its
>>> parameters & its parameter types. I also need to extract variable names &
>>> variable types from a given set of statements.
>>>
>>> Please help me out to get this done.
>>>
>>> Thanks for your help in advance.
>>>
>>> Regards,
>>>
>>> Fayezin
>>>
>>>
>>>
>>>
>>>
>
>
>


--------------070606070704050207030907
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Fayezin,<br>
<br>
It sounds like you still need to put all of jdt's dependencies on the
classpath.&nbsp; E.g., IProgressMonitor is in
org.eclips.equinox.common_*.jar.<br>
<br>
<br>
Fayezin Islam wrote:
<blockquote cite="mideqnpfi$t4g$1@utils.eclipse.org" type="cite">
<pre wrap="">Hello,

This is the program I am trying to run:

import java.io.*;
import org.eclipse.jdt.core.dom.*;

public class ParseJava {

public static void main(String[] args) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(code.toCharArray());
parser.setKind(ASTParser.K_EXPRESSION);
ASTNode node = parser.createAST(null);

if (node.getNodeType() == ASTNode.METHOD_INVOCATION) {
MethodInvocation methodInvocation = (MethodInvocation) node;
System.out.println("Invoking method : "+methodInvocation.getName());
}
}
}

I get this error during compilation (maybe its asking for the runtime?)-
The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is
indirectly referenced from required .class files

I am using the org.eclipse.jdt.core_3.2.1.v_677_r32x.jar in my buildpath.
If its the wrong one, please advise which one to use?

Can you please help me solve this problem?

Thanks,

Fayezin

"Ed Merks" <a class="moz-txt-link-rfc2396E" href="mailto:merks@ca.ibm.com">&lt;merks@ca.ibm.com&gt;</a> wrote in message
<a class="moz-txt-link-freetext" href="news:eqn9rs$ql$1@utils.eclipse.org">news:eqn9rs$ql$1@utils.eclipse.org</a>...
</pre>
<blockquote type="cite">
<pre wrap="">Fayezin,

I believe this should be possible. Perhaps if you show what you've tried
and show the exception stack trace someone, will know what you need to do
to avoid it.


Fayezin Islam wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hello,

Can I use Eclipse's ASTParser to parse java source code from a standalone
application (i.e. without the null pointed exception)?

If yes, which version should I use?
If not, what alternatives are there for parsing java code?

Here is what I need to do-
Use a parser to create an AST and extract the names of the methods, its
parameters &amp; its parameter types. I also need to extract variable names &amp;
variable types from a given set of statements.

Please help me out to get this done.

Thanks for your help in advance.

Regards,

Fayezin




</pre>
</blockquote>
</blockquote>
<pre wrap=""><!---->

</pre>
</blockquote>
<br>
</body>
</html>

--------------070606070704050207030907--
Re: ASTParser Help [message #240668 is a reply to message #240648] Sun, 11 February 2007 21:32 Go to previous messageGo to next message
Eclipse UserFriend
Fayezin Islam a écrit :
> I get this error during compilation (maybe its asking for the runtime?)-
> The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is
> indirectly referenced from required .class files
> I am using the org.eclipse.jdt.core_3.2.1.v_677_r32x.jar in my buildpath.
> If its the wrong one, please advise which one to use?
>
> Can you please help me solve this problem?
It can run inside a standalone application as long as you are not using
Javamodel element (IJavaProject, ICompilationUnit).
Even if you don't use any java element, you need to put some jars on the
classpath in order to find some classes required to resolve some signatures.
--
Olivier
Re: ASTParser Help [message #240713 is a reply to message #240668] Tue, 13 February 2007 19:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nitrousfiz.googlemail.com

Many thanks for your replies. It really helped a lot!

Now that I have added the requred jars, I am able to create a
CompilationUnit object from the source code.

I dont know how to extract the method & parameter names & variable names
from a CompilationUnit object.

Please advice how can I do it.

Thanks & Regards,

Fayezin


"Olivier Thomann" <olivier_thomann@ca.ibm.com> wrote in message
news:eqojka$9ki$1@utils.eclipse.org...
> Fayezin Islam a
Re: ASTParser Help [message #240718 is a reply to message #240713] Tue, 13 February 2007 19:45 Go to previous messageGo to next message
Eclipse UserFriend
Fayezin Islam a écrit :
> Many thanks for your replies. It really helped a lot!
> Now that I have added the requred jars, I am able to create a
> CompilationUnit object from the source code.
> I dont know how to extract the method & parameter names & variable names
> from a CompilationUnit object.
> Please advice how can I do it.
The CompilationUnit node that you have is a node tree.
If you want to extract method names, parameter names or variable names,
you can simply define a org.eclipse.jdt.core.dom.ASTVisitor. Then you
define the method like:
visit(MethodDeclaration)
visit(VariableDeclarationFragment)
visit(SingleVariableDeclaration).
If you have troub;e with this, you might want to read the article about AST.
See
http://www.eclipse.org/articles/Article-JavaCodeManipulation _AST/index.html.

HTH,
Olivier
Re: ASTParser Help [message #246155 is a reply to message #240713] Wed, 25 July 2007 10:49 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: chengt.uwm.edu

I tried your example, and I get this error. I'm using jar files from
eclipse 3.2.2.

Exception in thread "main" java.lang.NoClassDefFoundError:
org/eclipse/core/runtime/Plugin
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at
org.eclipse.jdt.core.dom.ASTParser.initializeDefaults(ASTPar ser.java:230)
at org.eclipse.jdt.core.dom.ASTParser.<init>(ASTParser.java:212)
at org.eclipse.jdt.core.dom.ASTParser.newParser(ASTParser.java: 112)
at test.Test.main(Test.java:23)



Fayezin Islam wrote:
> Many thanks for your replies. It really helped a lot!
>
> Now that I have added the requred jars, I am able to create a
> CompilationUnit object from the source code.
>
> I dont know how to extract the method & parameter names & variable names
> from a CompilationUnit object.
>
> Please advice how can I do it.
>
> Thanks & Regards,
>
> Fayezin
>
>
> "Olivier Thomann" <olivier_thomann@ca.ibm.com> wrote in message
> news:eqojka$9ki$1@utils.eclipse.org...
>> Fayezin Islam a écrit :
>>> I get this error during compilation (maybe its asking for the runtime?)-
>>> The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It
>>> is indirectly referenced from required .class files I am using the
>>> org.eclipse.jdt.core_3.2.1.v_677_r32x.jar in my buildpath.
>>> If its the wrong one, please advise which one to use?
>>>
>>> Can you please help me solve this problem?
>> It can run inside a standalone application as long as you are not using
>> Javamodel element (IJavaProject, ICompilationUnit).
>> Even if you don't use any java element, you need to put some jars on the
>> classpath in order to find some classes required to resolve some
>> signatures.
>> --
>> Olivier
>
>
Re: ASTParser Help [message #246159 is a reply to message #246155] Wed, 25 July 2007 12:54 Go to previous messageGo to next message
Eclipse UserFriend
Cheng Thao a écrit :
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/eclipse/core/runtime/Plugin
> at java.lang.ClassLoader.defineClass1(Native Method)
> at java.lang.ClassLoader.defineClass(Unknown Source)
> at java.security.SecureClassLoader.defineClass(Unknown Source)
> at java.net.URLClassLoader.defineClass(Unknown Source)
> at java.net.URLClassLoader.access$000(Unknown Source)
> at java.net.URLClassLoader$1.run(Unknown Source)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at java.lang.ClassLoader.loadClassInternal(Unknown Source)
> at
> org.eclipse.jdt.core.dom.ASTParser.initializeDefaults(ASTPar ser.java:230)
> at org.eclipse.jdt.core.dom.ASTParser.<init>(ASTParser.java:212)
> at org.eclipse.jdt.core.dom.ASTParser.newParser(ASTParser.java: 112)
> at test.Test.main(Test.java:23)
You can get this if you don't have all the required jars on your classpath.
--
Olivier
Re: ASTParser Help [message #246161 is a reply to message #246159] Wed, 25 July 2007 14:14 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: chengt.uwm.edu

Olivier Thomann wrote:
> Cheng Thao a écrit :
>> Exception in thread "main" java.lang.NoClassDefFoundError:
>> org/eclipse/core/runtime/Plugin
>> at java.lang.ClassLoader.defineClass1(Native Method)
>> at java.lang.ClassLoader.defineClass(Unknown Source)
>> at java.security.SecureClassLoader.defineClass(Unknown Source)
>> at java.net.URLClassLoader.defineClass(Unknown Source)
>> at java.net.URLClassLoader.access$000(Unknown Source)
>> at java.net.URLClassLoader$1.run(Unknown Source)
>> at java.security.AccessController.doPrivileged(Native Method)
>> at java.net.URLClassLoader.findClass(Unknown Source)
>> at java.lang.ClassLoader.loadClass(Unknown Source)
>> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
>> at java.lang.ClassLoader.loadClass(Unknown Source)
>> at java.lang.ClassLoader.loadClassInternal(Unknown Source)
>> at
>> org.eclipse.jdt.core.dom.ASTParser.initializeDefaults(ASTPar ser.java:230)
>> at org.eclipse.jdt.core.dom.ASTParser.<init>(ASTParser.java:212)
>> at org.eclipse.jdt.core.dom.ASTParser.newParser(ASTParser.java: 112)
>> at test.Test.main(Test.java:23)
> You can get this if you don't have all the required jars on your classpath.
> --
> Olivier

org/eclipse/core/runtime/content/IContentTypeManager$IConten tTypeChangeListener
at java.lang.ClassLoader.defineClass1(Native Method)

I added many more jar files. Now it needs the IContentTypeManager which
I can't seem to find the jar that has it. I assume it is in the
org.eclipse.core.runtime.jar. I tried that, but it didn't work. I look
at the source code in org.eclipse.core.runtime.content, but there is no
IContentTypeManager.

I'm using Eclipse 3.2.2.
Re: ASTParser Help [message #246170 is a reply to message #246161] Wed, 25 July 2007 15:19 Go to previous message
Eclipse UserFriend
Originally posted by: chengt.uwm.edu

Cheng Thao wrote:
> Olivier Thomann wrote:
>> Cheng Thao a écrit :
>>> Exception in thread "main" java.lang.NoClassDefFoundError:
>>> org/eclipse/core/runtime/Plugin
>>> at java.lang.ClassLoader.defineClass1(Native Method)
>>> at java.lang.ClassLoader.defineClass(Unknown Source)
>>> at java.security.SecureClassLoader.defineClass(Unknown Source)
>>> at java.net.URLClassLoader.defineClass(Unknown Source)
>>> at java.net.URLClassLoader.access$000(Unknown Source)
>>> at java.net.URLClassLoader$1.run(Unknown Source)
>>> at java.security.AccessController.doPrivileged(Native Method)
>>> at java.net.URLClassLoader.findClass(Unknown Source)
>>> at java.lang.ClassLoader.loadClass(Unknown Source)
>>> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
>>> at java.lang.ClassLoader.loadClass(Unknown Source)
>>> at java.lang.ClassLoader.loadClassInternal(Unknown Source)
>>> at
>>> org.eclipse.jdt.core.dom.ASTParser.initializeDefaults(ASTPar ser.java:230)
>>>
>>> at org.eclipse.jdt.core.dom.ASTParser.<init>(ASTParser.java:212)
>>> at org.eclipse.jdt.core.dom.ASTParser.newParser(ASTParser.java: 112)
>>> at test.Test.main(Test.java:23)
>> You can get this if you don't have all the required jars on your
>> classpath.
>> --
>> Olivier
>
> org/eclipse/core/runtime/content/IContentTypeManager$IConten tTypeChangeListener
>
> at java.lang.ClassLoader.defineClass1(Native Method)
>
> I added many more jar files. Now it needs the IContentTypeManager which
> I can't seem to find the jar that has it. I assume it is in the
> org.eclipse.core.runtime.jar. I tried that, but it didn't work. I look
> at the source code in org.eclipse.core.runtime.content, but there is no
> IContentTypeManager.
>
> I'm using Eclipse 3.2.2.

OK, this is fixed. I had to include the following jar files.

org.eclipse.core.contenttype.jar
org.eclipse.core.jobs.jar
org.eclipse.core.resources.jar
org.eclipse.runtime.jar
org.eclipse.equinox.common.jar
org.eclipse.equinox.preferences.jar
org.eclipse.jdt.core.jar
org.eclipse.osgi.jar
Previous Topic:Re: Request about Importing Java Compiler
Next Topic:eclipse plugin generator problem
Goto Forum:
  


Current Time: Sat Jul 19 05:37:38 EDT 2025

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

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

Back to the top