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 import annotations.BootstrapMethod; 18 import annotations.CalledByIndy; 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 public class TestLinkerMethodMinimalArguments extends TestBase { 26 private static int forceFailureType = 0; 27 28 static final int FAILURE_TYPE_NONE = 0; 29 static final int FAILURE_TYPE_LINKER_METHOD_RETURNS_NULL = 1; 30 static final int FAILURE_TYPE_LINKER_METHOD_THROWS = 2; 31 static final int FAILURE_TYPE_TARGET_METHOD_THROWS = 3; 32 33 @CalledByIndy( 34 bootstrapMethod = 35 @BootstrapMethod( 36 enclosingType = TestLinkerMethodMinimalArguments.class, 37 parameterTypes = {MethodHandles.Lookup.class, String.class, MethodType.class}, 38 name = "linkerMethod" 39 ), 40 fieldOrMethodName = "_add", 41 returnType = int.class, 42 parameterTypes = {int.class, int.class} 43 ) add(int a, int b)44 private static int add(int a, int b) { 45 assertNotReached(); 46 return -1; 47 } 48 49 @SuppressWarnings("unused") _add(int a, int b)50 static int _add(int a, int b) { 51 if (forceFailureType == FAILURE_TYPE_TARGET_METHOD_THROWS) { 52 System.out.println("Throwing ArithmeticException in add()"); 53 throw new ArithmeticException("add"); 54 } 55 return a + b; 56 } 57 58 @SuppressWarnings("unused") linkerMethod( MethodHandles.Lookup caller, String name, MethodType methodType)59 private static CallSite linkerMethod( 60 MethodHandles.Lookup caller, String name, MethodType methodType) throws Throwable { 61 System.out.println("linkerMethod failure type " + forceFailureType); 62 MethodHandle mh_add = 63 caller.findStatic(TestLinkerMethodMinimalArguments.class, name, methodType); 64 switch (forceFailureType) { 65 case FAILURE_TYPE_LINKER_METHOD_RETURNS_NULL: 66 System.out.println( 67 "Returning null instead of CallSite for " + name + " " + methodType); 68 return null; 69 case FAILURE_TYPE_LINKER_METHOD_THROWS: 70 System.out.println("Throwing InstantiationException in linkerMethod()"); 71 throw new InstantiationException("linkerMethod"); 72 default: 73 return new ConstantCallSite(mh_add); 74 } 75 } 76 test(int failureType, int x, int y)77 public static void test(int failureType, int x, int y) throws Throwable { 78 assertTrue(failureType >= FAILURE_TYPE_NONE); 79 assertTrue(failureType <= FAILURE_TYPE_TARGET_METHOD_THROWS); 80 forceFailureType = failureType; 81 assertEquals(x + y, add(x, y)); 82 System.out.println("Failure Type + " + failureType + " (" + x + y + ")"); 83 } 84 } 85