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 49 Method[] methods = proxy.getClass().getDeclaredMethods(); 50 System.out.println("Proxy methods: " + Arrays.deepToString(methods)); 51 52 System.out.println("Invoking foo using I2 type: " + proxy.foo()); 53 54 I1 proxyAsParent = proxy; 55 System.out.println("Invoking foo using I1 type: " + proxyAsParent.foo()); 56 57 try { 58 proxy.foo(); 59 System.out.println("Didn't get expected exception"); 60 } catch (ClassCastException e) { 61 // With an I2 invocation returning an integer is an exception. 62 System.out.println("Got expected exception"); 63 } 64 65 System.out.println("Proxy narrowed invocation return type passed"); 66 } 67 } 68