Erhan
Erhan

Reputation: 53

How Can a SuperClass name be retrieved from a java file using ASTParser?

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...

Upvotes: 1

Views: 901

Answers (3)

Dan Gravell
Dan Gravell

Reputation: 8260

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.

Upvotes: 2

Vinay Lodha
Vinay Lodha

Reputation: 2223

This code will work..

IType type = ...;

ITypeHierarchy  a = type.newSupertypeHierarchy(new NullProgressMonitor());
        IType[] superTypes =  a.getAllSupertypes(type);
        if(superTypes.length == 1 && superTypes[0].getKey().equals("Ljava/lang/Object;")){
            this.isSuperClassObject = true;
        }
        else{
            this.isSuperClassObject = false;
        }

Upvotes: 0

user130076
user130076

Reputation:

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

getSuperClass()

Upvotes: 1

Related Questions