|
|
|
Re: How to change the superclass extended by a type? [message #1385925 is a reply to message #1385497] |
Thu, 12 June 2014 09:49 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Joao L.![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=joaopclopes%2Beclipse%40gmail.com) Messages: 6 Registered: June 2014 |
Junior Member |
|
|
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 10:01] Report message to a moderator
|
|
|
|
Re: How to change the superclass extended by a type? [message #1385941 is a reply to message #1385934] |
Thu, 12 June 2014 11:43 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Joao L.![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=joaopclopes%2Beclipse%40gmail.com) Messages: 6 Registered: June 2014 |
Junior Member |
|
|
Thanks for the fast reply!
As for obtaining the ICompilationUnit, I'm parsing a project and loading each class within each package with the following code. I know this works because I'm able to change the imports of those classes using just "unit.createImport(newImport, null);". I don't use the AST to change the imports and I don't save the compilation unit.
IPackageFragment[] packages = newJavaProject.getPackageFragments();
for (IPackageFragment aPackage : packages) {
if (aPackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
// The compilation unit is a class
for (ICompilationUnit unit : aPackage.getCompilationUnits()) {
replaceImports(unit); // Replaces imports and sometimes changes the superclass
}
}
}
edit: It seems my TextEdit is actually emtpy (prints "{MultiTextEdit} [0,0] [undefined]")... But I'm changing the super class inside the ASTVisitor.
[Updated on: Thu, 12 June 2014 12:01] Report message to a moderator
|
|
|
|
Re: How to change the superclass extended by a type? [message #1385951 is a reply to message #1385947] |
Thu, 12 June 2014 12:34 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Joao L.![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=joaopclopes%2Beclipse%40gmail.com) Messages: 6 Registered: June 2014 |
Junior Member |
|
|
I still get "Document does not match the AST". I wonder if I'm getting the document correctly?
ITextFileBufferManager textFileBufferManager =
FileBuffers.getTextFileBufferManager();
try {
IPath path = unit.getPath();
textFileBufferManager.connect(path, LocationKind.IFILE, null);
ITextFileBuffer textFileBuffer = textFileBufferManager.getTextFileBuffer(unit.getPath(), LocationKind.IFILE);
IDocument document = textFileBuffer.getDocument();
return document;
} catch (CoreException e) {
e.printStackTrace();
return null;
}
[Updated on: Thu, 12 June 2014 12:36] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04299 seconds