blob: 5bfe008fc8d2e13787c2589a10db196cf8d17972 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.lang.reflect.*;
25
26/*
27 * Debuggee which exercises various types of method calls which throw
28 * Exceptions, including some done via Reflection,
29 * This was written while investigating Bug ID 4359606
30 */
31
32class MethodCallsReflection {
33
34 public static void main(String args[]) throws Exception {
35 (new MethodCallsReflection()).go();
36 }
37
38 static void staticCaller1(MethodCallsReflection mc) throws Exception {
39 System.out.println("Called staticCaller1");
40 staticExceptionCallee();
41 }
42 static void staticCaller2(MethodCallsReflection mc) throws Exception {
43 System.out.println("Called staticCaller2");
44 mc.instanceExceptionCallee();
45 }
46 static void staticCaller3(MethodCallsReflection mc) throws Exception {
47 System.out.println("Called staticCaller3");
48 /*
49 * Invocation by reflection. This also exercises native method calls
50 * since Method.invoke is a native method.
51 */
52 Method m = MethodCallsReflection.class.getDeclaredMethod("staticExceptionCallee", new Class[0]);
53 m.invoke(mc, new Object[0]);
54 }
55
56 void instanceCaller1() throws Exception {
57 System.out.println("Called instanceCaller1");
58 staticExceptionCallee();
59 }
60
61 void instanceCaller2() throws Exception {
62 System.out.println("Called instanceCaller2");
63 instanceExceptionCallee();
64 }
65
66 void instanceCaller3() throws Exception {
67 System.out.println("Called instanceCaller3");
68
69 /*
70 * Invocation by reflection. This also exercises native method calls
71 * since Method.invoke is a native method.
72 */
73 Method m = getClass().getDeclaredMethod("instanceExceptionCallee", new Class[0]);
74 m.invoke(this, new Object[0]);
75 }
76
77 static void staticExceptionCallee() throws Exception {
78 System.out.println("Called staticExceptionCallee");
79 throw new IndexOutOfBoundsException ("staticExceptionCallee");
80 }
81
82 void instanceExceptionCallee() throws Exception {
83 System.out.println("Called instanceExceptionCallee");
84 throw new IndexOutOfBoundsException ("instanceExceptionCallee");
85 }
86
87 void go() throws Exception {
88 try {
89 instanceCaller1();
90 } catch (IndexOutOfBoundsException ex) {
91 System.out.println("Caught expected IndexOutOfBoundsException from instanceCaller1()");
92 }
93
94 try {
95 instanceCaller2();
96 } catch (IndexOutOfBoundsException ex) {
97 System.out.println("Caught expected IndexOutOfBoundsException from instanceCaller2()");
98 }
99
100 try {
101 instanceCaller3();
102 } catch (InvocationTargetException ex) {
103 System.out.println("Caught expected InvocationTargetException from instanceCaller3()");
104 }
105
106 try {
107 staticCaller1(this);
108 } catch (IndexOutOfBoundsException ex) {
109 System.out.println("Caught expected IndexOutOfBoundsException from staticCaller1()");
110 }
111
112 try {
113 staticCaller2(this);
114 } catch (IndexOutOfBoundsException ex) {
115 System.out.println("Caught expected IndexOutOfBoundsException from staticCaller2()");
116 }
117 try {
118 staticCaller3(this);
119 } catch (InvocationTargetException ex) {
120 System.out.println("Caught expected InvocationTargetException from staticCaller3()");
121 }
122 }
123}