blob: 95d150cbbd97686c5303671dc219b95be171acfe [file] [log] [blame]
Alex Light52c9da02016-05-09 15:31:18 -07001/*
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
17import java.lang.reflect.Constructor;
18import java.lang.reflect.InvocationHandler;
19import java.lang.reflect.Method;
20import java.lang.reflect.Proxy;
21
22/**
23 * Tests proxies when used with constructor methods.
24 */
25class 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();
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