Skip to content

Commit

Permalink
Cannot refactor in static block
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored and rgrunber committed Apr 13, 2022
1 parent 67063e4 commit 7034451
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws Core
if (fCompilationUnitNode == null) {
fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3));
}
pm.worked(1);

if (fCURewrite == null) {
fCURewrite = new CompilationUnitRewrite(fCu, fCompilationUnitNode);
Expand All @@ -272,6 +271,11 @@ public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws Core
}
pm.worked(1);

if (getMethodDeclaration() == null) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractFieldRefactoring_cannot_extract);
}
pm.worked(1);

if (isUsedInExplicitConstructorCall()) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

package org.eclipse.jdt.ls.core.internal.refactoring;

import static org.junit.Assert.assertEquals;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
Expand Down Expand Up @@ -414,6 +417,41 @@ public void testExtractLambdaBodyToMethod() throws Exception {
assertCodeActions(codeActions, e1);
}

// https:/redhat-developer/vscode-java/issues/2370
@Test
public void testExtractMethodInStaticBlock() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
setOnly(CodeActionKind.Refactor);
//@formatter:off
String contents = "package test1;\r\n"
+ "public class E {\r\n"
+ " public static String STR;\r\n"
+ " static {\r\n"
+ " STR = new String(\"test\").strip();\r\n"
+ " }\r\n"
+ "}";
//@formatter:on
ICompilationUnit cu = pack1.createCompilationUnit("E.java", contents, false, null);
//@formatter:off
String expected = "package test1;\r\n"
+ "public class E {\r\n"
+ " public static String STR;\r\n"
+ " static {\r\n"
+ " STR = extracted().strip();\r\n"
+ " }\r\n"
+ " private static String extracted() {\r\n"
+ " return new String(\"test\");\r\n"
+ " }\r\n"
+ "}";
//@formatter:on
Range range = new Range(new Position(4, 14), new Position(4, 32));
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(4, codeActions.size());
List<Either<Command, CodeAction>> extractMethod = codeActions.stream().filter((c) -> c.getRight().getTitle().equals("Extract to method")).collect(Collectors.toList());
Expected e1 = new Expected("Extract to method", expected, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD);
assertCodeActions(extractMethod, e1);
}

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

0 comments on commit 7034451

Please sign in to comment.