How do i get the Superclasses node in Eclipse jdt ui?
Asked Answered
H

1

6

I have a code here:

public class TestOverride {
    int foo() {
        return -1;
    }
}

class B extends TestOverride {
    @Override
    int foo() {
        // error - quick fix to add "return super.foo();"   
    }
}

As you can see I have mentioned the error. I'm trying to create a quickfix for this in eclipse jdt ui. But i'm unable to get the superclass node of the class B that is Class TestOverride.

I tried the following code

if(selectedNode instanceof MethodDeclaration) {
    ASTNode type = selectedNode.getParent();
    if(type instanceof TypeDeclaration) {
        ASTNode parentClass = ((TypeDeclaration) type).getSuperclassType();
    }
}

In here I got parentClass as TestOverride only. But when I checked this is not of the type TypeDeclaration it's not of type SimpleName either.

My query is how I get the class TestOverride node?

EDIT

  for (IMethodBinding parentMethodBinding :superClassBinding.getDeclaredMethods()){
     if (methodBinding.overrides(parentMethodBinding)){
        ReturnStatement rs = ast.newReturnStatement();
        SuperMethodInvocation smi = ast.newSuperMethodInvocation();
        rs.setExpression(smi);
        Block oldBody = methodDecl.getBody();
        ListRewrite listRewrite = rewriter.getListRewrite(oldBody, Block.STATEMENTS_PROPERTY);
        listRewrite.insertFirst(rs, null);
}
Hanaper answered 24/4, 2016 at 4:18 Comment(1)
Do you really need the TestOverride node if you just insert the return super.foo(); call?Improvised
I
5

You will have to work with bindings. To have bindings available, that means resolveBinding() not returning null, possibly additional steps I have posted are necessary.

To work with the bindings this visitor should help getting in the right direction:

class TypeHierarchyVisitor extends ASTVisitor {
    public boolean visit(MethodDeclaration node) {
        // e.g. foo()
        IMethodBinding methodBinding = node.resolveBinding();

        // e.g. class B
        ITypeBinding classBinding = methodBinding.getDeclaringClass();

        // e.g. class TestOverride
        ITypeBinding superclassBinding = classBinding.getSuperclass();
        if (superclassBinding != null) {
            for (IMethodBinding parentBinding: superclassBinding.getDeclaredMethods()) {
                if (methodBinding.overrides(parentBinding)) {
                    // now you know `node` overrides a method and
                    // you can add the `super` statement
                }
            }
        }
        return super.visit(node);
    }
}
Improvised answered 1/5, 2016 at 8:20 Comment(6)
thanks for the answer. But how should I get the node out of this.Hanaper
@Hanaper If you mean the superclass node TestOverride I am not sure for what you need that node.Improvised
I need it to get its declared methods, since i'm a beginner I don't know if there's any other way of getting it.Hanaper
@Hanaper you don't need the node. You can work with superclassBinding which represents TestOverride in this example (You could also get the ASTNode with the binding - see my first link - but it won't help you). As you see in my edit, there is a method overrides() for IMethodBinding available.Improvised
also I would like to know how to make the function return super.foo(). I have edited the code and now i'm getting this return super.MISSING() error.Hanaper
@Hanaper you need smi.setName(ast.newSimpleName(node.getName().getIdentifier())), where node is the MethodDeclarationImprovised

© 2022 - 2024 — McMap. All rights reserved.