< prev index next >

test/java/lang/Class/getMethods/StarInheritance.java

Print this page




  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 
  34 // D.m
  35 interface A1 extends B1, C1 {}
  36 interface B1 extends D1 {}
  37 interface C1 extends D1 {}
  38 interface D1 { void m(); }
  39 
  40 // A.m
  41 interface A2 extends B2, C2 { void m(); }
  42 interface B2 extends D2 {}
  43 interface C2 extends D2 {}
  44 interface D2 { void m(); }
  45 
  46 // B.m, C.m
  47 interface A3 extends B3, C3 {}
  48 interface B3 extends D3 { void m(); }
  49 interface C3 extends D3 { void m(); }
  50 interface D3 { void m() ; }
  51 
  52 // B.m, D.m
  53 interface A4 extends B4, C4 {}
  54 interface B4 extends D4 { void m(); }
  55 interface C4 extends D4 {}
  56 interface D4 { void m(); }
  57 
















  58 public class StarInheritance {
  59     private static int n = 1;
  60 
  61     private static void test(Method [] ma, ArrayList expect) {





  62         System.out.println("Test " + n++);
  63 
  64         if (expect.size() != ma.length) {
  65             System.err.println("  found methods: " + Arrays.asList(ma));
  66             System.err.println("  expected locations: " + expect);
  67             throw new RuntimeException("found = " + ma.length
  68                                        +"; expected = " + expect.size());
  69         }
  70 
  71         for (int i = 0; i < ma.length; i++) {
  72             Method m = ma[i];
  73             System.out.println("  " + m.toString());
  74             int n;
  75             if (m.getName().equals("m")
  76                 && (n = expect.indexOf(m.getDeclaringClass())) != -1) {
  77                 expect.remove(n);
  78             } else {
  79                 throw new RuntimeException("unable to locate method in class: "
  80                                            + m.getDeclaringClass());
  81             }
  82         }
  83     }
  84 
  85     public static void main(String [] args) {
  86         Class [] l1 = {D1.class};
  87         test(A1.class.getMethods(), new ArrayList(Arrays.asList(l1)));












  88 
  89         Class [] l2 = {A2.class};
  90         test(A2.class.getMethods(), new ArrayList(Arrays.asList(l2)));
  91 
  92         Class [] l3 = {B3.class, C3.class};
  93         test(A3.class.getMethods(), new ArrayList(Arrays.asList(l3)));
  94 
  95         Class [] l4 = {B4.class, D4.class};
  96         test(A4.class.getMethods(), new ArrayList(Arrays.asList(l4)));
  97     }
  98 }


  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 }
< prev index next >