问题
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