Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/DirectHandleTest.java
6007 views
/*******************************************************************************1* Copyright (c) 2014, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9.jsr292;2223import org.testng.annotations.Test;24import org.testng.AssertJUnit;25import java.lang.invoke.MethodHandle;26import java.lang.invoke.MethodHandles;27import java.lang.invoke.MethodType;28import java.lang.reflect.Field;2930/**31* A class to check if a specific method is correctly compiled by JIT32*/33public class DirectHandleTest {3435@Test(groups = { "level.extended" })36public void testIfDirectHandleTestIsJitted() throws Throwable {37try {38TestClass_DH clazz = new TestClass_DH();39MethodHandle mh = MethodHandles.lookup().findVirtual(TestClass_DH.class, "test", MethodType.methodType(void.class));40AssertJUnit.assertTrue(mh.getClass().toString().equals("class java.lang.invoke.DirectHandle"));41try {42while (true) {43mh.invokeExact(clazz);44}45} catch (Error t) {46t.printStackTrace();47}48} catch (JittingDetector j) {49AssertJUnit.assertTrue(true);50}51}52}5354class TestClass_DH {5556static long PC = 0;5758static final Field throwable_walkback;5960static {61Field f;62try {63f = Throwable.class.getDeclaredField("walkback");64f.setAccessible(true);65throwable_walkback = f;66} catch (NoSuchFieldException | SecurityException e) {67throw new RuntimeException();68}69}7071public final void test() throws Throwable {72Throwable t = new Throwable();73Object walkback = throwable_walkback.get(t);74if (walkback instanceof int[]) {75int[] iWalkback = (int[])walkback;76if (PC == 0) {77PC = iWalkback[0];78} else if (PC != iWalkback[0]) {79throw new JittingDetector("detected jitting");80}81} else {82long[] iWalkback = (long[])walkback;83if (PC == 0) {84PC = iWalkback[0];85} else if (PC != iWalkback[0]) {86throw new JittingDetector("detected jitting");87}88}89}90}919293