]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
CompilationUnitTextSelection: can now find anonymous declaring types
authorErlend Kristiansen <erlenkr@ifi.uio.no>
Thu, 27 Feb 2014 12:43:21 +0000 (13:43 +0100)
committerErlend Kristiansen <erlenkr@ifi.uio.no>
Thu, 27 Feb 2014 12:43:21 +0000 (13:43 +0100)
software/no.uio.ifi.refaktor.examples/examples/src/selection/Main.java
software/no.uio.ifi.refaktor.examples/examples/src/selection/TakingB.java [new file with mode: 0644]
software/no.uio.ifi.refaktor.examples/src/no/uio/ifi/refaktor/examples/manager/ExampleCodeManager.java
software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/CompilationUnitTextSelection.java

index a605f5ba81bc23701507ad4be2763feee66ac126..b191585812b3f2319c100ae83b0871c525ae62e4 100644 (file)
@@ -15,11 +15,11 @@ public class Main {
                a.b.c.b.bar();
                b.c.a.foo();
        }
-       
+
        private void emptyMethod() {
                // Keep empty
        }
-       
+
        private boolean searchableMethod() {
                A a = new A();
                B b = new B();
@@ -30,11 +30,11 @@ public class Main {
                b.c.fred();
                return true;
        }
-       
+
        public static void main(String args[]) {
                new Main().testing();
        }
-       
+
        private boolean anotherSearchableMethod() {
                // Do not touch me
                A a = new A();
@@ -46,9 +46,29 @@ public class Main {
                b.c.fred();
                return true;
        }
-       
+
        class InnerClass {
                private void methodOfInnerClass() {
                }
        }
+
+       private void iHaveAnAnonymousClass() {
+               TakingB anonymous = new TakingB() {
+                       // Must be the first anonymous class in Main for the tests to pass
+                       @Override
+                       public void foo(B b) {
+                               b.foo();
+                               b.bar();
+                       }
+               };
+       }
+
+       private void iHaveALocalClass() {
+               class ALocalClass {
+                       public void foo(B b) {
+                               b.foo();
+                               b.bar();
+                       }
+               }
+       }
 }
\ No newline at end of file
diff --git a/software/no.uio.ifi.refaktor.examples/examples/src/selection/TakingB.java b/software/no.uio.ifi.refaktor.examples/examples/src/selection/TakingB.java
new file mode 100644 (file)
index 0000000..3f3774d
--- /dev/null
@@ -0,0 +1,5 @@
+package selection;
+
+interface TakingB {
+       void foo(B b);
+}
\ No newline at end of file
index 98db625bd8e6eb9ae826cd869a64e2e19296897a..b5145c53a0fca6dde11caa64a5390929fea883a5 100644 (file)
@@ -8,6 +8,8 @@ import no.uio.ifi.refaktor.utils.RefaktorDebug;
 import org.eclipse.core.resources.IMarker;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.IWorkspaceDescription;
 import org.eclipse.core.resources.IncrementalProjectBuilder;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
@@ -36,6 +38,19 @@ public enum ExampleCodeManager {
                        this.project.delete(true, true, null);
                this.project = new LoadExample().loadFilesFromBundleToProject(LoadExample.PLUGIN_ID, LoadExample.PARENT_FOLDER_NAME, projectName, false, monitor);
                testExampleCodeLoaded();
+               turnOffAutobuild();
+               cleanProject();
+       }
+
+       private void turnOffAutobuild() {
+               IWorkspace workspace = ResourcesPlugin.getWorkspace();
+               IWorkspaceDescription description = workspace.getDescription();
+               description.setAutoBuilding(false);
+               try {
+                       workspace.setDescription(description);
+               } catch (CoreException e) {
+                       assert false;
+               }
        }
 
        private void testExampleCodeLoaded() {
@@ -72,10 +87,14 @@ public enum ExampleCodeManager {
 
        public void cleanBuild() {
                try {
-                       project.build(IncrementalProjectBuilder.CLEAN_BUILD, null);
+                       cleanProject();
                        project.build(IncrementalProjectBuilder.FULL_BUILD, null);
                } catch (CoreException e) {
                        fail("Exception occurred when project was clean-built.");
                }
        }
+
+       private void cleanProject() throws CoreException {
+               project.build(IncrementalProjectBuilder.CLEAN_BUILD, null);
+       }
 }
\ No newline at end of file
index 7e72ac3e505fd228f9273d693eefac2eeea06925..ca7dca8aab3b9f692d81b8c7dc8e6ce855588ea3 100644 (file)
@@ -84,6 +84,16 @@ public class CompilationUnitTextSelectionTest {
                assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main.InnerClass"));
        }
        
+       @Test
+       public void testCoveringTypeAnonymous() throws Exception {
+               IProject project = ExampleCodeManager.INSTANCE.getProject();
+               IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$1", "foo", "QB;");
+               assertThat(method.exists());
+               
+               CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
+               assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$1"));
+       }
+       
        @Test
        public void testLastStatement() throws Exception {
                IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", "iHaveAConditionalWithAReturnStatement", new String[] {});
index 6e51f216fac1f9436904c08e551e0794aaea117a..51184080064804b75901471f166e72b7d2ffcf1a 100644 (file)
@@ -9,8 +9,11 @@ import no.uio.ifi.refaktor.analyze.collectors.LastStatementCollector;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.core.dom.ASTNode;
 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
+import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
 import org.eclipse.jdt.core.dom.CompilationUnit;
 import org.eclipse.jdt.core.dom.NodeFinder;
 import org.eclipse.jdt.core.dom.Statement;
@@ -104,19 +107,35 @@ public class CompilationUnitTextSelection extends TextSelection implements IText
        }
 
        public String getFullyQualifiedNameOfSurroundingType() {
-               ASTNode node = getCoveringNode();
-
-               while (!(node instanceof AbstractTypeDeclaration || isRootNode(node)))
-                       node = node.getParent();
+               ASTNode node = findClosestSurroundingClassDeclaration();
 
                if (!isRootNode(node)) {
-                       assert node instanceof AbstractTypeDeclaration;
-                       return ((AbstractTypeDeclaration) node).resolveBinding().getQualifiedName();
+                       assert isClassDeclaration(node);
+
+                       if (node instanceof AbstractTypeDeclaration)
+                               return ((AbstractTypeDeclaration) node).resolveBinding().getQualifiedName();
+                       else if (node instanceof AnonymousClassDeclaration) {
+                               IJavaElement javaElement = ((AnonymousClassDeclaration) node).resolveBinding().getJavaElement();
+                               if (javaElement instanceof IType)
+                                       return ((IType) javaElement).getFullyQualifiedName();
+                       }
                }
 
                return "";
        }
 
+       private ASTNode findClosestSurroundingClassDeclaration() {
+               ASTNode node = getCoveringNode();
+
+               while (!(isClassDeclaration(node) || isRootNode(node)))
+                       node = node.getParent();
+               return node;
+       }
+
+       private boolean isClassDeclaration(ASTNode node) {
+               return node instanceof AbstractTypeDeclaration || node instanceof AnonymousClassDeclaration;
+       }
+
        private boolean isRootNode(ASTNode node) {
                return node.getNodeType() == ASTNode.COMPILATION_UNIT;
        }
@@ -169,7 +188,7 @@ public class CompilationUnitTextSelection extends TextSelection implements IText
 
        public boolean isEquivalentTo(CompilationUnitTextSelection textSelection) {
                return equals(textSelection) || servesTheSamePurposeAs(textSelection);
-                       
+
        }
 
        private boolean servesTheSamePurposeAs(CompilationUnitTextSelection textSelection) {
@@ -184,7 +203,7 @@ public class CompilationUnitTextSelection extends TextSelection implements IText
                        assert coveringNode instanceof Statement;
                        return (Statement) coveringNode;
                }
-                       
+
                LastStatementCollector lastStatementCollector = new LastStatementCollector(this, coveringNode);
                coveringNode.accept(lastStatementCollector);
                return lastStatementCollector.getLastStatement();