TestInterfaceInit.java 3.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

/*
 * @test
 * @bug 8034275
28 29
 * @bug 8163969
 * @summary [JDK 8u40] Test interface init: only for interfaces declaring default methods, when subclass inits
30 31 32 33 34 35 36 37 38 39 40 41 42
 * @run main TestInterfaceInit
 */
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;

public class TestInterfaceInit {

   static List<Class<?>> cInitOrder = new ArrayList<>();

   // Declares a default method and initializes
   interface I {
       boolean v = TestInterfaceInit.out(I.class);
43
        default void ix() {}
44 45 46 47 48
   }

   // Declares a default method and initializes
   interface J extends I {
       boolean v = TestInterfaceInit.out(J.class);
49
       default void jx() {}
50
   }
51
   // No default method, has an abstract method, does not initialize
52 53
   interface JN extends J {
       boolean v = TestInterfaceInit.out(JN.class);
54
       public abstract void jnx();
55 56 57 58 59
   }

   // Declares a default method and initializes
   interface K extends I {
       boolean v = TestInterfaceInit.out(K.class);
60
        default void kx() {}
61 62
   }

63
   // No default method, has a static method, does not initialize
64 65
   interface KN extends K {
       boolean v = TestInterfaceInit.out(KN.class);
66
       static void knx() {}
67 68 69 70
   }

   interface L extends JN, KN {
       boolean v = TestInterfaceInit.out(L.class);
71 72 73 74 75 76
        default void lx() {}
   }

   static class ChildClass implements JN, KN {
       boolean v = TestInterfaceInit.out(ChildClass.class);
       public void jnx() {}
77 78 79 80 81 82
   }

   public static void main(String[] args) {
       // Trigger initialization
       boolean v = L.v;

83 84 85 86 87 88 89 90 91
       List<Class<?>> expectedCInitOrder = Arrays.asList(L.class);
       if (!cInitOrder.equals(expectedCInitOrder)) {
         throw new RuntimeException(String.format("Class initialization array %s not equal to expected array %s", cInitOrder, expectedCInitOrder));
       }

       ChildClass myC = new ChildClass();
       boolean w = myC.v;

       expectedCInitOrder = Arrays.asList(L.class,I.class,J.class,K.class,ChildClass.class);
92 93 94
       if (!cInitOrder.equals(expectedCInitOrder)) {
         throw new RuntimeException(String.format("Class initialization array %s not equal to expected array %s", cInitOrder, expectedCInitOrder));
       }
95

96 97 98 99 100 101 102 103 104
   }

   static boolean out(Class c) {
       System.out.println("#: initializing " + c.getName());
       cInitOrder.add(c);
       return true;
   }

}