How to change the superclass extended by a type? [message #1385451] |
Fri, 06 June 2014 10:02  |
Eclipse User |
|
|
|
Hey all,
I'm developing a plugin that, among other things, has to change imports and some superclasses (I have a text file indicating which types must be changed to which other types). I'm using an ASTVisitor and extending visit(TypeDeclaration). When I finally do typeDeclaration.setSuperclassType(newType); nothing seems to happen to the actual compilation unit. In debug, I can see that the TypeDeclaration parameter was updated, but in the end, the generated code remained unchanged. Any clues? I would also appreciate any tips in how to debug this further. It's hard to understand what's going on behind the scenes in ASTVisitor.
package plugin;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.TypeDeclaration;
public class SuperClassVisitor extends ASTVisitor{
public SimpleType superClass;
public String newSuperClass;
private String oldSuperClass;
public SuperClassVisitor(String newType, String oldType) {
// HACK: use simple name instead of qualified name
// This returns the simple name from the fully qualified name
String[] newTypes = newType.split("\\.");
String[] oldTypes = oldType.split("\\.");
this.newSuperClass = newTypes[newTypes.length-1];
this.oldSuperClass = oldTypes[oldTypes.length-1];
}
public boolean visit(TypeDeclaration typeDeclaration) {
superClass = (SimpleType) typeDeclaration.getSuperclassType();
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.getName().getFullyQualifiedName().equals(oldType.getName().getFullyQualifiedName())) {
typeDeclaration.setSuperclassType(newType);
}
}
return false;
}
}
|
|
|
|
|
Re: How to change the superclass extended by a type? [message #1385925 is a reply to message #1385497] |
Thu, 12 June 2014 05:49   |
Eclipse User |
|
|
|
Thanks for pointing me in that direction. I'm now using ASTRewrite, but nothing happens, just like before. My code:
public static void setSuperclass(ICompilationUnit unit,
String newImportName, String oldImportName) {
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
//create a ASTRewrite
astRoot.recordModifications();
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
// Visit the node and perform the changes needed
SuperClassVisitor visitor = new SuperClassVisitor(newImportName, oldImportName);
astRoot.accept(visitor);
// True if any change was made during the visit
if (visitor.changed) {
try {
IDocument document = getDocument(unit);
TextEdit textEdits = rewrite.rewriteAST(document, null);
textEdits.apply(document);
} catch (MalformedTreeException | BadLocationException e) {
e.printStackTrace();
}
}
}
I run this in debug and the change is indeed happening in the node, as the code from the first post shows. Do I need anything other than rewriteAST and apply?
edit: When I try to do "astRoot.rewrite(document, null);" it throws "java.lang.IllegalArgumentException: Document does not match the AST"
[Updated on: Thu, 12 June 2014 06:01] by Moderator
|
|
|
|
|
|
|
|
|
Re: How to change the superclass extended by a type? [message #1386009 is a reply to message #1385984] |
Thu, 12 June 2014 17:11  |
Eclipse User |
|
|
|
Quote:
Caused by: org.eclipse.core.runtime.CoreException: End Of File
at org.eclipse.jdt.internal.core.dom.rewrite.TokenScanner.readNext(TokenScanner.java:92)
at org.eclipse.jdt.internal.core.dom.rewrite.TokenScanner.readToToken(TokenScanner.java:149)
at org.eclipse.jdt.internal.core.dom.rewrite.TokenScanner.readToToken(TokenScanner.java:162)
at org.eclipse.jdt.internal.core.dom.rewrite.TokenScanner.getTokenEndOffset(TokenScanner.java:187)
at org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.visit(ASTRewriteAnalyzer.java:1840)
... 79 more
have a look at the Scanner inside that TokenScanner: does it have the expected content? (us its toString() output in the debugger).
S.
|
|
|
Powered by
FUDForum. Page generated in 0.06922 seconds