+++ /dev/null
-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
-
- }
-
-}
--- /dev/null
+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());
+ }
+}
--- /dev/null
+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
+
+ }
+
+}