JDT - Trying to change superclass of Type. I don't know the Qualified Name of a Super Class

北战南征 提交于 2020-01-02 12:09:26

问题


I have a program that, among other tasks, has to change the super class of some classes using JDT. I have two strings with the qualified name of the superclasses to swap, for example "org.example.John" and "org.example.Smith" and I'm parsing the whole AST searching for classes that extend these ones.

My problem happens when I'm trying to find out the qualified name of the super class. I just can't find a way. Given an ICompilationUnit (representing a class), I use a an ASTVisitor to parse the TypeDeclaration, as the following method shows.

public boolean visit(TypeDeclaration typeDeclaration) {
    TypeDeclaration[] types = typeDeclaration.getTypes();
    superClass = typeDeclaration.getSuperclassType();
    superClass.getNodeType();
    Class nodeType = ASTNode.nodeClassForType(superClass.getNodeType());
    if (newSuperClass != null) {
        Name oldName = typeDeclaration.getAST().newName(oldSuperClass);
        SimpleType oldType = typeDeclaration.getAST().newSimpleType(oldName);

        Name newName = typeDeclaration.getAST().newName(newSuperClass);
        SimpleType newType = typeDeclaration.getAST().newSimpleType(newName);

        if (superClass != null && superClass.equals(oldType)) {
            typeDeclaration.setSuperclassType(newType);             
        }
    }
    return true;
}

It it my understanding that Eclipse is capable of making the connection between the super class declaration (e.g. public class MySubClass extends John) and map it to the actual Type defined in the imports (e.g. import org.example.John). I'm looking for a way to do the same as Eclipse does.

Possible alternative solutions would be anything that would help me change the superclass of a class using JDT, given a plain text qualified name.

来源:https://stackoverflow.com/questions/24079862/jdt-trying-to-change-superclass-of-type-i-dont-know-the-qualified-name-of-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!