1 /* 2 * Copyright (c) 2002, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4654698 27 * @summary Verify that expected methods are returned for star inheritance. 28 */ 29 30 import java.lang.reflect.Method; 31 import java.util.Arrays; 32 import java.util.ArrayList; 33 import java.util.List; 34 import java.util.stream.Collectors; 35 import java.util.stream.Stream; 36 37 // D.m 38 interface A1 extends B1, C1 {} 39 interface B1 extends D1 {} 40 interface C1 extends D1 {} 41 interface D1 { void m(); } 42 43 // A.m 44 interface A2 extends B2, C2 { void m(); } 45 interface B2 extends D2 {} 46 interface C2 extends D2 {} 47 interface D2 { void m(); } 48 49 // B.m, C.m 50 interface A3 extends B3, C3 {} 51 interface B3 extends D3 { void m(); } 52 interface C3 extends D3 { void m(); } 53 interface D3 { void m() ; } 54 55 // B.m, D.m 56 interface A4 extends B4, C4 {} 57 interface B4 extends D4 { void m(); } 58 interface C4 extends D4 {} 59 interface D4 { void m(); } 60 61 // D.m 62 abstract class A_1 extends B_1 implements C1 {} 63 abstract class B_1 implements D1 {} 64 65 // A_.m 66 abstract class A_2 extends B_2 implements C2 { public abstract void m(); } 67 abstract class B_2 implements D2 {} 68 69 // B_.m, C.m 70 abstract class A_3 extends B_3 implements C3 {} 71 abstract class B_3 implements D3 { public abstract void m(); } 72 73 // B_.m, D.m 74 abstract class A_4 extends B_4 implements C4 {} 75 abstract class B_4 implements D4 { public abstract void m(); } 76 77 public class StarInheritance { 78 private static int n = 1; 79 80 private static void test(Method [] ma, ArrayList<Class<?>> expect) { 81 // filter out Object methods 82 List<Method> ml = Stream.of(ma) 83 .filter(m -> m.getDeclaringClass() != Object.class) 84 .collect(Collectors.toList()); 85 86 System.out.println("Test " + n++); 87 88 if (expect.size() != ml.size()) { 89 System.err.println(" found methods: " + ml); 90 System.err.println(" expected locations: " + expect); 91 throw new RuntimeException("found = " + ml.size() 92 +"; expected = " + expect.size()); 93 } 94 95 for (Method m : ml) { 96 System.out.println(" " + m.toString()); 97 int n; 98 if (m.getName().equals("m") 99 && (n = expect.indexOf(m.getDeclaringClass())) != -1) { 100 expect.remove(n); 101 } else { 102 throw new RuntimeException("unable to locate method in class: " 103 + m.getDeclaringClass()); 104 } 105 } 106 } 107 108 public static void main(String [] args) { 109 Class<?> [] l1 = {D1.class}; 110 test(A1.class.getMethods(), new ArrayList<>(Arrays.asList(l1))); 111 112 Class<?> [] l2 = {A2.class}; 113 test(A2.class.getMethods(), new ArrayList<>(Arrays.asList(l2))); 114 115 Class<?> [] l3 = {B3.class, C3.class}; 116 test(A3.class.getMethods(), new ArrayList<>(Arrays.asList(l3))); 117 118 Class<?> [] l4 = {B4.class, D4.class}; 119 test(A4.class.getMethods(), new ArrayList<>(Arrays.asList(l4))); 120 121 Class<?> [] l_1 = {D1.class}; 122 test(A_1.class.getMethods(), new ArrayList<>(Arrays.asList(l_1))); 123 124 Class<?> [] l_2 = {A_2.class}; 125 test(A_2.class.getMethods(), new ArrayList<>(Arrays.asList(l_2))); 126 127 Class<?> [] l_3 = {B_3.class, C3.class}; 128 test(A_3.class.getMethods(), new ArrayList<>(Arrays.asList(l_3))); 129 130 Class<?> [] l_4 = {B_4.class, D4.class}; 131 test(A_4.class.getMethods(), new ArrayList<>(Arrays.asList(l_4))); 132 } 133 }