summaryrefslogtreecommitdiff
path: root/tests/044-proxy/src/WrappedThrow.java
blob: 27ae84e24c998549d3c5179bfe0f9c208cbd1c53 (plain)
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

/*
 * Create a Proxy class that blah.
 */
public class WrappedThrow {
    public static void main(String[] args) {
        WTMix mix = new WTMix();
        InvocationHandler handler = new WTInvocationHandler(mix);
        Object proxy;

        try {
            proxy = Proxy.newProxyInstance(WrappedThrow.class.getClassLoader(),
                new Class[] { InterfaceW1.class, InterfaceW2.class },
                handler);
        } catch (IllegalArgumentException iae) {
            System.out.println("WT init failed");
            return;
        }

        InterfaceW1 if1 = (InterfaceW1) proxy;
        InterfaceW2 if2 = (InterfaceW2) proxy;
        try {
            if1.throwFunky();
            System.err.println("No exception thrown");
        } catch (UndeclaredThrowableException ute) {
            System.out.println("Got expected UTE");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }

        try {
            if1.throwFunky2();
            System.err.println("No exception thrown");
        } catch (IOException ioe) {
            System.out.println("Got expected IOE");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }

        try {
            if2.throwFunky2();
            System.err.println("No exception thrown");
        } catch (IOException ioe) {
            System.out.println("Got expected IOE");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }

        /*
         * Throw exceptions, walking down the hierarchy.
         */
        try {
            if1.throwException();
            System.err.println("No exception thrown");
        } catch (UndeclaredThrowableException ute) {
            System.out.println("Got expected UTE");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }

        try {
            if1.throwBase();
            System.err.println("No exception thrown");
        } catch (UndeclaredThrowableException ute) {
            System.out.println("Got expected UTE");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }

        try {
            if2.throwSub();
            System.err.println("No exception thrown");
        } catch (SubException se) {
            System.out.println("Got expected exception");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }

        try {
            if2.throwSubSub();
            System.err.println("No exception thrown");
        } catch (SubException se) {
            System.out.println("Got expected exception");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }

        /*
         * Make sure that, if the class explicitly allows the base
         * class of an exception, that we still allow it.
         */
        try {
            if1.bothThrowBase();
            System.err.println("No exception thrown");
        } catch (BaseException se) {
            System.out.println("Got expected exception");
        } catch (Throwable t) {
            System.err.println("Got unexpected exception: " + t);
        }
    }
}

class BaseException extends Exception {}
class SubException extends BaseException {}
class SubSubException extends SubException {}

interface InterfaceW1 {
    public void throwFunky();

    public void throwFunky2() throws BaseException,
           NoSuchMethodException, IOException;

    public void throwException() throws BaseException;
    public void throwBase() throws BaseException;
    public void throwSub() throws BaseException;
    public void throwSubSub() throws BaseException;

    public void bothThrowBase() throws BaseException, SubException, SubSubException;
}

interface InterfaceW2 {
    public void throwFunky2() throws InterruptedException,
           NoSuchMethodException, IOException;

    public void throwException() throws SubException;
    public void throwBase() throws SubException;
    public void throwSub() throws SubException;
    public void throwSubSub() throws SubException;

    public void bothThrowBase() throws SubException, BaseException, SubSubException;
}

/**
 * Implement all of the proxied interfaces.
 */
class WTMix implements InterfaceW1, InterfaceW2 {
    public int dastardlyDeed() throws SubException {
        System.out.println("Throwing SubException");
        throw new SubException();
    }

    /* these don't actually get called; they just cause exceptions */
    public void throwFunky() {}
    public void throwFunky2() {}
    public void throwException() throws SubException {}
    public void throwBase() throws SubException {}
    public void throwSub() throws SubException {}
    public void throwSubSub() throws SubException {}

    public void bothThrowBase() throws BaseException, SubException {}
}

/**
 * Invocation handler for our proxy class.
 */
class WTInvocationHandler implements InvocationHandler {
    private Object mObj;

    public WTInvocationHandler(Object obj) {
        mObj = obj;
    }

    /*
     * This is called when anything gets invoked in the proxy object.
     */
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {

        Object result = null;

        // Trap Object calls.  This is important here to avoid a recursive
        // invocation of toString() in the print statements below.
        if (method.getDeclaringClass() == java.lang.Object.class) {
            //System.out.println("!!! object " + method.getName());
            if (method.getName().equals("toString"))
                return super.toString();
            else if (method.getName().equals("hashCode"))
                return Integer.valueOf(super.hashCode());
            else if (method.getName().equals("equals"))
                return Boolean.valueOf(super.equals(args[0]));
            else
                throw new RuntimeException("huh?");
        }

        System.out.println("Invoke " + method);
        if (args == null || args.length == 0) {
            System.out.println(" (no args)");
        } else {
            for (int i = 0; i < args.length; i++)
                System.out.println(" " + i + ": " + args[i]);
        }

        try {
            if (method.getName().equals("throwFunky"))
                throw new InterruptedException("fake");
            if (method.getName().equals("throwFunky2"))
                throw new IOException("fake2");
            if (method.getName().equals("throwException"))
                throw new Exception();
            if (method.getName().equals("throwBase"))
                throw new BaseException();
            if (method.getName().equals("throwSub"))
                throw new SubException();
            if (method.getName().equals("throwSubSub"))
                throw new SubSubException();
            if (method.getName().equals("bothThrowBase"))
                throw new BaseException();

            if (true)
                result = method.invoke(mObj, args);
            else
                result = -1;
            System.out.println("Success: method " + method.getName()
                + " res=" + result);
        } catch (InvocationTargetException ite) {
            throw ite.getTargetException();
        } catch (IllegalAccessException iae) {
            throw new RuntimeException(iae);
        }
        return result;
    }
}