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();
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();
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
--- /dev/null
+package selection;
+
+interface TakingB {
+ void foo(B b);
+}
\ No newline at end of file
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;
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() {
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
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[] {});
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;
}
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;
}
public boolean isEquivalentTo(CompilationUnitTextSelection textSelection) {
return equals(textSelection) || servesTheSamePurposeAs(textSelection);
-
+
}
private boolean servesTheSamePurposeAs(CompilationUnitTextSelection textSelection) {
assert coveringNode instanceof Statement;
return (Statement) coveringNode;
}
-
+
LastStatementCollector lastStatementCollector = new LastStatementCollector(this, coveringNode);
coveringNode.accept(lastStatementCollector);
return lastStatementCollector.getLastStatement();