Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the anonymousToNest refactor more widely #1541

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ private boolean getInlineProposal(IInvocationContext context, ASTNode node, Coll
}


private boolean getConvertAnonymousToNestedProposals(CodeActionParams params, IInvocationContext context, final ASTNode node, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
if (!(node instanceof Name)) {
return false;
}

private boolean getConvertAnonymousToNestedProposals(CodeActionParams params, IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
if (proposals == null) {
return false;
}
Expand All @@ -405,12 +401,11 @@ private boolean getConvertAnonymousToNestedProposals(CodeActionParams params, II

public static RefactoringCorrectionProposal getConvertAnonymousToNestedProposal(CodeActionParams params, IInvocationContext context, final ASTNode node, boolean returnAsCommand) throws CoreException {
String label = CorrectionMessages.QuickAssistProcessor_convert_anonym_to_nested;
ASTNode normalized = ASTNodes.getNormalizedNode(node);
if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY) {
ClassInstanceCreation cic = getClassInstanceCreation(node);
if (cic == null) {
return null;
}

final AnonymousClassDeclaration anonymTypeDecl = ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
final AnonymousClassDeclaration anonymTypeDecl = cic.getAnonymousClassDeclaration();
if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
return null;
}
Expand All @@ -425,7 +420,7 @@ public static RefactoringCorrectionProposal getConvertAnonymousToNestedProposal(
Arrays.asList(CONVERT_ANONYMOUS_CLASS_TO_NESTED_COMMAND, params));
}

String extTypeName = ASTNodes.getSimpleNameIdentifier((Name) node);
String extTypeName = ASTNodes.getTypeName(cic.getType());
ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
String className;
if (anonymTypeBinding.getInterfaces().length == 0) {
Expand All @@ -448,27 +443,26 @@ public static RefactoringCorrectionProposal getConvertAnonymousToNestedProposal(
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.Refactor, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED);
proposal.setLinkedProposalModel(linkedProposalModel);
return proposal;
}

private static ClassInstanceCreation getClassInstanceCreation(ASTNode node) {
while (node instanceof Name || node instanceof Type || node instanceof Dimension || node.getParent() instanceof MethodDeclaration
|| node.getLocationInParent() == AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY) {
node = node.getParent();
}

if (node instanceof ClassInstanceCreation) {
return (ClassInstanceCreation) node;
} else if (node.getLocationInParent() == ClassInstanceCreation.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
return (ClassInstanceCreation) node.getParent();
} else {
return null;
}
}

private static boolean getConvertAnonymousClassCreationsToLambdaProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
while (covering instanceof Name || covering instanceof Type || covering instanceof Dimension || covering.getParent() instanceof MethodDeclaration
|| covering.getLocationInParent() == AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY) {
covering = covering.getParent();
}

ClassInstanceCreation cic;
if (covering instanceof ClassInstanceCreation) {
cic = (ClassInstanceCreation) covering;
} else if (covering.getLocationInParent() == ClassInstanceCreation.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
cic = (ClassInstanceCreation) covering.getParent();
} else if (covering instanceof Name) {
ASTNode normalized = ASTNodes.getNormalizedNode(covering);
if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY) {
return false;
}
cic = (ClassInstanceCreation) normalized.getParent();
} else {
ClassInstanceCreation cic = getClassInstanceCreation(covering);
if (cic == null) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,97 @@ public void testConvertToNestedWithExtends() throws Exception {
Range range = CodeActionUtil.getRange(cu, "Foo() {", 0);
assertCodeActions(cu, range, expected);
}

@Test
public void testConvertToNestedCursor1() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class Foo {}\n");
buf.append("public class E {\n");
buf.append(" public void test() {\n");
buf.append(" Foo foo = new Foo(/*cursor*/) {};\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class Foo {}\n");
buf.append("public class E {\n");
buf.append(" private final class FooExtension extends Foo {\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" public void test() {\n");
buf.append(" Foo foo = new FooExtension();\n");
buf.append(" }\n");
buf.append("}\n");
Expected expected = new Expected(CONVERT_ANONYMOUS_TO_NESTED, buf.toString(), CodeActionKind.Refactor);

Range range = CodeActionUtil.getRange(cu, "/*cursor*/", 0);
assertCodeActions(cu, range, expected);
}

@Test
public void testConvertToNestedCursor2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class Foo {}\n");
buf.append("public class E {\n");
buf.append(" public void test() {\n");
buf.append(" Foo foo = new Foo() /*cursor*/{};\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class Foo {}\n");
buf.append("public class E {\n");
buf.append(" private final class FooExtension extends Foo {\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" public void test() {\n");
buf.append(" Foo foo = new FooExtension();\n");
buf.append(" }\n");
buf.append("}\n");
Expected expected = new Expected(CONVERT_ANONYMOUS_TO_NESTED, buf.toString(), CodeActionKind.Refactor);

Range range = CodeActionUtil.getRange(cu, "/*cursor*/", 0);
assertCodeActions(cu, range, expected);
}

@Test
public void testConvertToNestedCursor3() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class Foo {}\n");
buf.append("public class E {\n");
buf.append(" public void test() {\n");
buf.append(" Foo foo = new Foo() {/*cursor*/};\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class Foo {}\n");
buf.append("public class E {\n");
buf.append(" private final class FooExtension extends Foo {\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" public void test() {\n");
buf.append(" Foo foo = new FooExtension();\n");
buf.append(" }\n");
buf.append("}\n");
Expected expected = new Expected(CONVERT_ANONYMOUS_TO_NESTED, buf.toString(), CodeActionKind.Refactor);

Range range = CodeActionUtil.getRange(cu, "/*cursor*/", 0);
assertCodeActions(cu, range, expected);
}
}