]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
added changer; extract local refactoring now works at a minimum
authorAnna Eilertsen <anna.eilertsen@student.uib.no>
Mon, 5 Oct 2015 13:07:00 +0000 (15:07 +0200)
committerAnna Eilertsen <anna.eilertsen@student.uib.no>
Mon, 5 Oct 2015 13:07:00 +0000 (15:07 +0200)
software/no.uio.ifi.refaktor/.classpath
software/no.uio.ifi.refaktor/plugin.xml
software/no.uio.ifi.refaktor/src/ExtractLocalVariableWithAsserts.java [deleted file]
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/change/changers/ExtractLocalVariableWithAssertsChanger.java [new file with mode: 0644]
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/ExtractLocalVariableWithAssertsHandler.java [new file with mode: 0644]

index 7b2870a6076c8307b6642a38727205826b1b2177..afbe77155a520f52a8123d78b31b663ba8e6fe6e 100644 (file)
@@ -5,6 +5,7 @@
        <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins">
                <accessrules>
                        <accessrule kind="accessible" pattern="*"/>
+                       <accessrule kind="accessible" pattern="org/eclipse/jdt/internal/corext/refactoring/code/*"/>
                </accessrules>
        </classpathentry>
        <classpathentry kind="src" path="src"/>
index a40b5b8754c014c1461779ccc84428df8c141786..2e30f681225d6ca61e7199bbb8bbb8a9a1b42b10 100644 (file)
@@ -82,7 +82,7 @@
       </handler>
       <handler
                commandId="no.uio.ifi.refaktor.extractLocalVariableWithAsserts"
-               class="ExtractLocalVariableWithAsserts">
+               class="no.uio.ifi.refaktor.handlers.ExtractLocalVariableWithAssertsHandler">
              <enabledWhen>
                    <with
                          variable="selection">
diff --git a/software/no.uio.ifi.refaktor/src/ExtractLocalVariableWithAsserts.java b/software/no.uio.ifi.refaktor/src/ExtractLocalVariableWithAsserts.java
deleted file mode 100644 (file)
index 834d244..0000000
+++ /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 (file)
index 0000000..87220e5
--- /dev/null
@@ -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 (file)
index 0000000..be4a171
--- /dev/null
@@ -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
+
+       }
+
+}