Skip to content

Commit 3e93eb1

Browse files
hopehadfieldrgrunber
authored andcommitted
Add 'final' to 'Extract to local variable' result if requested
- 'Extract to local variable' / 'Extract to local variable (replace all occurrences)' will respect 'java.codeGeneration.addFinalForNewDeclaration' Signed-off-by: Hope Hadfield <[email protected]>
1 parent 5c5f2e3 commit 3e93eb1

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/RefactorProposalUtility.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ private static ProposalKindWrapper getExtractVariableAllOccurrenceProposal(CodeA
367367
} else {
368368
relevance = IProposalRelevance.EXTRACT_LOCAL_ALL;
369369
}
370+
String declsToFinal = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getCodeGenerationAddFinalForNewDeclaration();
371+
boolean setFinal = "all".equals(declsToFinal) || "variables".equals(declsToFinal);
370372
if (inferSelectionSupport && context.getSelectionLength() == 0) {
371373
ASTNode parent = context.getCoveringNode();
372374
while (parent != null && parent instanceof Expression) {
@@ -375,6 +377,7 @@ private static ProposalKindWrapper getExtractVariableAllOccurrenceProposal(CodeA
375377
continue;
376378
}
377379
ExtractTempRefactoring refactoring = new ExtractTempRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
380+
refactoring.setDeclareFinal(setFinal);
378381
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
379382
CUCorrectionCommandProposal proposal = new CUCorrectionCommandProposal(label, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params));
380383
return CodeActionHandler.wrap(proposal, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE);
@@ -384,6 +387,7 @@ private static ProposalKindWrapper getExtractVariableAllOccurrenceProposal(CodeA
384387
return null;
385388
}
386389
ExtractTempRefactoring extractTempRefactoring = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formatterOptions);
390+
extractTempRefactoring.setDeclareFinal(setFinal);
387391
if (extractTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
388392
if (returnAsCommand) {
389393
CUCorrectionCommandProposal proposal = new CUCorrectionCommandProposal(label, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params));
@@ -424,6 +428,8 @@ private static ProposalKindWrapper getExtractVariableProposal(CodeActionParams p
424428
} else {
425429
relevance = IProposalRelevance.EXTRACT_LOCAL;
426430
}
431+
String declsToFinal = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getCodeGenerationAddFinalForNewDeclaration();
432+
boolean setFinal = "all".equals(declsToFinal) || "variables".equals(declsToFinal);
427433
if (inferSelectionSupport && context.getSelectionLength() == 0) {
428434
ASTNode parent = context.getCoveringNode();
429435
while (parent != null && parent instanceof Expression) {
@@ -432,6 +438,7 @@ private static ProposalKindWrapper getExtractVariableProposal(CodeActionParams p
432438
continue;
433439
}
434440
ExtractTempRefactoring refactoring = new ExtractTempRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
441+
refactoring.setDeclareFinal(setFinal);
435442
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
436443
CUCorrectionCommandProposal proposal = new CUCorrectionCommandProposal(label, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_COMMAND, params));
437444
return CodeActionHandler.wrap(proposal, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE);
@@ -442,6 +449,7 @@ private static ProposalKindWrapper getExtractVariableProposal(CodeActionParams p
442449
}
443450
ExtractTempRefactoring extractTempRefactoringSelectedOnly = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formatterOptions);
444451
extractTempRefactoringSelectedOnly.setReplaceAllOccurrences(false);
452+
extractTempRefactoringSelectedOnly.setDeclareFinal(setFinal);
445453
if (extractTempRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
446454
if (returnAsCommand) {
447455
CUCorrectionCommandProposal proposal = new CUCorrectionCommandProposal(label, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_COMMAND, params));

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/refactoring/GetRefactorEditHandlerTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,45 @@ public void testExtractVariable() throws Exception {
8888
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
8989
}
9090

91+
@Test
92+
public void testExtractVariableFinal() throws Exception {
93+
preferences.setCodeGenerationAddFinalForNewDeclaration("all");
94+
95+
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
96+
97+
StringBuilder buf = new StringBuilder();
98+
buf.append("package test1;\n");
99+
buf.append("class A{\n");
100+
buf.append(" void m(int i){\n");
101+
buf.append(" int x= /*]*/0/*[*/;\n");
102+
buf.append(" }\n");
103+
buf.append("}\n");
104+
105+
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
106+
Range selection = getRange(cu, null);
107+
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
108+
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND, params);
109+
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
110+
Assert.assertNotNull(refactorEdit);
111+
Assert.assertNotNull(refactorEdit.edit);
112+
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
113+
114+
buf = new StringBuilder();
115+
buf.append("package test1;\n");
116+
buf.append("class A{\n");
117+
buf.append(" void m(int i){\n");
118+
buf.append(" final int j = 0;\n");
119+
buf.append(" int x= /*]*/j/*[*/;\n");
120+
buf.append(" }\n");
121+
buf.append("}\n");
122+
AbstractSourceTestCase.compareSource(buf.toString(), actual);
123+
124+
Assert.assertNotNull(refactorEdit.command);
125+
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
126+
Assert.assertNotNull(refactorEdit.command.getArguments());
127+
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
128+
}
129+
91130
@Test
92131
public void testExtractVariableAllOccurrence() throws Exception {
93132
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
@@ -125,6 +164,45 @@ public void testExtractVariableAllOccurrence() throws Exception {
125164
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
126165
}
127166

167+
@Test
168+
public void testExtractVariableAllOccurrenceFinal() throws Exception {
169+
preferences.setCodeGenerationAddFinalForNewDeclaration("all");
170+
171+
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
172+
173+
StringBuilder buf = new StringBuilder();
174+
buf.append("package test1;\n");
175+
buf.append("class A{\n");
176+
buf.append(" void m(int i){\n");
177+
buf.append(" int x= /*]*/0/*[*/;\n");
178+
buf.append(" }\n");
179+
buf.append("}\n");
180+
181+
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
182+
Range selection = getRange(cu, null);
183+
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
184+
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params);
185+
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
186+
Assert.assertNotNull(refactorEdit);
187+
Assert.assertNotNull(refactorEdit.edit);
188+
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
189+
190+
buf = new StringBuilder();
191+
buf.append("package test1;\n");
192+
buf.append("class A{\n");
193+
buf.append(" void m(int i){\n");
194+
buf.append(" final int j = 0;\n");
195+
buf.append(" int x= /*]*/j/*[*/;\n");
196+
buf.append(" }\n");
197+
buf.append("}\n");
198+
AbstractSourceTestCase.compareSource(buf.toString(), actual);
199+
200+
Assert.assertNotNull(refactorEdit.command);
201+
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
202+
Assert.assertNotNull(refactorEdit.command.getArguments());
203+
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
204+
}
205+
128206
@Test
129207
public void testExtractField() throws Exception {
130208
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

0 commit comments

Comments
 (0)