mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract method: disable make static when extracting from default method; ensure static extracting from constant in interface (IDEA-134636)
This commit is contained in:
+28
-15
@@ -1339,7 +1339,10 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
}
|
||||
|
||||
if (myTargetClass.isInterface() && PsiUtil.isLanguageLevel8OrHigher(myTargetClass)) {
|
||||
PsiUtil.setModifierProperty(newMethod, PsiModifier.DEFAULT, true);
|
||||
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(myCodeFragmentMember, PsiMethod.class, false);
|
||||
if (containingMethod != null && containingMethod.hasModifierProperty(PsiModifier.DEFAULT)) {
|
||||
PsiUtil.setModifierProperty(newMethod, PsiModifier.DEFAULT, true);
|
||||
}
|
||||
}
|
||||
return (PsiMethod)myStyleManager.reformat(newMethod);
|
||||
}
|
||||
@@ -1579,24 +1582,34 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
myStatic = shouldBeStatic();
|
||||
final Set<PsiField> fields = new LinkedHashSet<PsiField>();
|
||||
if (!PsiUtil.isLocalOrAnonymousClass(myTargetClass) && (myTargetClass.getContainingClass() == null || myTargetClass.hasModifierProperty(PsiModifier.STATIC))) {
|
||||
ElementNeedsThis needsThis = new ElementNeedsThis(myTargetClass) {
|
||||
@Override
|
||||
protected void visitClassMemberReferenceElement(PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
|
||||
if (classMember instanceof PsiField && !classMember.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
final PsiExpression expression = PsiTreeUtil.getParentOfType(classMemberReference, PsiExpression.class, false);
|
||||
if (expression == null || !PsiUtil.isAccessedForWriting(expression)) {
|
||||
fields.add((PsiField)classMember);
|
||||
return;
|
||||
boolean canBeStatic = true;
|
||||
if (myTargetClass.isInterface()) {
|
||||
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(myCodeFragmentMember, PsiMethod.class, false);
|
||||
canBeStatic = containingMethod == null || containingMethod.hasModifierProperty(PsiModifier.STATIC);
|
||||
}
|
||||
if (canBeStatic) {
|
||||
ElementNeedsThis needsThis = new ElementNeedsThis(myTargetClass) {
|
||||
@Override
|
||||
protected void visitClassMemberReferenceElement(PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
|
||||
if (classMember instanceof PsiField && !classMember.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
final PsiExpression expression = PsiTreeUtil.getParentOfType(classMemberReference, PsiExpression.class, false);
|
||||
if (expression == null || !PsiUtil.isAccessedForWriting(expression)) {
|
||||
fields.add((PsiField)classMember);
|
||||
return;
|
||||
}
|
||||
}
|
||||
super.visitClassMemberReferenceElement(classMember, classMemberReference);
|
||||
}
|
||||
super.visitClassMemberReferenceElement(classMember, classMemberReference);
|
||||
};
|
||||
for (int i = 0; i < myElements.length && !needsThis.usesMembers(); i++) {
|
||||
PsiElement element = myElements[i];
|
||||
element.accept(needsThis);
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < myElements.length && !needsThis.usesMembers(); i++) {
|
||||
PsiElement element = myElements[i];
|
||||
element.accept(needsThis);
|
||||
myCanBeStatic = !needsThis.usesMembers();
|
||||
}
|
||||
else {
|
||||
myCanBeStatic = false;
|
||||
}
|
||||
myCanBeStatic = !needsThis.usesMembers();
|
||||
}
|
||||
else {
|
||||
myCanBeStatic = false;
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
interface I {
|
||||
String FOO = <selection>"hello";</selection>
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
interface I {
|
||||
String FOO = newMethod();
|
||||
|
||||
@NotNull
|
||||
private static String newMethod() {
|
||||
return "hello";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
interface I {
|
||||
static void foo () {
|
||||
<selection>System.out.println("hello");</selection>
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
interface I {
|
||||
static void foo () {
|
||||
newMethod();
|
||||
}
|
||||
|
||||
private static void newMethod() {
|
||||
System.out.println("hello");
|
||||
}
|
||||
}
|
||||
@@ -581,6 +581,14 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
public void testMethod2Interface() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testMethod2InterfaceFromStatic() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testMethod2InterfaceFromConstant() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testParamDetection() throws Exception {
|
||||
doTest();
|
||||
|
||||
Reference in New Issue
Block a user