Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How to parse ASTNode for IClassFile? (no attached source)
How to parse ASTNode for IClassFile? (no attached source) [message #1692556] Thu, 16 April 2015 09:11 Go to next message
Eclipse UserFriend
Hello!

I've opened a .class in the Class Editor, Eclipse says I have to attach some source, but in the outline I can see all the method signatures etc.

I've tryed to parse an AST like the following:

protected static CompilationUnit parse(IClassFile classFile) {
		ASTParser parser = ASTParser.newParser(AST.JLS4);
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		parser.setProject(classFile.getJavaProject());
		parser.setSource(classFile);
		parser.setResolveBindings(true);
		return (CompilationUnit) parser.createAST(null);
	}


(Also with K_CLASS_BODY_DECLARATIONS instead of K_COMPILATION_UNIT)

But I'm only getting java.lang.IllegalStateException

Is it somehow possible to get ASTNodes or JavaModelElements from an IClassFile without attached Source?
Re: How to parse ASTNode for IClassFile? (no attached source) [message #1692570 is a reply to message #1692556] Thu, 16 April 2015 10:27 Go to previous message
Eclipse UserFriend
Java model elements are easy. An IClassFile is a java model element, so you're already there. That's why you can see things in the outline.

You can't get an AST for it without source. You can, however, get IBindings for it if you can get an ASTNode somewhere else that uses that type. E.g. suppose you want information about type Bar, but you don't have the source for it. If you have an ICompilationUnit Foo that has a field of type Bar, you can get an ITypeBinding for Bar from the VariableDeclarationFragment for your field. IBindings carry a lot of useful information.
Previous Topic:LogicalStructureTypeDelegate no expansion
Next Topic:new module JBoss using Eclipse
Goto Forum:
  


Current Time: Thu Apr 17 10:35:04 EDT 2025

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

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

Back to the top