1 /* 2 * Copyright (C) 2018 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.invoke.MethodHandle; 18 import java.lang.invoke.MethodHandles; 19 import java.lang.invoke.MethodType; 20 import java.lang.invoke.VarHandle; 21 import java.lang.reflect.InvocationTargetException; 22 import java.lang.reflect.Method; 23 24 public class Main { 25 private static final int ITERATIONS = 100; 26 27 private static final VarHandle widgetIdVarHandle; 28 getHotnessCounter(Class<?> cls, String methodName)29 public static native int getHotnessCounter(Class<?> cls, String methodName); 30 31 public static class Widget { Widget(int id)32 public Widget(int id) { 33 this.id = id; 34 } 35 getId()36 int getId() { 37 return id; 38 } 39 40 int id; 41 } 42 43 static { 44 try { 45 widgetIdVarHandle = MethodHandles.lookup().findVarHandle(Widget.class, "id", int.class); 46 } catch (Exception e) { 47 throw new Error(e); 48 } 49 } 50 assertEquals(int i1, int i2)51 private static void assertEquals(int i1, int i2) { 52 if (i1 == i2) { 53 return; 54 } 55 throw new AssertionError("assertEquals i1: " + i1 + ", i2: " + i2); 56 } 57 assertEquals(Object o, Object p)58 private static void assertEquals(Object o, Object p) { 59 if (o == p) { 60 return; 61 } 62 if (o != null && p != null && o.equals(p)) { 63 return; 64 } 65 throw new AssertionError("assertEquals: o1: " + o + ", o2: " + p); 66 } 67 fail()68 private static void fail() { 69 System.out.println("fail"); 70 Thread.dumpStack(); 71 } 72 fail(String message)73 private static void fail(String message) { 74 System.out.println("fail: " + message); 75 Thread.dumpStack(); 76 } 77 testMethodHandleCounters()78 private static void testMethodHandleCounters() throws Throwable { 79 for (int i = 0; i < ITERATIONS; ++i) { 80 // Regular MethodHandle invocations 81 MethodHandle mh = 82 MethodHandles.lookup() 83 .findConstructor( 84 Widget.class, MethodType.methodType(void.class, int.class)); 85 Widget w = (Widget) mh.invoke(3); 86 w = (Widget) mh.invokeExact(3); 87 assertEquals(0, getHotnessCounter(MethodHandle.class, "invoke")); 88 assertEquals(0, getHotnessCounter(MethodHandle.class, "invokeExact")); 89 90 // Reflective MethodHandle invocations 91 String[] methodNames = {"invoke", "invokeExact"}; 92 for (String methodName : methodNames) { 93 Method invokeMethod = MethodHandle.class.getMethod(methodName, Object[].class); 94 MethodHandle instance = 95 MethodHandles.lookup() 96 .findVirtual( 97 Widget.class, "getId", MethodType.methodType(int.class)); 98 try { 99 invokeMethod.invoke(instance, new Object[] {new Object[] {}}); 100 fail(); 101 } catch (InvocationTargetException ite) { 102 assertEquals(ite.getCause().getClass(), UnsupportedOperationException.class); 103 } 104 } 105 assertEquals(0, getHotnessCounter(MethodHandle.class, "invoke")); 106 assertEquals(0, getHotnessCounter(MethodHandle.class, "invokeExact")); 107 } 108 109 System.out.println("MethodHandle OK"); 110 } 111 testVarHandleCounters()112 private static void testVarHandleCounters() throws Throwable { 113 Widget w = new Widget(0); 114 for (int i = 0; i < ITERATIONS; ++i) { 115 // Regular accessor invocations 116 widgetIdVarHandle.set(w, i); 117 assertEquals(i, widgetIdVarHandle.get(w)); 118 assertEquals(0, getHotnessCounter(VarHandle.class, "set")); 119 assertEquals(0, getHotnessCounter(VarHandle.class, "get")); 120 121 // Reflective accessor invocations 122 for (String accessorName : new String[] {"get", "set"}) { 123 Method setMethod = VarHandle.class.getMethod(accessorName, Object[].class); 124 try { 125 setMethod.invoke(widgetIdVarHandle, new Object[] {new Object[0]}); 126 fail(); 127 } catch (InvocationTargetException ite) { 128 assertEquals(ite.getCause().getClass(), UnsupportedOperationException.class); 129 } 130 } 131 assertEquals(0, getHotnessCounter(VarHandle.class, "set")); 132 assertEquals(0, getHotnessCounter(VarHandle.class, "get")); 133 } 134 System.out.println("VarHandle OK"); 135 } 136 main(String[] args)137 public static void main(String[] args) throws Throwable { 138 System.loadLibrary(args[0]); 139 testMethodHandleCounters(); 140 testVarHandleCounters(); 141 } 142 } 143