Skip to content

Commit

Permalink
Expose the anonymousToNest refactor more widely
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored and fbricon committed Sep 9, 2020
1 parent 36993ac commit 7d064d7
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 27 deletions.
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);
}
}

0 comments on commit 7d064d7

Please sign in to comment.