mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
move members: show conflict if non-final field is moved to interface
This commit is contained in:
+11
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
package com.intellij.refactoring.move.moveMembers;
|
||||
|
||||
import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector;
|
||||
import com.intellij.lang.LanguageExtension;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -271,6 +272,16 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor {
|
||||
conflicts.put(member, message);
|
||||
}
|
||||
}
|
||||
|
||||
if (member instanceof PsiField && myTargetClass.isInterface()) {
|
||||
final ReadWriteAccessDetector accessDetector = ReadWriteAccessDetector.findDetector(member);
|
||||
if (accessDetector != null) {
|
||||
final ReadWriteAccessDetector.Access access = accessDetector.getExpressionAccess(element);
|
||||
if (access != ReadWriteAccessDetector.Access.Read) {
|
||||
conflicts.put(element, CommonRefactoringUtil.capitalize(RefactoringUIUtil.getDescription(member, true)) + " has write access but is moved to an interface");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
public interface A {
|
||||
|
||||
String ONE = "";
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public class B {
|
||||
|
||||
void foo() {
|
||||
A.ONE = "foo";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public interface A {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public class B {
|
||||
public static String ONE = "";
|
||||
|
||||
void foo() {
|
||||
ONE = "foo";
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import com.intellij.JavaTestUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
|
||||
public class MoveMembersTest extends MultiFileTestCase {
|
||||
@Override
|
||||
@@ -84,6 +85,16 @@ public class MoveMembersTest extends MultiFileTestCase {
|
||||
doTest("B", "A", 0);
|
||||
}
|
||||
|
||||
public void testWritableField() throws Exception {
|
||||
try {
|
||||
doTest("B", "A", 0);
|
||||
fail("conflict expected");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertEquals(e.getMessage(), "Found conflicts: Field <b><code>B.ONE</code></b> has write access but is moved to an interface");
|
||||
}
|
||||
}
|
||||
|
||||
protected String getTestRoot() {
|
||||
return "/refactoring/moveMembers/";
|
||||
}
|
||||
@@ -124,7 +135,15 @@ public class MoveMembersTest extends MultiFileTestCase {
|
||||
|
||||
MockMoveMembersOptions options = new MockMoveMembersOptions(targetClass.getQualifiedName(), memberSet);
|
||||
options.setMemberVisibility(null);
|
||||
new MoveMembersProcessor(myProject, null, options).run();
|
||||
new MoveMembersProcessor(myProject, null, options){
|
||||
@Override
|
||||
protected boolean showConflicts(Map<PsiElement, String> conflicts) {
|
||||
if (!conflicts.isEmpty()) {
|
||||
throw new RuntimeException("Found conflicts: " + conflicts.values().iterator().next());
|
||||
}
|
||||
return super.showConflicts(conflicts);
|
||||
}
|
||||
}.run();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user