From: Anna Eilertsen Date: Mon, 5 Oct 2015 13:07:00 +0000 (+0200) Subject: added changer; extract local refactoring now works at a minimum X-Git-Url: http://git.uio.no/git/?a=commitdiff_plain;h=3561fca608335bc5e83506b9ea6516e4e9ee62fa;p=ifi-stolz-refaktor.git added changer; extract local refactoring now works at a minimum --- diff --git a/software/no.uio.ifi.refaktor/.classpath b/software/no.uio.ifi.refaktor/.classpath index 7b2870a6..afbe7715 100644 --- a/software/no.uio.ifi.refaktor/.classpath +++ b/software/no.uio.ifi.refaktor/.classpath @@ -5,6 +5,7 @@ + diff --git a/software/no.uio.ifi.refaktor/plugin.xml b/software/no.uio.ifi.refaktor/plugin.xml index a40b5b87..2e30f681 100644 --- a/software/no.uio.ifi.refaktor/plugin.xml +++ b/software/no.uio.ifi.refaktor/plugin.xml @@ -82,7 +82,7 @@ + class="no.uio.ifi.refaktor.handlers.ExtractLocalVariableWithAssertsHandler"> diff --git a/software/no.uio.ifi.refaktor/src/ExtractLocalVariableWithAsserts.java b/software/no.uio.ifi.refaktor/src/ExtractLocalVariableWithAsserts.java deleted file mode 100644 index 834d2445..00000000 --- a/software/no.uio.ifi.refaktor/src/ExtractLocalVariableWithAsserts.java +++ /dev/null @@ -1,44 +0,0 @@ -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.commands.IHandler; -import org.eclipse.core.commands.IHandlerListener; - -public class ExtractLocalVariableWithAsserts implements IHandler { - - @Override - public void addHandlerListener(IHandlerListener handlerListener) { - // TODO Auto-generated method stub - - } - - @Override - public void dispose() { - // TODO Auto-generated method stub - - } - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - System.out.println("it's working"); - return null; - } - - @Override - public boolean isEnabled() { - // TODO Auto-generated method stub - return true; - } - - @Override - public boolean isHandled() { - // TODO Auto-generated method stub - return true; - } - - @Override - public void removeHandlerListener(IHandlerListener handlerListener) { - // TODO Auto-generated method stub - - } - -} diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/change/changers/ExtractLocalVariableWithAssertsChanger.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/change/changers/ExtractLocalVariableWithAssertsChanger.java new file mode 100644 index 00000000..87220e5e --- /dev/null +++ b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/change/changers/ExtractLocalVariableWithAssertsChanger.java @@ -0,0 +1,122 @@ +package no.uio.ifi.refaktor.change.changers; + +import java.util.Random; + +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.IPackageBinding; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.internal.core.CompilationUnit; +import org.eclipse.jdt.internal.corext.refactoring.code.ExtractTempRefactoring; +import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil; +import org.eclipse.jdt.internal.corext.util.JavaModelUtil; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.ChangeDescriptor; +import org.eclipse.text.edits.MalformedTreeException; + +import no.uio.ifi.refaktor.analyze.exceptions.RefaktorAnalyzerException; +import no.uio.ifi.refaktor.refactorings.visitors.InsertAssertsVisitor; +import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection; + +/** + * Changer for Extract Local Variable with Asserts Refactoring + * + * + * @author Anna Eilertsen + */ +public class ExtractLocalVariableWithAssertsChanger implements RefaktorChanger{ + private CompilationUnitTextSelection cstextsel; + private ExtractTempRefactoring refactorer; + + public ExtractLocalVariableWithAssertsChanger(CompilationUnitTextSelection textSelection) { + cstextsel = textSelection; + refactorer = new ExtractTempRefactoring(textSelection.getCompilationUnit(), textSelection.getOffset(), textSelection.getLength()); + } + + @Override + public void checkPreconditions() throws RefaktorAnalyzerException { + try { + refactorer.checkInitialConditions(new NullProgressMonitor()); + } catch (CoreException e) { + throw new RefaktorAnalyzerException(e) { + }; + } + } + + @Override + public void execute(IProgressMonitor monitor) throws CoreException { + String name = generateName(); + refactorer.setTempName(name); + refactorer.checkFinalConditions(monitor); + + Change change = refactorer.createChange(monitor); + change.perform(monitor); + + ChangeDescriptor k = change.getDescriptor(); + CompilationUnit cu = (CompilationUnit) change.getModifiedElement(); + Object o = change.getModifiedElement().getClass(); + insertAsserts(cu, name, cstextsel.getText()); + + change.dispose(); + int i = 0; + + } + + /** + * Initiates the insertion of asserts + * + * @param cu The changed compilation unit + * @param temp The name of the extracted variable + * @param text the text originally selected, i.e. referring the obj the temp is now pointing to + */ + private void insertAsserts(CompilationUnit cu, String temp, String text) { +// IPackageBinding o = cu.getPackage().resolveBinding(); +// IType tx = (IType) JavaModelUtil.findTypeContainer(o.getJavaElement().getJavaProject(), originalTarget.getType().getQualifiedName()); +// //Lookup on name: guaranteed fresh => unique +// IMethod m = null; +// for (IMethod n : tx.getMethods()) { +// if (n.getElementName().equals(postExecutionResources.getExtractedMethod().getElementName())) { +// m = n; break; +// } +// } +// assert m != null; +// InsertAssertsVisitor insertassertsvisitor = new InsertAssertsVisitor(originalTarget, postExecutionResourcesMethod); +// MethodDeclaration methodDeclarationNode = ASTNodeSearchUtil.getMethodDeclarationNode(m, cu); +// methodDeclarationNode.accept(insertassertsvisitor); +// insertassertsvisitor.getRewrite().rewriteAST(document, null).apply(document); +// +// +// // commit changes to underlying file +// textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); // (3) +// +// } catch (CoreException | MalformedTreeException | IllegalArgumentException | BadLocationException e) { +// throw new RefaktorAnalyzerException(e) { +// }; +// } finally { +// try { +// bufferManager.disconnect(path, null); +// } catch (CoreException e) { +// throw new RefaktorAnalyzerException(e) { +// }; +// } // (4) +// } +// } + } + + public static String generateName() { + return "temp_" + Math.abs(new Random().nextLong()); + } +} diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/ExtractLocalVariableWithAssertsHandler.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/ExtractLocalVariableWithAssertsHandler.java new file mode 100644 index 00000000..be4a171b --- /dev/null +++ b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/ExtractLocalVariableWithAssertsHandler.java @@ -0,0 +1,74 @@ +package no.uio.ifi.refaktor.handlers; +import org.eclipse.jface.viewers.TreePath; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.commands.IHandlerListener; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeSelection; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.handlers.HandlerUtil; + +import com.sun.xml.internal.txw2.Document; + +import no.uio.ifi.refaktor.change.changers.ExtractLocalVariableWithAssertsChanger; +import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection; +import no.uio.ifi.refaktor.utils.DocumentUtils; + +public class ExtractLocalVariableWithAssertsHandler implements IHandler { + + @Override + public void addHandlerListener(IHandlerListener handlerListener) { + // TODO Auto-generated method stub + + } + + @Override + public void dispose() { + // TODO Auto-generated method stub + + } + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + CompilationUnitTextSelection cstextsel = DocumentUtils.getCompilationUnitTextSelectionFromExecutionEvent(event); + + ExtractLocalVariableWithAssertsChanger changer = new ExtractLocalVariableWithAssertsChanger(cstextsel); + changer.checkPreconditions(); + try { + changer.execute(new NullProgressMonitor()); + } catch (CoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + return null; + } + + @Override + public boolean isEnabled() { + // TODO Auto-generated method stub + return true; + } + + @Override + public boolean isHandled() { + // TODO Auto-generated method stub + return true; + } + + @Override + public void removeHandlerListener(IHandlerListener handlerListener) { + // TODO Auto-generated method stub + + } + +}