blob: 5b80d7284f3ada0781ffb70d134ad847f4fa823f [file] [log] [blame]
Ian Rogers530f71c2013-02-22 23:29:00 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import java.lang.reflect.*;
17import java.util.Arrays;
18
19class NarrowingTest {
20
21 interface I1 {
22 public Object foo();
23 }
24
25 interface I2 extends I1 {
26 // Note that this method declaration narrows the return type.
27 @Override
28 public String foo();
29 }
30
31 public static void main(String[] args) {
32 I2 proxy = (I2) Proxy.newProxyInstance(NarrowingTest.class.getClassLoader(),
33 new Class<?>[] { I2.class },
34 new InvocationHandler() {
35 int count = 0;
36 @Override
37 public Object invoke(Object proxy, Method method,
38 Object[] args) throws Throwable {
39 System.out.println("Invocation of " + method);
40 if (count == 0) {
41 count++;
42 return "hello";
43 } else {
44 return Integer.valueOf(1);
45 }
46 }
47 });
Hiroshi Yamauchi75d50532015-12-04 16:18:49 -080048 Main.registerProxyClassName(proxy.getClass().getCanonicalName());
Ian Rogers530f71c2013-02-22 23:29:00 -080049
50 Method[] methods = proxy.getClass().getDeclaredMethods();
Hiroshi Yamauchi75d50532015-12-04 16:18:49 -080051 System.out.println("Proxy methods: " +
52 Main.replaceProxyClassNamesForOutput(Arrays.deepToString(methods)));
Ian Rogers530f71c2013-02-22 23:29:00 -080053
54 System.out.println("Invoking foo using I2 type: " + proxy.foo());
55
56 I1 proxyAsParent = proxy;
57 System.out.println("Invoking foo using I1 type: " + proxyAsParent.foo());
58
59 try {
60 proxy.foo();
61 System.out.println("Didn't get expected exception");
62 } catch (ClassCastException e) {
63 // With an I2 invocation returning an integer is an exception.
64 System.out.println("Got expected exception");
65 }
66
67 System.out.println("Proxy narrowed invocation return type passed");
68 }
69}