From 36f783ff6e00a44dfe1d65c0c337691f7d67bf78 Mon Sep 17 00:00:00 2001 From: Erlend Kristiansen Date: Mon, 8 Apr 2013 21:36:42 +0200 Subject: [PATCH] SaferRefactoring with assert statement working on 'a simple example', but still contains a lot of debug output. --- .../examples/src/aSimpleExample/C.java | 3 +- .../no/uio/ifi/refaktor/SaferRefactoring.java | 37 ++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/software/no.uio.ifi.refaktor.examples/examples/src/aSimpleExample/C.java b/software/no.uio.ifi.refaktor.examples/examples/src/aSimpleExample/C.java index 5bd60c23..450406a8 100644 --- a/software/no.uio.ifi.refaktor.examples/examples/src/aSimpleExample/C.java +++ b/software/no.uio.ifi.refaktor.examples/examples/src/aSimpleExample/C.java @@ -14,7 +14,8 @@ public class C { } public static void main(String[] args) { - new C().f(); + C c = new C(); + c.f(); } } \ No newline at end of file diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/SaferRefactoring.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/SaferRefactoring.java index 39ce7bf0..f30a238f 100644 --- a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/SaferRefactoring.java +++ b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/SaferRefactoring.java @@ -11,9 +11,11 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.IVariableBinding; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.VariableDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.core.refactoring.CompilationUnitChange; import org.eclipse.jdt.core.refactoring.IJavaRefactorings; import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringContribution; @@ -38,7 +40,7 @@ public class SaferRefactoring extends RefaktorRefactoring { public SaferRefactoring() throws CoreException { super(); setProjectName("examples"); - setMethod(getMethod("aPackage.Utils", "foo", new String[] {"QT1;", "QT2;"})); + setMethod(getMethod("aSimpleExample.C", "f", new String[] {})); } /** @@ -80,7 +82,28 @@ public class SaferRefactoring extends RefaktorRefactoring { System.err.println("\nEDIT:\n=====\n"); TextEdit edit = getChildNumber(1, levelOne).getEdit(); printEdit(edit, 0); -// edit.addChild(new InsertEdit(0, "import findMe;")); + TextEdit firstChild = edit.getChildren()[0]; + System.err.println("\nFIRST CHILD OF FIRST CHILD:\n===========================\n"); + System.err.println(firstChild.getChildren()[1]); + InsertEdit toBeReplaced = (InsertEdit) firstChild.getChildren()[1]; + + String text = toBeReplaced.getText(); + String[] strings = text.split(";"); + + String assert_text = "\n\n\t\tassert that.x == this:\n\t\t\t\"The Move Method refactoring has broken your program due to aliasing issues!\";\n"; + + strings[1] = assert_text + strings[1]; + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < strings.length; i++) { + builder.append(strings[i]); + if (i < strings.length - 1) + builder.append(";"); + } + String new_text = builder.toString(); + InsertEdit replacement = new InsertEdit(toBeReplaced.getOffset(), new_text); + firstChild.removeChild(toBeReplaced); + firstChild.addChild(replacement); + System.err.println("\nAFTER:\n=======\n"); for (TextEdit t: getChildNumber(1, levelOne).getTextEditChangeGroups()[0].getTextEditGroup().getTextEdits()) { System.err.println(t); @@ -130,13 +153,17 @@ public class SaferRefactoring extends RefaktorRefactoring { getMoveInstanceMethodProcessor(refactoring).setUseGetters(true); getMoveInstanceMethodProcessor(refactoring).setInlineDelegator(true); getMoveInstanceMethodProcessor(refactoring).setRemoveDelegator(true); - getMoveInstanceMethodProcessor(refactoring).setTargetName("utils"); + getMoveInstanceMethodProcessor(refactoring).setTargetName("that"); getMoveInstanceMethodProcessor(refactoring).setTarget(getTarget()); this.refactoring = refactoring; } - private IVariableBinding getTarget() throws JavaModelException { - return ((VariableDeclaration) getMethodDeclaration().parameters().get(0)).resolveBinding(); + private IVariableBinding getTarget() throws CoreException { + return getFieldDeclarationFragment().resolveBinding(); + } + + private VariableDeclarationFragment getFieldDeclarationFragment() throws JavaModelException, CoreException { + return ASTNodeSearchUtil.getFieldDeclarationFragmentNode(getIType("aSimpleExample.C").getField("x"), parse(getMethod().getCompilationUnit())); } private MethodDeclaration getMethodDeclaration() throws JavaModelException { -- 2.43.5