Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2008 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 
     17 import java.lang.reflect.InvocationHandler;
     18 import java.lang.reflect.InvocationTargetException;
     19 import java.lang.reflect.Method;
     20 import java.lang.reflect.Proxy;
     21 
     22 /*
     23  * Try to instantiate a proxy class with interfaces that have conflicting
     24  * duplicate methods (primitive vs. object).
     25  */
     26 public class Clash {
     27     public static void main(String[] args) {
     28         InvocationHandler handler = new ClashInvocationHandler();
     29 
     30         /* try passing in the same interface twice */
     31         try {
     32             Proxy.newProxyInstance(Clash.class.getClassLoader(),
     33                 new Class<?>[] { Interface1A.class, Interface1A.class },
     34                 handler);
     35             System.out.println("Dupe did not throw expected exception");
     36         } catch (IllegalArgumentException iae) {
     37             System.out.println("Dupe threw expected exception");
     38         }
     39 
     40         try {
     41             Proxy.newProxyInstance(Clash.class.getClassLoader(),
     42                 new Class<?>[] { Interface1A.class, Interface1B.class },
     43                 handler);
     44             System.out.println("Clash did not throw expected exception");
     45         } catch (IllegalArgumentException iae) {
     46             System.out.println("Clash threw expected exception");
     47         }
     48     }
     49 }
     50 
     51 interface Interface1A {
     52     public int thisIsOkay();
     53 
     54     public float thisIsTrouble();
     55 }
     56 
     57 interface Interface1B {
     58     public int thisIsOkay();
     59 
     60     public Object thisIsTrouble();
     61 }
     62 
     63 class ClashInvocationHandler implements InvocationHandler {
     64     /* don't really need to do anything -- should never get this far */
     65     public Object invoke(Object proxy, Method method, Object[] args)
     66         throws Throwable {
     67 
     68         return null;
     69     }
     70 }
     71