views:

93

answers:

2

I need to retrieve SuperClass name of a java file (which extends a class, i need that class name). In order to do this, I began to use ASTParser but I'm newbie so any example or illustration can be helpful. I use ASTView to comprehend which ways that AST keeps a java file structure but I'm stuck while trying visit(), endvisit() functions. I could not find any detailed example.

Thanks in advance...

+1  A: 

Could you use the getSuperClass() method of the Class class in your code instead?

getSuperClass()

Peter
No, actually I'm devoloping an eclipse plugin to parse all java files that is under the selected package. So I have ICompilationUnit ready to be parsed with ASTParser. And I want to decide which type of java file I have. In this project Type can be determined by only retrieveing which superclass that file extends. So I have to use ASTParser and visit() functions. But I'm newbie at this subjects. Any example of traversing AST for a file and retrieving data from it can be helpful. Thanks by the way.
Erhan
+1  A: 

You didn't say much of how far you had got already.

In the visitor, override visit(TypeDeclaration node) and then, on the node, call getSuperclassType().

If you return true the parsing will continue inside the type declaration, otherwise it stops.

Dan Gravell
thanks; It worked well. Is there any site that explains these visit functions in details that you know?
Erhan
The first thing is to make sure you understand the Java Model stored in Eclipse. Just knowing what a TypeDeclaration is would help, and you could build your knowledge from there. You've probably already seen it but, just in case, I'd have to recommend the Eclipse help... under the JDT Plug-in Developer Guide see 'Java Model' and 'Manipulating Java Code'.http://www.ibm.com/developerworks/opensource/library/os-ast/ documents some general uses of the ASTParsing stuff.
Dan Gravell