Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Using AST to parse my code with eclipse 3.2
Using AST to parse my code with eclipse 3.2 [message #235877] Thu, 14 September 2006 01:39 Go to next message
Eclipse UserFriend
Originally posted by: diegobt.gmail.com

Hi All:
I want to use Eclipses ASTParser to parse a .java and generate an AST to
print it.
After reading this
http://www-128.ibm.com/developerworks/opensource/library/os- ast/?ca=dgr-lnxw97ASTParser

I tried to get this snippet to do it.

File file = new File("ClassA.java");
BufferedReader in = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String line = null;
while (null != (line = in.readLine())) {
buffer.append("\t" + line);
buffer.append("\n");
}

ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
String text = buffer.toString();
parser.setSource(text.toCharArray());
CompilationUnit node = (CompilationUnit) parser.createAST(null);
ASTVisitor visitor = new ASTPrintVisitor();
node.accept(visitor);

But I get this:

Exception in thread "main" java.lang.NoClassDefFoundError:
org/eclipse/core/runtime/Plugin
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at
java.security.SecureClassLoader.defineClass(SecureClassLoade r.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268 )
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319 )
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 ASTPrint.main(ASTPrint.java:18)

I've been debugging it but cant figure out what to do. Perhaps someone
could help me out.
Thanks in advance!
/diego
Re: Using AST to parse my code with eclipse 3.2 [message #235887 is a reply to message #235877] Thu, 14 September 2006 08:37 Go to previous messageGo to next message
Philippe Mulet is currently offline Philippe MuletFriend
Messages: 229
Registered: July 2009
Senior Member
This error means you are running your code outside an instance of
running Eclipse (this is why the plugin layer isn't initialized).

This being said, since 3.2M6, we allow creating DOM ASTs outside Eclipse
with some limitation (no binding). Please check
https://bugs.eclipse.org/bugs/show_bug.cgi?id=87852 and see if you
deviate from the scenario supported there.

Diego Tiscornia wrote:
> Hi All:
> I want to use Eclipses ASTParser to parse a .java and generate an AST to
> print it.
> After reading this
> http://www-128.ibm.com/developerworks/opensource/library/os- ast/?ca=dgr-lnxw97ASTParser
>
>
> I tried to get this snippet to do it.
> File file = new File("ClassA.java");
> BufferedReader in = new BufferedReader(new FileReader(file));
> StringBuffer buffer = new StringBuffer();
> String line = null;
> while (null != (line = in.readLine())) {
> buffer.append("\t" + line);
> buffer.append("\n");
> }
>
> ASTParser parser = ASTParser.newParser(AST.JLS2);
> parser.setKind(ASTParser.K_COMPILATION_UNIT);
> String text = buffer.toString();
> parser.setSource(text.toCharArray());
> CompilationUnit node = (CompilationUnit) parser.createAST(null);
> ASTVisitor visitor = new ASTPrintVisitor();
> node.accept(visitor);
>
> But I get this:
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/eclipse/core/runtime/Plugin
> at java.lang.ClassLoader.defineClass1(Native Method)
> at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
> at
> java.security.SecureClassLoader.defineClass(SecureClassLoade r.java:124)
> at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
> at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
> at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268 )
> at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
> at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319 )
> 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 ASTPrint.main(ASTPrint.java:18)
>
> I've been debugging it but cant figure out what to do. Perhaps someone
> could help me out.
> Thanks in advance!
> /diego
>
Re: Using AST to parse my code with eclipse 3.2 [message #658561 is a reply to message #235877] Wed, 09 March 2011 00:58 Go to previous message
Babloo is currently offline BablooFriend
Messages: 12
Registered: September 2009
Junior Member
You are missing "org.eclipse.core.resources" as one of the main thread's dependency.
Previous Topic:Content Assist [Intellisense] Sending Machine to 100% CPU
Next Topic:"create project from existing source" option
Goto Forum:
  


Current Time: Tue Apr 23 09:33:47 GMT 2024

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

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

Back to the top