test/tools/javac/lambdaShapes/org/openjdk/tests/vm/DefaultMethodsTest.java

Mon, 21 Jan 2013 20:15:16 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:15:16 +0000
changeset 1512
b12ffdfa1341
parent 1448
7d34e91f66bb
child 1675
b54122b9372d
permissions
-rw-r--r--

8005851: Remove support for synchronized interface methods
Summary: Synchronized default methods are no longer supported
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package org.openjdk.tests.vm;
    28 import java.lang.reflect.*;
    29 import java.util.*;
    30 import java.io.File;
    31 import java.io.IOException;
    33 import org.testng.annotations.Test;
    34 import org.openjdk.tests.separate.*;
    35 import org.openjdk.tests.separate.Compiler;
    37 import static org.testng.Assert.*;
    38 import static org.openjdk.tests.separate.SourceModel.*;
    39 import static org.openjdk.tests.separate.SourceModel.Class;
    41 @Test(groups = "vm")
    42 public class DefaultMethodsTest extends TestHarness {
    43     public DefaultMethodsTest() {
    44         super(false, false);
    45     }
    47     /**
    48      * class C { public int m() { return 22; } }
    49      *
    50      * TEST: C c = new C(); c.m() == 22
    51      */
    52     public void testHarnessInvokeVirtual() {
    53         Class C = new Class("C", ConcreteMethod.std("22"));
    54         assertInvokeVirtualEquals(22, C);
    55     }
    57     /**
    58      * interface I { int m(); }
    59      * class C implements I { public int m() { return 33; } }
    60      *
    61      * TEST: I i = new C(); i.m() == 33;
    62      */
    63     public void testHarnessInvokeInterface() {
    64         Interface I = new Interface("I", AbstractMethod.std());
    65         Class C = new Class("C", I, ConcreteMethod.std("33"));
    66         assertInvokeInterfaceEquals(33, C, I);
    67     }
    69     /**
    70      * class C {}
    71      *
    72      * TEST: C c = new C(); c.m() throws NoSuchMethod
    73      */
    74     public void testHarnessThrows() {
    75         Class C = new Class("C");
    76         assertThrows(NoSuchMethodError.class, C);
    77     }
    79     /**
    80      * interface I { int m() default { return 44; } }
    81      * class C implements I {}
    82      *
    83      * TEST: C c = new C(); c.m() == 44;
    84      * TEST: I i = new C(); i.m() == 44;
    85      */
    86     public void testBasicDefault() {
    87         Interface I = new Interface("I", DefaultMethod.std("44"));
    88         Class C = new Class("C", I);
    90         assertInvokeVirtualEquals(44, C);
    91         assertInvokeInterfaceEquals(44, C, I);
    92     }
    94     /**
    95      * interface I { default int m() { return 44; } }
    96      * interface J extends I {}
    97      * interface K extends J {}
    98      * class C implements K {}
    99      *
   100      * TEST: C c = new C(); c.m() == 44;
   101      * TEST: I i = new C(); i.m() == 44;
   102      */
   103     public void testFarDefault() {
   104         Interface I = new Interface("I", DefaultMethod.std("44"));
   105         Interface J = new Interface("J", I);
   106         Interface K = new Interface("K", J);
   107         Class C = new Class("C", K);
   109         assertInvokeVirtualEquals(44, C);
   110         assertInvokeInterfaceEquals(44, C, K);
   111     }
   113     /**
   114      * interface I { int m(); }
   115      * interface J extends I { default int m() { return 44; } }
   116      * interface K extends J {}
   117      * class C implements K {}
   118      *
   119      * TEST: C c = new C(); c.m() == 44;
   120      * TEST: K k = new C(); k.m() == 44;
   121      */
   122     public void testOverrideAbstract() {
   123         Interface I = new Interface("I", AbstractMethod.std());
   124         Interface J = new Interface("J", I, DefaultMethod.std("44"));
   125         Interface K = new Interface("K", J);
   126         Class C = new Class("C", K);
   128         assertInvokeVirtualEquals(44, C);
   129         assertInvokeInterfaceEquals(44, C, K);
   130     }
   132     /**
   133      * interface I { int m() default { return 44; } }
   134      * class C implements I { public int m() { return 55; } }
   135      *
   136      * TEST: C c = new C(); c.m() == 55;
   137      * TEST: I i = new C(); i.m() == 55;
   138      */
   139     public void testExisting() {
   140         Interface I = new Interface("I", DefaultMethod.std("44"));
   141         Class C = new Class("C", I, ConcreteMethod.std("55"));
   143         assertInvokeVirtualEquals(55, C);
   144         assertInvokeInterfaceEquals(55, C, I);
   145     }
   147     /**
   148      * interface I { default int m() { return 99; } }
   149      * class B implements I {}
   150      * class C extends B {}
   151      *
   152      * TEST: C c = new C(); c.m() == 99;
   153      * TEST: I i = new C(); i.m() == 99;
   154      */
   155     public void testInherited() {
   156         Interface I = new Interface("I", DefaultMethod.std("99"));
   157         Class B = new Class("B", I);
   158         Class C = new Class("C", B);
   160         assertInvokeVirtualEquals(99, C);
   161         assertInvokeInterfaceEquals(99, C, I);
   162     }
   164     /**
   165      * interface I { default int m() { return 99; } }
   166      * class C { public int m() { return 11; } }
   167      * class D extends C implements I {}
   168      *
   169      * TEST: D d = new D(); d.m() == 11;
   170      * TEST: I i = new D(); i.m() == 11;
   171      */
   172     public void testExistingInherited() {
   173         Interface I = new Interface("I", DefaultMethod.std("99"));
   174         Class C = new Class("C", ConcreteMethod.std("11"));
   175         Class D = new Class("D", C, I);
   177         assertInvokeVirtualEquals(11, D);
   178         assertInvokeInterfaceEquals(11, D, I);
   179     }
   181     /**
   182      * interface I { default int m() { return 44; } }
   183      * class C implements I { public int m() { return 11; } }
   184      * class D extends C { public int m() { return 22; } }
   185      *
   186      * TEST: D d = new D(); d.m() == 22;
   187      * TEST: I i = new D(); i.m() == 22;
   188      */
   189     void testExistingInheritedOverride() {
   190         Interface I = new Interface("I", DefaultMethod.std("99"));
   191         Class C = new Class("C", I, ConcreteMethod.std("11"));
   192         Class D = new Class("D", C, ConcreteMethod.std("22"));
   194         assertInvokeVirtualEquals(22, D);
   195         assertInvokeInterfaceEquals(22, D, I);
   196     }
   198     /**
   199      * interface I { default int m() { return 99; } }
   200      * interface J { defaultint m() { return 88; } }
   201      * class C implements I { public int m() { return 11; } }
   202      * class D extends C { public int m() { return 22; } }
   203      * class E extends D implements J {}
   204      *
   205      * TEST: E e = new E(); e.m() == 22;
   206      * TEST: J j = new E(); j.m() == 22;
   207      */
   208     public void testExistingInheritedPlusDefault() {
   209         Interface I = new Interface("I", DefaultMethod.std("99"));
   210         Interface J = new Interface("J", DefaultMethod.std("88"));
   211         Class C = new Class("C", I, ConcreteMethod.std("11"));
   212         Class D = new Class("D", C, ConcreteMethod.std("22"));
   213         Class E = new Class("E", D, J);
   215         assertInvokeVirtualEquals(22, E);
   216         assertInvokeInterfaceEquals(22, E, J);
   217     }
   219     /**
   220      * interface I { default int m() { return 99; } }
   221      * class B implements I {}
   222      * class C extends B { public int m() { return 77; } }
   223      *
   224      * TEST: C c = new C(); c.m() == 77;
   225      * TEST: I i = new C(); i.m() == 77;
   226      */
   227     public void testInheritedWithConcrete() {
   228         Interface I = new Interface("I", DefaultMethod.std("99"));
   229         Class B = new Class("B", I);
   230         Class C = new Class("C", B, ConcreteMethod.std("77"));
   232         assertInvokeVirtualEquals(77, C);
   233         assertInvokeInterfaceEquals(77, C, I);
   234     }
   236     /**
   237      * interface I { default int m() { return 99; } }
   238      * class B implements I {}
   239      * class C extends B implements I { public int m() { return 66; } }
   240      *
   241      * TEST: C c = new C(); c.m() == 66;
   242      * TEST: I i = new C(); i.m() == 66;
   243      */
   244     public void testInheritedWithConcreteAndImpl() {
   245         Interface I = new Interface("I", DefaultMethod.std("99"));
   246         Class B = new Class("B", I);
   247         Class C = new Class("C", B, I, ConcreteMethod.std("66"));
   249         assertInvokeVirtualEquals(66, C);
   250         assertInvokeInterfaceEquals(66, C, I);
   251     }
   253     /**
   254      * interface I { default int m() { return 99; } }
   255      * interface J { default int m() { return 88; } }
   256      * class C implements I, J {}
   257      *
   258      * TEST: C c = new C(); c.m() throws AME
   259      */
   260     public void testConflict() {
   261         // debugTest();
   262         Interface I = new Interface("I", DefaultMethod.std("99"));
   263         Interface J = new Interface("J", DefaultMethod.std("88"));
   264         Class C = new Class("C", I, J);
   266         assertThrows(AbstractMethodError.class, C);
   267     }
   269     /**
   270      * interface I { int m(); }
   271      * interface J { default int m() { return 88; } }
   272      * class C implements I, J {}
   273      *
   274      * TEST: C c = new C(); c.m() throws AME
   275      */
   276     public void testAmbiguousReabstract() {
   277         Interface I = new Interface("I", AbstractMethod.std());
   278         Interface J = new Interface("J", DefaultMethod.std("88"));
   279         Class C = new Class("C", I, J);
   281         assertThrows(AbstractMethodError.class, C);
   282     }
   284     /**
   285      * interface I { default int m() { return 99; } }
   286      * interface J extends I { }
   287      * interface K extends I { }
   288      * class C implements J, K {}
   289      *
   290      * TEST: C c = new C(); c.m() == 99
   291      * TEST: J j = new C(); j.m() == 99
   292      * TEST: K k = new C(); k.m() == 99
   293      * TEST: I i = new C(); i.m() == 99
   294      */
   295     public void testDiamond() {
   296         Interface I = new Interface("I", DefaultMethod.std("99"));
   297         Interface J = new Interface("J", I);
   298         Interface K = new Interface("K", I);
   299         Class C = new Class("C", J, K);
   301         assertInvokeVirtualEquals(99, C);
   302         assertInvokeInterfaceEquals(99, C, J);
   303         assertInvokeInterfaceEquals(99, C, K);
   304         assertInvokeInterfaceEquals(99, C, I);
   305     }
   307     /**
   308      * interface I { default int m() { return 99; } }
   309      * interface J extends I { }
   310      * interface K extends I { }
   311      * interface L extends I { }
   312      * interface M extends I { }
   313      * class C implements I, J, K, L, M {}
   314      *
   315      * TEST: C c = new C(); c.m() == 99
   316      * TEST: J j = new C(); j.m() == 99
   317      * TEST: K k = new C(); k.m() == 99
   318      * TEST: I i = new C(); i.m() == 99
   319      * TEST: L l = new C(); l.m() == 99
   320      * TEST: M m = new C(); m.m() == 99
   321      */
   322     public void testExpandedDiamond() {
   323         Interface I = new Interface("I", DefaultMethod.std("99"));
   324         Interface J = new Interface("J", I);
   325         Interface K = new Interface("K", I);
   326         Interface L = new Interface("L", I);
   327         Interface M = new Interface("M", L);
   328         Class C = new Class("C", I, J, K, L, M);
   330         assertInvokeVirtualEquals(99, C);
   331         assertInvokeInterfaceEquals(99, C, J);
   332         assertInvokeInterfaceEquals(99, C, K);
   333         assertInvokeInterfaceEquals(99, C, I);
   334         assertInvokeInterfaceEquals(99, C, L);
   335         assertInvokeInterfaceEquals(99, C, M);
   336     }
   338     /**
   339      * interface I { int m() default { return 99; } }
   340      * interface J extends I { int m(); }
   341      * class C implements J {}
   342      *
   343      * TEST: C c = new C(); c.m() throws AME
   344      */
   345     public void testReabstract() {
   346         Interface I = new Interface("I", DefaultMethod.std("99"));
   347         Interface J = new Interface("J", I, AbstractMethod.std());
   348         Class C = new Class("C", J);
   350         assertThrows(AbstractMethodError.class, C);
   351     }
   353     /**
   354      * interface I { default int m() { return 88; } }
   355      * interface J extends I { default int m() { return 99; } }
   356      * class C implements J {}
   357      *
   358      * TEST: C c = new C(); c.m() == 99;
   359      * TEST: J j = new C(); j.m() == 99;
   360      * TEST: I i = new C(); i.m() == 99;
   361      */
   362     public void testShadow() {
   363         Interface I = new Interface("I", DefaultMethod.std("88"));
   364         Interface J = new Interface("J", I, DefaultMethod.std("99"));
   365         Class C = new Class("C", J);
   367         assertInvokeVirtualEquals(99, C);
   368         assertInvokeInterfaceEquals(99, C, J);
   369         assertInvokeInterfaceEquals(99, C, I);
   370     }
   372     /**
   373      * interface I { default int m() { return 88; } }
   374      * interface J extends I { default int m() { return 99; } }
   375      * class C implements I, J {}
   376      *
   377      * TEST: C c = new C(); c.m() == 99;
   378      * TEST: J j = new C(); j.m() == 99;
   379      * TEST: I i = new C(); i.m() == 99;
   380      */
   381     public void testDisqualified() {
   382         Interface I = new Interface("I", DefaultMethod.std("88"));
   383         Interface J = new Interface("J", I, DefaultMethod.std("99"));
   384         Class C = new Class("C", I, J);
   386         assertInvokeVirtualEquals(99, C);
   387         assertInvokeInterfaceEquals(99, C, J);
   388         assertInvokeInterfaceEquals(99, C, I);
   389     }
   391     /**
   392      * interface I<T> { default int m(T t) { return 99; } }
   393      * Class C implements I<String> { public int m() { return 88; } }
   394      *
   395      * TEST: C c = new C(); c.m() == 88;
   396      * TEST: I i = new C(); i.m() == 88;
   397      */
   398     public void testSelfFill() {
   399         // This test ensures that a concrete method overrides a default method
   400         // that matches at the language-level, but has a different method
   401         // signature due to erasure.
   403         // debugTest();
   405         DefaultMethod dm = new DefaultMethod(
   406             "int", "m", "return 99;", new MethodParameter("T", "t"));
   407         ConcreteMethod cm = new ConcreteMethod(
   408             "int", "m", "return 88;", AccessFlag.PUBLIC,
   409             new MethodParameter("String", "s"));
   411         Interface I = new Interface("I", new TypeParameter("T"), dm);
   412         Class C = new Class("C", I.with("String"), cm);
   414         AbstractMethod pm = new AbstractMethod(
   415             "int", "m", new MethodParameter("T", "t"));
   417         assertInvokeVirtualEquals(new Integer(88), C, cm, "-1", "\"string\"");
   418         assertInvokeInterfaceEquals(
   419             new Integer(88), C, I.with("String"), pm, "\"string\"");
   420     }
   422     /**
   423      * interface I { default int m() { return 99; } }
   424      * class C implements I {}
   425      *
   426      * TEST: C.class.getMethod("m").invoke(new C()) == 99
   427      */
   428     public void testReflectCall() {
   429         Interface I = new Interface("I", DefaultMethod.std("99"));
   430         Class C = new Class("C", I);
   432         Compiler.Flags[] flags = this.verbose ?
   433             new Compiler.Flags[] { Compiler.Flags.VERBOSE } :
   434             new Compiler.Flags[] {};
   435         Compiler compiler = new Compiler(flags);
   436         java.lang.Class<?> cls = null;
   437         try {
   438             cls = compiler.compileAndLoad(C);
   439         } catch (ClassNotFoundException e) {
   440             fail("Could not load class");
   441         }
   443         java.lang.reflect.Method method = null;
   444         try {
   445             method = cls.getMethod(stdMethodName);
   446         } catch (NoSuchMethodException e) {
   447             fail("Could not find method in class");
   448         }
   449         assertNotNull(method);
   451         Object c = null;
   452         try {
   453             c = cls.newInstance();
   454         } catch (InstantiationException | IllegalAccessException e) {
   455             fail("Could not create instance of class");
   456         }
   457         assertNotNull(c);
   459         Integer res = null;
   460         try {
   461             res = (Integer)method.invoke(c);
   462         } catch (IllegalAccessException |
   463                  java.lang.reflect.InvocationTargetException e) {
   464             fail("Could not invoke default instance method");
   465         }
   466         assertNotNull(res);
   468         assertEquals(res.intValue(), 99);
   470         compiler.cleanup();
   471     }
   473     /**
   474      * interface I<T,V,W> { default int m(T t, V v, W w) { return 99; } }
   475      * interface J<T,V> extends I<String,T,V> { int m(T t, V v, String w); } }
   476      * interface K<T> extends J<String,T> { int m(T t, String v, String w); } }
   477      * class C implements K<String> {
   478      *     public int m(String t, String v, String w) { return 88; }
   479      * }
   480      *
   481      * TEST: I<String,String,String> i = new C(); i.m("A","B","C") == 88;
   482      * TEST: J<String,String> j = new C(); j.m("A","B","C") == 88;
   483      * TEST: K<String> k = new C(); k.m("A","B","C") == 88;
   484      */
   485     public void testBridges() {
   486         DefaultMethod dm = new DefaultMethod("int", stdMethodName, "return 99;",
   487             new MethodParameter("T", "t"), new MethodParameter("V", "v"),
   488             new MethodParameter("W", "w"));
   490         AbstractMethod pm0 = new AbstractMethod("int", stdMethodName,
   491             new MethodParameter("T", "t"), new MethodParameter("V", "v"),
   492             new MethodParameter("W", "w"));
   494         AbstractMethod pm1 = new AbstractMethod("int", stdMethodName,
   495             new MethodParameter("T", "t"), new MethodParameter("V", "v"),
   496             new MethodParameter("String", "w"));
   498         AbstractMethod pm2 = new AbstractMethod("int", stdMethodName,
   499             new MethodParameter("T", "t"), new MethodParameter("String", "v"),
   500             new MethodParameter("String", "w"));
   502         ConcreteMethod cm = new ConcreteMethod("int",stdMethodName,"return 88;",
   503             AccessFlag.PUBLIC,
   504             new MethodParameter("String", "t"),
   505             new MethodParameter("String", "v"),
   506             new MethodParameter("String", "w"));
   508         Interface I = new Interface("I", new TypeParameter("T"),
   509             new TypeParameter("V"), new TypeParameter("W"), dm);
   510         Interface J = new Interface("J",
   511             new TypeParameter("T"), new TypeParameter("V"),
   512             I.with("String", "T", "V"), pm1);
   513         Interface K = new Interface("K", new TypeParameter("T"),
   514             J.with("String", "T"), pm2);
   515         Class C = new Class("C", K.with("String"), cm);
   517         String[] args = new String[] { "\"A\"", "\"B\"", "\"C\"" };
   518         assertInvokeInterfaceEquals(new Integer(88), C,
   519             I.with("String", "String", "String"), pm0, args);
   520         assertInvokeInterfaceEquals(new Integer(88), C,
   521             J.with("String", "String"), pm1, args);
   522         assertInvokeInterfaceEquals(new Integer(88), C,
   523             K.with("String"), pm2, args);
   524     }
   526     /**
   527      * interface J { default int m() { return 88; } }
   528      * interface I extends J { default int m() { return J.super.m(); } }
   529      * class C implements I {}
   530      *
   531      * TEST: C c = new C(); c.m() == 88;
   532      * TEST: I i = new C(); i.m() == 88;
   533      */
   534     public void testSuperBasic() {
   535         // debugTest();
   537         Interface J = new Interface("J", DefaultMethod.std("88"));
   538         Interface I = new Interface("I", J, new DefaultMethod(
   539             "int", stdMethodName, "return J.super.m();"));
   540         I.addCompilationDependency(J.findMethod(stdMethodName));
   541         Class C = new Class("C", I);
   543         assertInvokeVirtualEquals(88, C);
   544         assertInvokeInterfaceEquals(88, C, I);
   545     }
   547     /**
   548      * interface K { int m() default { return 99; } }
   549      * interface L { int m() default { return 101; } }
   550      * interface J extends K, L {}
   551      * interface I extends J, K { int m() default { J.super.m(); } }
   552      * class C implements I {}
   553      *
   554      * TEST: C c = new C(); c.m() throws AME
   555      * TODO: add case for K k = new C(); k.m() throws AME
   556      */
   557     public void testSuperConflict() {
   558         // debugTest();
   560         Interface K = new Interface("K", DefaultMethod.std("99"));
   561         Interface L = new Interface("L", DefaultMethod.std("101"));
   562         Interface J = new Interface("J", K, L);
   563         Interface I = new Interface("I", J, K, new DefaultMethod(
   564             "int", stdMethodName, "return J.super.m();"));
   565         Interface Jstub = new Interface("J", DefaultMethod.std("-1"));
   566         I.addCompilationDependency(Jstub);
   567         I.addCompilationDependency(Jstub.findMethod(stdMethodName));
   568         Class C = new Class("C", I);
   570         assertThrows(AbstractMethodError.class, C);
   571     }
   573     /**
   574      * interface I { default int m() { return 99; } }
   575      * interface J extends I { default int m() { return 55; } }
   576      * class C implements I, J { public int m() { return I.super.m(); } }
   577      *
   578      * TEST: C c = new C(); c.m() throws AME
   579      * TODO: add case for J j = new C(); j.m() throws AME
   580      */
   581     public void testSuperDisqual() {
   582         Interface I = new Interface("I", DefaultMethod.std("99"));
   583         Interface J = new Interface("J", I, DefaultMethod.std("55"));
   584         Class C = new Class("C", I, J,
   585             new ConcreteMethod("int", stdMethodName, "return I.super.m();",
   586                 AccessFlag.PUBLIC));
   587         C.addCompilationDependency(I.findMethod(stdMethodName));
   589         assertThrows(AbstractMethodError.class, C);
   590     }
   592     /**
   593      * interface J { int m(); }
   594      * interface I extends J { default int m() { return J.super.m(); } }
   595      * class C implements I {}
   596      *
   597      * TEST: C c = new C(); c.m() throws AME
   598      * TODO: add case for I i = new C(); i.m() throws AME
   599      */
   600     public void testSuperNull() {
   601         Interface J = new Interface("J", AbstractMethod.std());
   602         Interface I = new Interface("I", J, new DefaultMethod(
   603             "int", stdMethodName, "return J.super.m();"));
   604         Interface Jstub = new Interface("J", DefaultMethod.std("99"));
   605         I.addCompilationDependency(Jstub);
   606         I.addCompilationDependency(Jstub.findMethod(stdMethodName));
   607         Class C = new Class("C", I);
   609         assertThrows(AbstractMethodError.class, C);
   610     }
   612     /**
   613      * interface J<T> { default int m(T t) { return 88; } }
   614      * interface I extends J<String> {
   615      *     int m(String s) default { return J.super.m(); }
   616      * }
   617      * class C implements I {}
   618      *
   619      * TEST: I i = new C(); i.m("") == 88;
   620      */
   621     public void testSuperGeneric() {
   622         Interface J = new Interface("J", new TypeParameter("T"),
   623             new DefaultMethod("int", stdMethodName, "return 88;",
   624                 new MethodParameter("T", "t")));
   625         Interface I = new Interface("I", J.with("String"),
   626             new DefaultMethod("int", stdMethodName, "return J.super.m(s);",
   627                 new MethodParameter("String", "s")));
   628         I.addCompilationDependency(J.findMethod(stdMethodName));
   629         Class C = new Class("C", I);
   631         AbstractMethod pm = new AbstractMethod("int", stdMethodName,
   632             new MethodParameter("String", "s"));
   634         assertInvokeInterfaceEquals(
   635             new Integer(88), C, new Extends(I), pm, "\"\"");
   636     }
   638     /**
   639      * interface I<T> { int m(T t) default { return 44; } }
   640      * interface J extends I<String> { int m(String s) default { return 55; } }
   641      * class C implements I<String>, J {
   642      *     public int m(String s) { return I.super.m(s); }
   643      * }
   644      *
   645      * TEST: C c = new C(); c.m("string") throws AME
   646      */
   647     public void testSuperGenericDisqual() {
   648         MethodParameter t = new MethodParameter("T", "t");
   649         MethodParameter s = new MethodParameter("String", "s");
   651         Interface I = new Interface("I", new TypeParameter("T"),
   652             new DefaultMethod("int", stdMethodName, "return 44;", t));
   653         Interface J = new Interface("J", I.with("String"),
   654             new DefaultMethod("int", stdMethodName, "return 55;", s));
   655         Class C = new Class("C", I.with("String"), J,
   656             new ConcreteMethod("int", stdMethodName,
   657                 "return I.super.m(s);", AccessFlag.PUBLIC, s));
   658         C.addCompilationDependency(I.findMethod(stdMethodName));
   660         assertThrows(AbstractMethodError.class, C,
   661             new ConcreteMethod(
   662                 "int", stdMethodName, "return -1;", AccessFlag.PUBLIC, s),
   663             "-1", "\"string\"");
   664     }
   666     /**
   667      * interface I { default Integer m() { return new Integer(88); } }
   668      * class C { Number m() { return new Integer(99); } }
   669      * class D extends C implements I {}
   670      * class S { Object foo() { return (new D()).m(); } // link sig: ()LInteger;
   671      * TEST: S s = new S(); s.foo() == new Integer(99)
   672      */
   673     public void testCovarBridge() {
   674         Interface I = new Interface("I", new DefaultMethod(
   675             "Integer", "m", "return new Integer(88);"));
   676         Class C = new Class("C", new ConcreteMethod(
   677             "Number", "m", "return new Integer(99);", AccessFlag.PUBLIC));
   678         Class D = new Class("D", I, C);
   680         ConcreteMethod DstubMethod = new ConcreteMethod(
   681             "Integer", "m", "return null;", AccessFlag.PUBLIC);
   682         Class Dstub = new Class("D", DstubMethod);
   684         ConcreteMethod toCall = new ConcreteMethod(
   685             "Object", "foo", "return (new D()).m();", AccessFlag.PUBLIC);
   686         Class S = new Class("S", D, toCall);
   687         S.addCompilationDependency(Dstub);
   688         S.addCompilationDependency(DstubMethod);
   690         assertInvokeVirtualEquals(new Integer(99), S, toCall, "null");
   691     }
   693     /**
   694      * interface I { default Integer m() { return new Integer(88); } }
   695      * class C { int m() { return 99; } }
   696      * class D extends C implements I {}
   697      * class S { Object foo() { return (new D()).m(); } // link sig: ()LInteger;
   698      * TEST: S s = new S(); s.foo() == new Integer(88)
   699      */
   700     public void testNoCovarNoBridge() {
   701         Interface I = new Interface("I", new DefaultMethod(
   702             "Integer", "m", "return new Integer(88);"));
   703         Class C = new Class("C", new ConcreteMethod(
   704             "int", "m", "return 99;", AccessFlag.PUBLIC));
   705         Class D = new Class("D", I, C);
   707         ConcreteMethod DstubMethod = new ConcreteMethod(
   708             "Integer", "m", "return null;", AccessFlag.PUBLIC);
   709         Class Dstub = new Class("D", DstubMethod);
   711         ConcreteMethod toCall = new ConcreteMethod(
   712             "Object", "foo", "return (new D()).m();", AccessFlag.PUBLIC);
   713         Class S = new Class("S", D, toCall);
   714         S.addCompilationDependency(Dstub);
   715         S.addCompilationDependency(DstubMethod);
   717         assertInvokeVirtualEquals(new Integer(88), S, toCall, "null");
   718     }
   720     /**
   721      * interface J { int m(); }
   722      * interface I extends J { default int m() { return 99; } }
   723      * class B implements J {}
   724      * class C extends B implements I {}
   725      * TEST: C c = new C(); c.m() == 99
   726      *
   727      * The point of this test is that B does not get default method analysis,
   728      * and C does not generate any new miranda methods in the vtable.
   729      * It verifies that default method analysis occurs when mirandas have been
   730      * inherited and the supertypes don't have any overpass methods.
   731      */
   732     public void testNoNewMiranda() {
   733         Interface J = new Interface("J", AbstractMethod.std());
   734         Interface I = new Interface("I", J, DefaultMethod.std("99"));
   735         Class B = new Class("B", J);
   736         Class C = new Class("C", B, I);
   737         assertInvokeVirtualEquals(99, C);
   738     }
   740     /**
   741      * interface I<T,V,W> { int m(T t, V v, W w); }
   742      * interface J<T,V> implements I<T,V,String> { int m(T t, V v, String w); }
   743      * interface K<T> implements J<T,String> {
   744      *     int m(T t, String v, String w); { return 99; } }
   745      * class C implements K<String> {
   746      *     public int m(Object t, Object v, String w) { return 77; }
   747      * }
   748      * TEST C = new C(); ((I)c).m(Object,Object,Object) == 99
   749      * TEST C = new C(); ((J)c).m(Object,Object,String) == 77
   750      * TEST C = new C(); ((K)c).m(Object,String,String) == 99
   751      *
   752      * Test that a erased-signature-matching method does not implement
   753      * non-language-level matching methods
   754      */
   755     public void testNonConcreteFill() {
   756         AbstractMethod ipm = new AbstractMethod("int", "m",
   757             new MethodParameter("T", "t"),
   758             new MethodParameter("V", "s"),
   759             new MethodParameter("W", "w"));
   760         Interface I = new Interface("I",
   761             new TypeParameter("T"),
   762             new TypeParameter("V"),
   763             new TypeParameter("W"), ipm);
   765         AbstractMethod jpm = new AbstractMethod("int", "m",
   766             new MethodParameter("T", "t"),
   767             new MethodParameter("V", "s"),
   768             new MethodParameter("String", "w"));
   769         Interface J = new Interface("J",
   770             new TypeParameter("T"),
   771             new TypeParameter("V"),
   772             I.with("T", "V", "String"), jpm);
   774         AbstractMethod kpm = new AbstractMethod("int", "m",
   775             new MethodParameter("T", "t"),
   776             new MethodParameter("String", "s"),
   777             new MethodParameter("String", "w"));
   778         Interface K = new Interface("K",
   779             new TypeParameter("T"),
   780             J.with("T", "String"),
   781             new DefaultMethod("int", "m", "return 99;",
   782                 new MethodParameter("T", "t"),
   783                 new MethodParameter("String", "v"),
   784                 new MethodParameter("String", "w")));
   786         Class C = new Class("C",
   787             K.with("String"),
   788             new ConcreteMethod("int", "m", "return 77;",
   789                 AccessFlag.PUBLIC,
   790                 new MethodParameter("Object", "t"),
   791                 new MethodParameter("Object", "v"),
   792                 new MethodParameter("String", "w")));
   794         String a = "\"\"";
   795         assertInvokeInterfaceEquals(99, C,
   796             K.with("String"), kpm, a, a, a);
   797         assertInvokeInterfaceEquals(77, C,
   798             J.with("String", "String"), jpm, a, a, a);
   799         assertInvokeInterfaceEquals(99, C,
   800             I.with("String", "String", "String"), ipm, a, a, a);
   801     }
   803     public void testStrictfpDefault() {
   804         try {
   805             java.lang.Class.forName("org.openjdk.tests.vm.StrictfpDefault");
   806         } catch (Exception e) {
   807             fail("Could not load class", e);
   808         }
   809     }
   810 }
   812 interface StrictfpDefault {
   813     default strictfp void m() {}
   814 }

mercurial