From f82892ff9992c74d54b5e1d20339ee85c6cbeb55 Mon Sep 17 00:00:00 2001 From: Erlend Kristiansen Date: Thu, 27 Feb 2014 13:43:21 +0100 Subject: [PATCH] CompilationUnitTextSelection: can now find anonymous declaring types --- .../examples/src/selection/Main.java | 30 +++++++++++++--- .../examples/src/selection/TakingB.java | 5 +++ .../examples/manager/ExampleCodeManager.java | 21 ++++++++++- .../CompilationUnitTextSelectionTest.java | 10 ++++++ .../utils/CompilationUnitTextSelection.java | 35 ++++++++++++++----- 5 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 software/no.uio.ifi.refaktor.examples/examples/src/selection/TakingB.java diff --git a/software/no.uio.ifi.refaktor.examples/examples/src/selection/Main.java b/software/no.uio.ifi.refaktor.examples/examples/src/selection/Main.java index a605f5ba..b1915858 100644 --- a/software/no.uio.ifi.refaktor.examples/examples/src/selection/Main.java +++ b/software/no.uio.ifi.refaktor.examples/examples/src/selection/Main.java @@ -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 index 00000000..3f3774d3 --- /dev/null +++ b/software/no.uio.ifi.refaktor.examples/examples/src/selection/TakingB.java @@ -0,0 +1,5 @@ +package selection; + +interface TakingB { + void foo(B b); +} \ No newline at end of file diff --git a/software/no.uio.ifi.refaktor.examples/src/no/uio/ifi/refaktor/examples/manager/ExampleCodeManager.java b/software/no.uio.ifi.refaktor.examples/src/no/uio/ifi/refaktor/examples/manager/ExampleCodeManager.java index 98db625b..b5145c53 100644 --- a/software/no.uio.ifi.refaktor.examples/src/no/uio/ifi/refaktor/examples/manager/ExampleCodeManager.java +++ b/software/no.uio.ifi.refaktor.examples/src/no/uio/ifi/refaktor/examples/manager/ExampleCodeManager.java @@ -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 diff --git a/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java b/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java index 7e72ac3e..ca7dca8a 100644 --- a/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java +++ b/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java @@ -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[] {}); diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/CompilationUnitTextSelection.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/CompilationUnitTextSelection.java index 6e51f216..51184080 100644 --- a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/CompilationUnitTextSelection.java +++ b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/CompilationUnitTextSelection.java @@ -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(); -- 2.43.5