Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright 2016 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.Constructor;
     18 import java.lang.reflect.InvocationHandler;
     19 import java.lang.reflect.Method;
     20 import java.lang.reflect.Proxy;
     21 
     22 /**
     23  * Tests proxies when used with constructor methods.
     24  */
     25 class ConstructorProxy implements InvocationHandler {
     26   public static void main() {
     27     try {
     28       new ConstructorProxy().runTest();
     29     } catch (Exception e) {
     30       System.out.println("Unexpected failure occured");
     31       e.printStackTrace(System.out);
     32     }
     33   }
     34 
     35   public void runTest() throws Exception {
     36     Class<?> proxyClass = Proxy.getProxyClass(
     37             getClass().getClassLoader(),
     38             new Class<?>[] { Runnable.class }
     39     );
     40     Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
     41     System.out.println("Found constructor.");
     42     // We used to crash when asking the exception types of the constructor, because the runtime was
     43     // not using the non-proxy ArtMethod
     44     Object[] exceptions = constructor.getExceptionTypes();
     45     System.out.println("Found constructors with " + exceptions.length + " exceptions");
     46   }
     47 
     48   @Override
     49   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
     50     return args[0];
     51   }
     52 }
     53 
     54