Skip to content

Commit ab93ac1

Browse files
Problem with calling super in overridden default method
Improve the fix from #4727: - treat default methods like abstract (2 locations) Fixes #1600
1 parent f1e122d commit ab93ac1

File tree

2 files changed

+39
-4
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup
  • org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression

2 files changed

+39
-4
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4681,10 +4681,10 @@ protected final MethodBinding mostSpecificMethodBinding(MethodBinding[] visible,
46814681
receiverType = receiverType instanceof CaptureBinding ? receiverType : (ReferenceBinding) receiverType.erasure();
46824682

46834683
// within the boundaries of "chosen arbitrarily among the subset of the maximally specific methods that are preferred"
4684-
// put concrete methods first, default methods second:
4684+
// put concrete methods first:
46854685
Arrays.sort(moreSpecific, (m1, m2) -> {
4686-
int rank1 = m1 == null ? 3 : m1.isAbstract() ? 2 : m1.isDefaultMethod() ? 1 : 0;
4687-
int rank2 = m2 == null ? 3 : m2.isAbstract() ? 2 : m2.isDefaultMethod() ? 1 : 0;
4686+
int rank1 = m1 == null ? 3 : (m1.isAbstract() && !m1.isDefaultMethod()) ? 2 : 0;
4687+
int rank2 = m2 == null ? 3 : (m2.isAbstract() && !m2.isDefaultMethod()) ? 2 : 0;
46884688
return rank1 - rank2;
46894689
});
46904690
boolean hasConsideredNullContract = false;
@@ -4703,7 +4703,7 @@ protected final MethodBinding mostSpecificMethodBinding(MethodBinding[] visible,
47034703
if (TypeBinding.equalsEquals(original.declaringClass, original2.declaringClass))
47044704
break nextSpecific; // duplicates thru substitution
47054705

4706-
if (!original.isAbstract()) {
4706+
if (!original.isAbstract() && !original.isDefaultMethod()) {
47074707
if (original2.isAbstract() || original2.isDefaultMethod())
47084708
continue; // only compare current against other concrete methods
47094709

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/InterfaceMethodsTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,4 +3312,39 @@ default void setReadOnly(boolean readOnly) {
33123312
}
33133313
"""});
33143314
}
3315+
public void testGH1600_2() {
3316+
runConformTest(new String[] {"Test.java",
3317+
"""
3318+
interface I0 {
3319+
default void m() throws Exception {}
3320+
default Number n() throws Exception { return 0l; }
3321+
}
3322+
interface I2 extends I0 {
3323+
@Override abstract void m() throws Exception;
3324+
@Override abstract Integer n() throws Exception;
3325+
}
3326+
class C2 implements I2 {
3327+
@Override public Integer n() throws Exception { return 13; }
3328+
@Override public void m() {}
3329+
}
3330+
public class Test {
3331+
void test1(I0 i) throws Exception {
3332+
if (i instanceof I2)
3333+
((I2) i).m();
3334+
else
3335+
System.out.print("no");
3336+
}
3337+
void test2(I0 i0) throws Exception {
3338+
Integer i = ((I2) i0).n();
3339+
System.out.print(i);
3340+
}
3341+
public static void main(String... args) throws Exception {
3342+
Test t = new Test();
3343+
t.test1(new I0() {});
3344+
t.test2(new C2());
3345+
}
3346+
}
3347+
"""},
3348+
"no13");
3349+
}
33153350
}

0 commit comments

Comments
 (0)