• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 import java.lang.reflect.*;
17 import java.util.Arrays;
18 
19 class NarrowingTest {
20 
21    interface I1 {
foo()22        public Object foo();
23    }
24 
25    interface I2 extends I1 {
26        // Note that this method declaration narrows the return type.
27        @Override
foo()28        public String foo();
29    }
30 
main(String[] args)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                });
48        Main.registerProxyClassName(proxy.getClass().getCanonicalName());
49 
50        Method[] methods = proxy.getClass().getDeclaredMethods();
51        System.out.println("Proxy methods: " +
52                           Main.replaceProxyClassNamesForOutput(Arrays.deepToString(methods)));
53 
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 }
70