Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Composition of Refactorings(How to compose two or more refactorings into a single undoable action?)
Composition of Refactorings [message #1132921] Fri, 11 October 2013 13:32
Erlend Kristiansen is currently offline Erlend KristiansenFriend
Messages: 2
Registered: September 2013
Junior Member
I'm working on a project for a master's thesis, and I'm trying to make composite refactorings while utilizing some of the the basic refactorings already existing in Eclipse. (Children of org.eclipse.ltk.core.refactoring.Refactoring.) Specifically I'm trying to extract a method based on a selection, and then move this method to another class. I want it to happen as an "atomic" operation, in the sense that it appears to the user as being a single action, and that it can be undone in one step.

I've bumped into for example CompositeChange, but it does not apply directly to my case, since my refactorings are interfering with each other. What is the preferred way, if there is any, to combine two or more refactorings in a way that I have described?

Is there any way of chaining the two CreateChangeOperations, and pass the result to the PerformChangeOperation?

Code examples follows:

...
ICompilationUnit cu = RefaktorHandleUtils.findType(editorProject, packageName, simpleTypeName).getCompilationUnit();

ExtractMethodRefactoring extractMethodRefactoring = makeExtractMethodRefactoring(prefix, cu);
CheckConditionsOperation checkConditionsOperation = new CheckConditionsOperation(extractMethodRefactoring, CheckConditionsOperation.ALL_CONDITIONS);
CreateChangeOperation createChangeOperation = makeCreateChangeOperation(checkConditionsOperation);
makeAndRunPerformChangeOperation(monitor, undoManager, undoName, createChangeOperation);

Refactoring moveRefactoring = makeMoveRefactoring(prefix, packageName, simpleTypeName, extractMethodRefactoring);
CheckConditionsOperation checkConditionsOperation2 = new CheckConditionsOperation(moveRefactoring, CheckConditionsOperation.ALL_CONDITIONS);
CreateChangeOperation createChangeOperation2 = makeCreateChangeOperation(checkConditionsOperation2);
makeAndRunPerformChangeOperation(monitor, undoManager, undoName, createChangeOperation2);
...


public void makeAndRunPerformChangeOperation(IProgressMonitor monitor, IUndoManager undoManager,
        String undoName, CreateChangeOperation createChangeOperation) throws CoreException {
    PerformChangeOperation performChangeOperation = new PerformChangeOperation(createChangeOperation);
    // setUndoManager is required to put the refactoring into the history:
    performChangeOperation.setUndoManager(undoManager, undoName);
    performChangeOperation.run(monitor);
}


Another approach I envision, is to do refactorings in a sequence, first in a "sandbox" to check that they are behaving, and then replay
them for real afterwards. With this approach, I would also need to generate some sort of undo action, that could rewind the editing history to the point in time where the sequence began. But I run into problems already by the "sandbox". For instance, I tried to make the "cu" from the code above a working copy, and then discard the changes before moving the method, just to trigger an exception, but it doesn't seem to matter.

This topic is somewhat discussed in an old forum post, but I wonder if anyone have something to add?

Erlend Kristiansen,
University of Oslo, Norway

(This topic was previously created in the Eclipse Platform forum. http://www.eclipse.org/forums/index.php/mv/msg/533112/1118745/#msg_1118745)
Previous Topic:Classpath Dependency Validator for user libraries
Next Topic:Fastest way to find all subtypes
Goto Forum:
  


Current Time: Sat Jan 18 09:51:00 GMT 2025

Powered by FUDForum. Page generated in 0.03275 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top