1 /*
2  * Copyright (c) 2003, 2007, 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 4530962
27  * @summary Tests method search using parameter classes in Statement
28  * @author Mark Davidson
29  */
30 
31 import java.beans.Statement;
32 
33 /**
34  * Ambiguous method signature should throw an exception.
35  * Statement should execute the most specific method.
36  */
37 public class Test4530962 {
main(String[] args)38     public static void main(String[] args) throws Exception {
39         try {
40             test(new A(), new Y(), new Y());
41             throw new Error("exception expected");
42         }
43         catch (NoSuchMethodException exception) {
44             // should throw an exception
45         }
46         catch (Exception exception) {
47             throw new Error("unexpected exception", exception);
48         }
49         test(new B(), new Y(), new Y());
50         test(new C(), new Z(), new Z());
51         test(new D(), new Z(), new Z());
52         test(new E(), new Z(), new Z());
53     }
54 
test(Object target, Object... params)55     private static void test(Object target, Object... params) throws Exception {
56         new Statement(target, "m", params).execute();
57     }
58 
59     /**
60      * All ambiguous method declarations should fail.
61      */
62     public static class A {
m(X x1, X x2)63         public void m(X x1, X x2) {
64             throw new Error("A.m(X,X) should not be called");
65         }
66 
m(X x1, Y y2)67         public void m(X x1, Y y2) {
68             throw new Error("A.m(X,Y) should not be called");
69         }
70 
m(Y y1, X x2)71         public void m(Y y1, X x2) {
72             throw new Error("A.m(Y,X) should not be called");
73         }
74     }
75 
76     /**
77      * The most specific method in this case would be the second declaration.
78      */
79     public static class B {
m(X x1, X x2)80         public void m(X x1, X x2) {
81             throw new Error("B.m(X,X) should not be called");
82         }
83 
m(X x1, Y y2)84         public void m(X x1, Y y2) {
85             // expected: B.m(X,Y) should be called
86         }
87     }
88 
89     /**
90      * The most specific method in this case would be the first declaration.
91      */
92     public static class C {
m(Y y1, Y y2)93         public void m(Y y1, Y y2) {
94             // expected: C.m(Y,Y) should be called
95         }
96 
m(X x1, X x2)97         public void m(X x1, X x2) {
98             throw new Error("C.m(X,X) should not be called");
99         }
100     }
101 
102     /**
103      * Same as the previous case but flip methods.
104      */
105     public static class D {
m(X x1, X x2)106         public void m(X x1, X x2) {
107             throw new Error("D.m(X,X) should not be called");
108         }
109 
m(Y y1, Y y2)110         public void m(Y y1, Y y2) {
111             // expected: D.m(Y,Y) should be called
112         }
113     }
114 
115     /**
116      * The method should be called with (Z,Z).
117      */
118     public static class E {
m(X x1, X x2)119         public void m(X x1, X x2) {
120             // expected: E.m(X,X) should be called
121         }
122     }
123 
124     public static class X {
125     }
126 
127     public static class Y extends X {
128     }
129 
130     public static class Z extends Y {
131     }
132 }
133