52

I am implementing an new eclipse refactoring. This will enable developers to Pull-up the preconditions statements from a child method to the parent method.

This all works perfectly when I select "Finish" in the refactoring wizard, but when I select "Preview" I get an error "No target edit provided." This seems to be caused by a problem in the TextEdit returned from ASTRewrite.rewriteAST(). However I cannot figure out why.

The stack trace of the exception happens after my Refactoring.createChange() code is run, and the change is used to generate the preview.

org.eclipse.text.edits.MalformedTreeException: No target edit provided.
at org.eclipse.text.edits.MoveSourceEdit.performConsistencyCheck(MoveSourceEdit.java:208)
at org.eclipse.text.edits.TextEdit.traverseConsistencyCheck(TextEdit.java:873)
at org.eclipse.text.edits.MoveSourceEdit.traverseConsistencyCheck(MoveSourceEdit.java:183)
at org.eclipse.text.edits.TextEdit.traverseConsistencyCheck(TextEdit.java:869)
at org.eclipse.text.edits.TextEdit.traverseConsistencyCheck(TextEdit.java:869)
at org.eclipse.text.edits.TextEditProcessor.checkIntegrityDo(TextEditProcessor.java:176)
at org.eclipse.text.edits.TextEdit.dispatchCheckIntegrity(TextEdit.java:743)
at org.eclipse.text.edits.TextEditProcessor.performEdits(TextEditProcessor.java:151)
at org.eclipse.ltk.core.refactoring.TextChange.getPreviewDocument(TextChange.java:534)
at org.eclipse.ltk.core.refactoring.TextChange.getPreviewDocument(TextChange.java:403)
at org.eclipse.ltk.core.refactoring.TextChange.getPreviewContent(TextChange.java:411)
at org.eclipse.ltk.internal.ui.refactoring.TextEditChangePreviewViewer.setInput(TextEditChangePreviewViewer.java:209)
at org.eclipse.ltk.internal.ui.refactoring.AbstractChangeNode.feedInput(AbstractChangeNode.java:99)
at org.eclipse.ltk.internal.ui.refactoring.PreviewWizardPage.showPreview(PreviewWizardPage.java:598)
at org.eclipse.ltk.internal.ui.refactoring.PreviewWizardPage.access$6(PreviewWizardPage.java:583)
at org.eclipse.ltk.internal.ui.refactoring.PreviewWizardPage$7.selectionChanged(PreviewWizardPage.java:574)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:162)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.runtime.Platform.run(Platform.java:888)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:48)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:160)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2132)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1669)
at org.eclipse.jface.viewers.TreeViewer.setSelection(TreeViewer.java:1124)
at org.eclipse.jface.viewers.Viewer.setSelection(Viewer.java:392)
at org.eclipse.ltk.internal.ui.refactoring.PreviewWizardPage.setVisible(PreviewWizardPage.java:505)
at org.eclipse.ltk.internal.ui.refactoring.RefactoringWizardDialog2.makeVisible(RefactoringWizardDialog2.java:762)
at org.eclipse.ltk.internal.ui.refactoring.RefactoringWizardDialog2.showCurrentPage(RefactoringWizardDialog2.java:477)
at org.eclipse.ltk.internal.ui.refactoring.RefactoringWizardDialog2.nextOrPreviewPressed(RefactoringWizardDialog2.java:507)
at org.eclipse.ltk.internal.ui.refactoring.RefactoringWizardDialog2.access$2(RefactoringWizardDialog2.java:492)
at org.eclipse.ltk.internal.ui.refactoring.RefactoringWizardDialog2$1.widgetSelected(RefactoringWizardDialog2.java:691)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3880)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3473)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
at org.eclipse.jface.window.Window.open(Window.java:801)
at org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation$1.run(RefactoringWizardOpenOperation.java:143)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation.run(RefactoringWizardOpenOperation.java:155)
at org.jmlspecs.eclipse.refactor.action.AbstractMethodActionDelegate.run(AbstractMethodActionDelegate.java:78)
at org.jmlspecs.eclipse.refactor.action.AbstractMethodActionDelegate.run(AbstractMethodActionDelegate.java:67)
at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:251)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:452)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3880)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3473)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2405)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2369)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
at org.eclipse.equinox.launcher.Main.run(Main.java:1311)
at org.eclipse.equinox.launcher.Main.main(Main.java:1287)

Currently the code that performs the change looks like this:

CompilationUnit sourceNode = ...
ASTRewrite sourceRewrite = ASTRewrite.create(sourceNode.getAST());

Statement statement = ...
sourceRewrite.createMoveTarget(statement);

CompilationUnit destinationNode = ...
MethodDeclaration destinationMethod = ...
ASTRewrite destinationRewrite = ASTRewrite.create(destinationNode.getAST());

ListRewrite lrw = destinationRewrite.getListRewrite(destinationMethod.getBody(),
    Block.STATEMENTS_PROPERTY);

lrw.insertFirst(statement, null);

I know that the way createMoveTarget is used is not how it is documented, but when I follow the documentation like below, the statement is deleted from the source but not moved to the destination and I still get the same error in the preview.

CompilationUnit sourceNode = ...
ASTRewrite sourceRewrite = ASTRewrite.create(sourceNode.getAST());

Statement statement = ...
Statement replacement = sourceRewrite.createMoveTarget(statement);
sourceRewrite.remove(statement, null);

CompilationUnit destinationNode = ...
MethodDeclaration destinationMethod = ...
ASTRewrite destinationRewrite = ASTRewrite.create(destinationNode.getAST());

ListRewrite lrw = destinationRewrite.getListRewrite(destinationMethod.getBody(),
    Block.STATEMENTS_PROPERTY);

lrw.insertFirst(replacement, null);

Here is an example of the Refactoring being performed.

Before:

class A {
    foo(int a) {
        return a * 2;
    }
}

class B {
    foo(int a) {
        JC.requires(a > 1);

        return a * 3;
    }
}

After:

class A {
    foo(int a) {
        JC.requires(a > 1);

        return a * 2;
    }
}

class B extends A {
    foo(int a) {        
        return a * 3;
    }
}
IAdapter
  • 62,595
  • 73
  • 179
  • 242
iain
  • 10,798
  • 3
  • 37
  • 41
  • 1
    Your refactoring example is not actually a recfactoring as it changes semantics but that's up to you. It's hard to tell what's wrong with the code since part of sources in stack are not available. Maybe you should track why MoveSourceEdit.setTargetEdit is not called or called with null argument. – Petr Gladkikh Dec 22 '09 at 09:50
  • 1
    Hi Petr, thanks for your suggestion. I will look into that in more detail and compare an eclipse refactoring to my own. Regarding the change of semantics when refactoring specifications, that is very interesting as it echos most academic work in the field. However Fowler's definition only preserves behavior, for example pull up method changes the semantics of the class hierarchy. I might add an option to preserve semantics by adding a new clause to the statement. Anyway I would be interested to discuss this further with you it you like. – iain Dec 30 '09 at 11:41

1 Answers1

2

I found the eclipse.org article the most helpful to get me started.

A good place to get started debugging the code is to set breakpoints on org.eclipse.jdt.core.dom.rewrite.ASTRewrite, particularly the rewriteAST() method, then trigger some refactorings.

Here are a few you might find useful. Are you looking for anything in particular or a general sense of how to process the AST?

http://www.ibm.com/developerworks/opensource/library/os-ast/  
http://blog.sahits.ch/?p=228
http://www.vogella.com/articles/EclipseJDT/article.html
mj-gholami
  • 883
  • 1
  • 6
  • 19
  • Hi, Thanks for the tips and links. I had seen the ibm and vogella links but not the other one. It is a long time since I worked on this code, it was part of a masters dissertation and functional enough to use. I will dig out the code over the coming weeks or so and see if I can address the issue. I got lost every time I debugged the jdt. The levels of indirection are endless and the stock refactorings are implemented with a private abstraction layer that makes them hard to decipher. – iain May 01 '12 at 16:15