1 /* 2 * Copyright (C) 2017 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 package invokecustom; 18 19 import java.lang.invoke.CallSite; 20 import java.lang.invoke.ConstantCallSite; 21 import java.lang.invoke.MethodHandle; 22 import java.lang.invoke.MethodHandles; 23 import java.lang.invoke.MethodType; 24 25 abstract class Super { 26 public void targetMethodTest4() { 27 System.out.println("targetMethodTest4 from Super"); 28 } 29 30 public abstract void helperMethodTest9(); 31 } 32 33 public class InvokeCustom extends Super implements Runnable { 34 35 public InvokeCustom() {} 36 public InvokeCustom(int i) { 37 System.out.println("InvokeCustom.<init>(" + i + ")"); 38 } 39 40 private static void targetMethodTest1() { 41 System.out.println("Hello World!"); 42 } 43 44 private static void targetMethodTest2(boolean z, byte b, char c, short s, int i, float f, long l, 45 double d, String str) { 46 System.out.println(z); 47 System.out.println(b); 48 System.out.println(c); 49 System.out.println(s); 50 System.out.println(i); 51 System.out.println(f); 52 System.out.println(l); 53 System.out.println(d); 54 System.out.println(str); 55 } 56 57 private static void targetMethodTest3() { 58 System.out.println("targetMethodTest3 from InvokeCustom"); 59 } 60 61 @Override 62 public void targetMethodTest4() { 63 // The generated code should be calling Super.targetMethodTest4. 64 System.out.println("targetMethodTest4 from InvokeCustom (oops!)"); 65 } 66 67 public static int targetMethodTest5(int x, int y, int total) { 68 int calculated = x + y; 69 System.out.println("targetMethodTest5 " + x + " + " + y + " = " + calculated); 70 if (calculated != total) { 71 System.out.println("Failed " + calculated + " != " + total); 72 } 73 return calculated; 74 } 75 76 public static long targetMethodTest6(long x, long y, long total) { 77 long calculated = x + y; 78 System.out.println("targetMethodTest6 " + x + " + " + y + " = " + calculated); 79 if (calculated != total) { 80 System.out.println("Failed " + calculated + " != " + total); 81 } 82 return calculated; 83 } 84 85 public static double targetMethodTest7(float x, float y, double product) { 86 double calculated = x * y; 87 System.out.println("targetMethodTest7 " + x + " * " + y + " = " + calculated); 88 if (calculated != product) { 89 System.out.println("Failed " + calculated + " != " + product); 90 } 91 return calculated; 92 } 93 94 public static void targetMethodTest8(String s) { 95 System.out.println("targetMethodTest8 " + s); 96 } 97 98 private static int staticFieldTest9 = 0; 99 100 private static void checkStaticFieldTest9(MethodHandle getter, MethodHandle setter) 101 throws Throwable { 102 final int NEW_VALUE = 0x76543210; 103 int oldValue = (int) getter.invokeExact(); 104 setter.invokeExact(NEW_VALUE); 105 int newValue = (int) getter.invokeExact(); 106 System.out.print("checkStaticFieldTest9: old " + oldValue + " new " + newValue + 107 " expected " + NEW_VALUE + " "); 108 System.out.println((newValue == NEW_VALUE) ? "OK" : "ERROR"); 109 } 110 111 private float fieldTest9 = 0.0f; 112 113 private void checkFieldTest9(MethodHandle getter, MethodHandle setter) 114 throws Throwable { 115 final float NEW_VALUE = 1.99e-19f; 116 float oldValue = (float) getter.invokeExact(this); 117 setter.invokeExact(this, NEW_VALUE); 118 float newValue = (float) getter.invokeExact(this); 119 System.out.print("checkFieldTest9: old " + oldValue + " new " + newValue + 120 " expected " + NEW_VALUE + " "); 121 System.out.println((newValue == NEW_VALUE) ? "OK" : "ERROR"); 122 } 123 124 public void helperMethodTest9() { 125 System.out.println("helperMethodTest9 in " + InvokeCustom.class); 126 } 127 128 private static void targetMethodTest9() { 129 System.out.println("targetMethodTest9()"); 130 } 131 132 public void run() { 133 System.out.println("run() for Test9"); 134 } 135 136 public static CallSite bsmLookupStatic(MethodHandles.Lookup caller, String name, MethodType type) 137 throws NoSuchMethodException, IllegalAccessException { 138 System.out.println("bsmLookupStatic []"); 139 final MethodHandles.Lookup lookup = MethodHandles.lookup(); 140 final MethodHandle targetMH = lookup.findStatic(lookup.lookupClass(), name, type); 141 return new ConstantCallSite(targetMH.asType(type)); 142 } 143 144 public static CallSite bsmLookupStaticWithExtraArgs( 145 MethodHandles.Lookup caller, String name, MethodType type, int i, long l, float f, double d) 146 throws NoSuchMethodException, IllegalAccessException { 147 System.out.println("bsmLookupStaticWithExtraArgs [" + i + ", " + l + ", " + f + ", " + d + "]"); 148 final MethodHandles.Lookup lookup = MethodHandles.lookup(); 149 final MethodHandle targetMH = lookup.findStatic(lookup.lookupClass(), name, type); 150 return new ConstantCallSite(targetMH.asType(type)); 151 } 152 153 public static CallSite bsmCreateCallSite( 154 MethodHandles.Lookup caller, String name, MethodType type, MethodHandle mh) 155 throws Throwable { 156 System.out.println("bsmCreateCallSite [" + mh + "]"); 157 return new ConstantCallSite(mh); 158 } 159 160 public static CallSite bsmLookupTest9(MethodHandles.Lookup caller, String name, MethodType type, 161 MethodHandle staticGetter, MethodHandle staticSetter, 162 MethodHandle fieldGetter, MethodHandle fieldSetter, 163 MethodHandle instanceInvoke, MethodHandle constructor, 164 MethodHandle interfaceInvoke) 165 throws Throwable { 166 System.out.println("bsmLookupTest9 [" + staticGetter + ", " + staticSetter + ", " + 167 fieldGetter + ", " + fieldSetter + "]"); 168 System.out.println(name + " " + type); 169 170 // Check constant method handles passed can be invoked. 171 checkStaticFieldTest9(staticGetter, staticSetter); 172 InvokeCustom instance = new InvokeCustom(); 173 instance.checkFieldTest9(fieldGetter, fieldSetter); 174 175 // Check virtual method. 176 instanceInvoke.invokeExact(instance); 177 178 InvokeCustom instance2 = (InvokeCustom) constructor.invokeExact(3); 179 interfaceInvoke.invoke(instance2); 180 181 final MethodHandles.Lookup lookup = MethodHandles.lookup(); 182 final MethodHandle targetMH = lookup.findStatic(lookup.lookupClass(), name, type); 183 return new ConstantCallSite(targetMH.asType(type)); 184 } 185 } 186