Java: Don't specify visibility for extracted method declared in an interface (IDEA-196426)

This commit is contained in:
Pavel Dolgov
2018-08-02 16:29:56 +03:00
parent 4fdbf3889b
commit 8f53696f2f
7 changed files with 72 additions and 5 deletions
@@ -1634,6 +1634,9 @@ public class ExtractMethodProcessor implements MatchProvider {
if (containingMethod != null && containingMethod.hasModifierProperty(PsiModifier.DEFAULT)) {
PsiUtil.setModifierProperty(newMethod, PsiModifier.DEFAULT, true);
}
PsiUtil.setModifierProperty(newMethod, PsiModifier.PUBLIC, false);
PsiUtil.setModifierProperty(newMethod, PsiModifier.PRIVATE, false);
PsiUtil.setModifierProperty(newMethod, PsiModifier.PROTECTED, false);
}
return (PsiMethod)myStyleManager.reformat(newMethod);
}
@@ -0,0 +1,15 @@
interface TweetParser {
static String getTweetMessageFrom(String fullTweet) {
String fieldName = "\"text\":\"";
<selection>int indexOfField = fullTweet.indexOf(fieldName) + fieldName.length();
int indexOfEndOfField = fullTweet.indexOf("\"", indexOfField);
return fullTweet.substring(indexOfField, indexOfEndOfField);</selection>
}
static String getTwitterHandleFromTweet(String fullTweet) {
String twitterHandleFieldName = "\"screen_name\":\"";
int indexOfTwitterHandleField = fullTweet.indexOf(twitterHandleFieldName) + twitterHandleFieldName.length();
int indexOfEndOfTwitterHandle = fullTweet.indexOf("\"", indexOfTwitterHandleField);
return fullTweet.substring(indexOfTwitterHandleField, indexOfEndOfTwitterHandle);
}
}
@@ -0,0 +1,17 @@
interface TweetParser {
static String getTweetMessageFrom(String fullTweet) {
String fieldName = "\"text\":\"";
return newMethod(fullTweet, fieldName);
}
static String newMethod(String fullTweet, String fieldName) {
int indexOfField = fullTweet.indexOf(fieldName) + fieldName.length();
int indexOfEndOfField = fullTweet.indexOf("\"", indexOfField);
return fullTweet.substring(indexOfField, indexOfEndOfField);
}
static String getTwitterHandleFromTweet(String fullTweet) {
String twitterHandleFieldName = "\"screen_name\":\"";
return newMethod(fullTweet, twitterHandleFieldName);
}
}
@@ -4,7 +4,7 @@ interface I {
String FOO = newMethod();
@NotNull
private static String newMethod() {
static String newMethod() {
return "hello";
}
}
@@ -3,7 +3,7 @@ interface I {
newMethod();
}
private static void newMethod() {
static void newMethod() {
System.out.println("hello");
}
}
@@ -3,7 +3,7 @@ interface I {
newMethod();
}
private default void newMethod() {
default void newMethod() {
System.out.println("hello");
}
}
@@ -17,6 +17,7 @@ package com.intellij.java.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
@@ -1158,7 +1159,8 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
configureByFile(BASE_PATH + getTestName(false) + ".java");
final PsiClass psiClass = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getSelectionModel().getLeadSelectionOffset()), PsiClass.class);
assertNotNull(psiClass);
boolean success = performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, false, null, psiClass.getContainingClass());
boolean success =
performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, false, null, psiClass.getContainingClass(), null);
assertTrue(success);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
@@ -1303,6 +1305,34 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doDuplicatesTest();
}
public void testInterfaceMethodVisibility() throws Exception {
final String doesNotExist = "foo.bar.baz.DoesNotExist";
final NullableNotNullManager nullManager = NullableNotNullManager.getInstance(getProject());
final List<String> nullables = nullManager.getNullables();
final List<String> notNulls = nullManager.getNotNulls();
final String defaultNullable = nullManager.getDefaultNullable();
final String defaultNotNull = nullManager.getDefaultNotNull();
try {
nullManager.setNullables(doesNotExist);
nullManager.setNotNulls(doesNotExist);
nullManager.setDefaultNullable(doesNotExist);
nullManager.setDefaultNotNull(doesNotExist);
configureByFile(BASE_PATH + getTestName(false) + ".java");
boolean success =
performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, false, null, null, PsiModifier.PUBLIC);
assertTrue(success);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
finally {
nullManager.setNullables(ArrayUtil.toStringArray(nullables));
nullManager.setNotNulls(ArrayUtil.toStringArray(notNulls));
nullManager.setDefaultNullable(defaultNullable);
nullManager.setDefaultNotNull(defaultNotNull);
}
}
public void testBeforeCommentAfterSelectedFragment() throws Exception {
doTest();
}
@@ -1428,7 +1458,7 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
int... disabledParams)
throws PrepareFailedException, IncorrectOperationException {
return performExtractMethod(doRefactor, replaceAllDuplicates, editor, file, project, extractChainedConstructor, returnType, makeStatic,
newNameOfFirstParam, null, disabledParams);
newNameOfFirstParam, null, null, disabledParams);
}
public static boolean performExtractMethod(boolean doRefactor,
@@ -1441,6 +1471,7 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
boolean makeStatic,
String newNameOfFirstParam,
PsiClass targetClass,
@Nullable @PsiModifier.ModifierConstant String methodVisibility,
int... disabledParams)
throws PrepareFailedException, IncorrectOperationException {
int startOffset = editor.getSelectionModel().getSelectionStart();
@@ -1474,6 +1505,7 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
if (doRefactor) {
processor.testTargetClass(targetClass);
processor.testPrepare(returnType, makeStatic);
if (methodVisibility != null) processor.setMethodVisibility(methodVisibility);
processor.testNullability();
if (disabledParams != null) {
for (int param : disabledParams) {