1 /* 2 * Copyright (C) 2023 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.*; 18 import dalvik.system.VMRuntime; 19 20 public class Main { main(String... args)21 public static void main(String... args) throws Throwable { 22 System.loadLibrary(args[0]); 23 enableHiddenApiChecks(); 24 // MH.identity(...) methods were marked as hidden in aosp/321456. 25 VMRuntime.getRuntime().setTargetSdkVersion(28); 26 27 MethodHandle intIdentity = MethodHandles.identity(int.class); 28 29 int value = 42; 30 int returnedValue = (int) intIdentity.invokeExact(value); 31 32 if (returnedValue != value) { 33 System.out.printf("Expected: %d, but identity MH returned %d\n", 34 value, returnedValue); 35 throw new AssertionError("identity fail"); 36 } 37 38 value = 101; 39 MethodHandle intConstant = MethodHandles.constant(int.class, value); 40 returnedValue = (int) intConstant.invokeExact(); 41 42 if (returnedValue != value) { 43 System.out.printf("Expected: %d, but constant MH returned %d\n", 44 value, returnedValue); 45 throw new AssertionError("constant failed"); 46 } 47 48 int secondCallValue = (int) intConstant.invokeExact(); 49 if (secondCallValue != value) { 50 System.out.printf("Expected: %d, but constant MH returned %d on subsequent call\n", 51 value, returnedValue); 52 throw new AssertionError("constant failed"); 53 } 54 } 55 enableHiddenApiChecks()56 private static native void enableHiddenApiChecks(); 57 } 58