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 
18 public class HotProxy {
19 
20   public static final String testName = "HotProxy";
21 
22   interface MyInterface {
voidFoo()23     void voidFoo();
24   }
25 
check(boolean x)26   static void check(boolean x) {
27     if (!x) {
28       throw new AssertionError(testName + " Check failed");
29     }
30   }
31 
32   static class MyInvocationHandler implements InvocationHandler {
invoke(Object proxy, Method method, Object[] args)33     public Object invoke(Object proxy, Method method, Object[] args) {
34       check(proxy instanceof Proxy);
35       check(method.getDeclaringClass() == MyInterface.class);
36       return null;
37     }
38   }
39 
testProxy()40   static void testProxy() {
41     MyInvocationHandler myHandler = new MyInvocationHandler();
42     MyInterface proxyMyInterface =
43         (MyInterface)Proxy.newProxyInstance(HotProxy.class.getClassLoader(),
44                                             new Class<?>[] { MyInterface.class },
45                                             myHandler);
46     // Invoke the proxy method multiple times to ensure it becomes hot and the JIT handles it.
47     for (int i = 0; i < 0x10000; i++) {
48       proxyMyInterface.voidFoo();
49     }
50   }
51 
main(String args[])52   public static void main(String args[]) {
53     testProxy();
54   }
55 }
56