Java Code Examples for org.eclipse.lsp4j.CodeActionKind#Refactor

The following examples show how to use org.eclipse.lsp4j.CodeActionKind#Refactor . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnonymousClassToNestedTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testConvertToNestedWithExtends() 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() {};\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, "Foo() {", 0);
	assertCodeActions(cu, range, expected);
}
 
Example 2
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertAndOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (true & true)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (false | false)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "true & true", "true & true".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "true & true", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 3
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertBooleanLiteralWithParentheses() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        while (!(!true))\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        while (!true)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "!(!true)", "!(!true)".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "!(!true)", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 4
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertOrOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (true | true)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (false & false)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "true | true", "true | true".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "true | true", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 5
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertNotEqualsOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 != 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 == 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "3 != 5", "3 != 5".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "3 != 5", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 6
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertEqualsOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 == 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 != 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "3 == 5", "3 == 5".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "3 == 5", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 7
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCombinedConditionWithPartialSelection2() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (isValid() || 3 < 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (isValid() || 3 >= 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "3 < 5", "3 < 5".length());
	assertCodeActions(cu, replacedRange, expected);
}
 
Example 8
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertLessEqualsOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 <= 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 > 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "3 <= 5", "3 <= 5".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "3 <= 5", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 9
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertGreaterOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 > 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 <= 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "3 > 5", "3 > 5".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "3 > 5", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 10
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertLessOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 < 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (3 >= 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "3 < 5", "3 < 5".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "3 < 5", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 11
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertBooleanLiteral() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        while (true)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        while (false)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "true", "true".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "true", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 12
Source File: AnonymousClassToNestedTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testConvertToNestedWithStaticModifier() throws Exception {
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E {\n");
	buf.append("    public static void test() {\n");
	buf.append("        Runnable run = new Runnable() {\n");
	buf.append("            @Override\n");
	buf.append("            public void run() {}\n");
	buf.append("        };\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("public class E {\n");
	buf.append("    private static final class RunnableImplementation implements Runnable {\n");
	buf.append("        @Override\n");
	buf.append("        public void run() {}\n");
	buf.append("    }\n");
	buf.append("\n");
	buf.append("    public static void test() {\n");
	buf.append("        Runnable run = new RunnableImplementation();\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected expected = new Expected(CONVERT_ANONYMOUS_TO_NESTED, buf.toString(), CodeActionKind.Refactor);

	Range range = CodeActionUtil.getRange(cu, "Runnable() {", 0);
	assertCodeActions(cu, range, expected);
}
 
Example 13
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCombinedConditionWithPartialSelection() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (isValid() || 3 < 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (!isValid() || 3 < 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "isValid()", "isValid()".length());
	assertCodeActions(cu, replacedRange, expected);
}
 
Example 14
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testConditionalOperator() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public int foo() {\n");
	buf.append("        return 3 > 5 ? 0 : -1;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public int foo() {\n");
	buf.append("        return 3 <= 5 ? 0 : -1;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "3 > 5", "3 > 5".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "3 > 5", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 15
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertMethodCalling() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (isValid())\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (!isValid())\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "isValid()", "isValid()".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "isValid()", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 16
Source File: JDTLanguageServer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private CodeActionOptions getCodeActionOptions() {
	String[] kinds = { CodeActionKind.QuickFix, CodeActionKind.Refactor, CodeActionKind.RefactorExtract, CodeActionKind.RefactorInline, CodeActionKind.RefactorRewrite, CodeActionKind.Source, CodeActionKind.SourceOrganizeImports };
	List<String> codeActionKinds = new ArrayList<>();
	for (String kind : kinds) {
		if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) {
			codeActionKinds.add(kind);
		}
	}
	CodeActionOptions options = new CodeActionOptions(codeActionKinds);
	return options;
}
 
Example 17
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvertComplexCondition() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        for (int i = 4; i > 3 && i < 10; i++)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo() {\n");
	buf.append("        for (int i = 4; i <= 3 || i >= 10; i++)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "i > 3 && i < 10", "i > 3 && i < 10".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "i > 3 && i < 10", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}
 
Example 18
Source File: RefactorProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean getMakeVariableDeclarationFinalProposals(IInvocationContext context, Collection<ChangeCorrectionProposal> resultingCollections) {
	IProposableFix fix = (IProposableFix) VariableDeclarationFixCore.createCleanUp(context.getASTRoot(), true, true, true);

	if (fix == null) {
		return false;
	}

	FixCorrectionProposal proposal = new FixCorrectionProposal(fix, null, IProposalRelevance.MAKE_VARIABLE_DECLARATION_FINAL, context, CodeActionKind.Refactor);
	proposal.setDisplayName("Change modifiers to final where possible");
	resultingCollections.add(proposal);
	return true;
}
 
Example 19
Source File: RefactorProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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) {
		return null;
	}

	final AnonymousClassDeclaration anonymTypeDecl = ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
	if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
		return null;
	}

	final ConvertAnonymousToNestedRefactoring refactoring = new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
	if (!refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
		return null;
	}

	if (returnAsCommand) {
		return new RefactoringCorrectionCommandProposal(label, CodeActionKind.Refactor, context.getCompilationUnit(), IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED, RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID,
				Arrays.asList(CONVERT_ANONYMOUS_CLASS_TO_NESTED_COMMAND, params));
	}

	String extTypeName = ASTNodes.getSimpleNameIdentifier((Name) node);
	ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
	String className;
	if (anonymTypeBinding.getInterfaces().length == 0) {
		className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
	} else {
		className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
	}
	String[][] existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
	int i = 1;
	while (existingTypes != null) {
		i++;
		existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
	}
	refactoring.setClassName(i == 1 ? className : className + i);

	LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
	refactoring.setLinkedProposalModel(linkedProposalModel);

	final ICompilationUnit cu = context.getCompilationUnit();
	RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.Refactor, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED);
	proposal.setLinkedProposalModel(linkedProposalModel);
	return proposal;

}
 
Example 20
Source File: InvertConditionTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testCombinedCondition() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (isValid() || 3 < 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public boolean isValid() {\n");
	buf.append("        return true;\n");
	buf.append("    }\n");
	buf.append("    public void foo() {\n");
	buf.append("        if (!isValid() && 3 >= 5)\n");
	buf.append("            return;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected("Invert conditions", buf.toString(), CodeActionKind.Refactor);
	Range replacedRange = CodeActionUtil.getRange(cu, "isValid() || 3 < 5", "isValid() || 3 < 5".length());
	assertCodeActions(cu, replacedRange, expected);

	Range nonSelectionRange = CodeActionUtil.getRange(cu, "isValid()", 0);
	assertCodeActions(cu, nonSelectionRange, expected);

	nonSelectionRange = CodeActionUtil.getRange(cu, "3 < 5", 0);
	assertCodeActions(cu, nonSelectionRange, expected);

	nonSelectionRange = CodeActionUtil.getRange(cu, "||", 0);
	assertCodeActions(cu, nonSelectionRange, expected);
}