mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
Push Down: super should not access over hierarchy (IDEA-20816)
This commit is contained in:
@@ -75,6 +75,26 @@ public class PushDownConflicts {
|
||||
if (targetClass != null) {
|
||||
for (final PsiMember movedMember : myMovedMembers) {
|
||||
checkMemberPlacementInTargetClassConflict(targetClass, movedMember);
|
||||
movedMember.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
|
||||
super.visitMethodCallExpression(expression);
|
||||
if (expression.getMethodExpression().getQualifierExpression() instanceof PsiSuperExpression) {
|
||||
final PsiMethod resolvedMethod = expression.resolveMethod();
|
||||
if (resolvedMethod != null) {
|
||||
final PsiClass resolvedClass = resolvedMethod.getContainingClass();
|
||||
if (resolvedClass != null) {
|
||||
if (myClass.isInheritor(resolvedClass, true)) {
|
||||
final PsiMethod methodBySignature = myClass.findMethodBySignature(resolvedMethod, false);
|
||||
if (methodBySignature != null && !myMovedMembers.contains(methodBySignature)) {
|
||||
myConflicts.putValue(expression, "Super method call will resolve to another method");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Members:
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
class A {
|
||||
void k() {
|
||||
System.out.println(23);
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
void k() {
|
||||
System.out.println(42);
|
||||
}
|
||||
|
||||
void <caret>m() {
|
||||
new C() {
|
||||
void foo() {
|
||||
super.k();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class C extends B {
|
||||
public static void main(String[] args) {
|
||||
new C().m();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
class A {
|
||||
void k() {
|
||||
System.out.println(23);
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
void k() {
|
||||
System.out.println(42);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class C extends B {
|
||||
public static void main(String[] args) {
|
||||
new C().m();
|
||||
}
|
||||
|
||||
void m() {
|
||||
new C() {
|
||||
void foo() {
|
||||
super.k();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
class A {
|
||||
void k() {
|
||||
System.out.println(23);
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
void k() {
|
||||
System.out.println(42);
|
||||
}
|
||||
|
||||
void <caret>m() {
|
||||
super.k();
|
||||
}
|
||||
}
|
||||
|
||||
public class C extends B {
|
||||
public static void main(String[] args) {
|
||||
new C().m();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
class A {
|
||||
void k() {
|
||||
System.out.println(23);
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
void k() {
|
||||
System.out.println(42);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class C extends B {
|
||||
public static void main(String[] args) {
|
||||
new C().m();
|
||||
}
|
||||
|
||||
void m() {
|
||||
super.k();
|
||||
}
|
||||
}
|
||||
@@ -35,13 +35,13 @@ public class PushDownTest extends LightCodeInsightTestCase {
|
||||
|
||||
final PsiMember psiMember = (PsiMember)targetElement;
|
||||
|
||||
final PsiClass[] classes = ((PsiJavaFile)psiMember.getContainingFile()).getClasses();
|
||||
final PsiClass currentClass = psiMember.getContainingClass();
|
||||
|
||||
assert classes.length > 0;
|
||||
assert currentClass != null;
|
||||
|
||||
final List<MemberInfo> membersToMove = new ArrayList<MemberInfo>();
|
||||
|
||||
final PsiField fieldByName = classes[0].findFieldByName("fieldToMove", false);
|
||||
final PsiField fieldByName = currentClass.findFieldByName("fieldToMove", false);
|
||||
if (fieldByName != null) {
|
||||
final MemberInfo memberInfo = new MemberInfo(fieldByName);
|
||||
memberInfo.setChecked(true);
|
||||
@@ -52,7 +52,7 @@ public class PushDownTest extends LightCodeInsightTestCase {
|
||||
memberInfo.setChecked(true);
|
||||
membersToMove.add(memberInfo);
|
||||
|
||||
new PushDownProcessor(getProject(), membersToMove.toArray(new MemberInfo[membersToMove.size()]), classes[0], new DocCommentPolicy(DocCommentPolicy.ASIS)){
|
||||
new PushDownProcessor(getProject(), membersToMove.toArray(new MemberInfo[membersToMove.size()]), currentClass, new DocCommentPolicy(DocCommentPolicy.ASIS)){
|
||||
@Override
|
||||
protected boolean showConflicts(MultiMap<PsiElement,String> conflicts) {
|
||||
if (failure ? conflicts.isEmpty() : !conflicts.isEmpty()) {
|
||||
@@ -92,4 +92,12 @@ public class PushDownTest extends LightCodeInsightTestCase {
|
||||
public void testThisRefInAnonymous() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSuperOverHierarchyConflict() throws Exception {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
public void testSuperOverHierarchy() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user