Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/pool/MethodHolder.java
41161 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.compilercontrol.share.pool;2425import jdk.test.lib.util.Pair;2627import java.lang.reflect.Executable;28import java.lang.reflect.Method;29import java.util.ArrayList;30import java.util.List;31import java.util.concurrent.Callable;3233/**34* Represents a holder that contains test methods35*/36public abstract class MethodHolder {37/**38* Helper method to get executable for the specified method39*40* @param holder class that holds specified method41* @param name method name42* @param parameterTypes parameter types of the specified method43* @return {@link Method} instance44*/45public Method getMethod(MethodHolder holder,46String name,47Class<?>... parameterTypes) {48try {49return holder.getClass().getDeclaredMethod(name, parameterTypes);50} catch (NoSuchMethodException e) {51throw new Error("TESTBUG: Can't get method " + name, e);52}53}5455/**56* Gets all test methods57*58* @return pairs of Executable and appropriate Callable59*/60public List<Pair<Executable, Callable<?>>> getAllMethods() {61Class<?> aClass = this.getClass();62Object classInstance;63try {64classInstance = aClass.newInstance();65} catch (ReflectiveOperationException e) {66throw new Error("TESTBUG: unable to get new instance", e);67}68List<Pair<Executable, Callable<?>>> pairs = new ArrayList<>();69{70Method method = getMethod(this, "method", int.class, String[].class,71Integer.class, byte[].class, double[][].class);72Pair<Executable, Callable<?>> pair = new Pair<>(method,73() -> {74// Make args75int a = 0;76String[] ss = {"a", "b", "c", "d"};77Integer i = 1;78byte[] bb = {1, 2};79double[][] dd = {80{1.618033, 3.141592},81{2.718281, 0.007874}82};83// Invoke method84method.invoke(classInstance, a, ss, i, bb, dd);85return true;86});87pairs.add(pair);88}89{90Method method = getMethod(this, "method");91Pair<Executable, Callable<?>> pair = new Pair<>(method,92() -> {93method.invoke(classInstance);94return true;95});96pairs.add(pair);97}98{99Method method = getMethod(this, "smethod");100Pair<Executable, Callable<?>> pair = new Pair<>(method,101() -> method.invoke(classInstance));102pairs.add(pair);103}104{105Method method = getMethod(this, "smethod", int.class, int[].class);106Pair<Executable, Callable<?>> pair = new Pair<>(method,107() -> {108int[] array = {1, 2, 3};109return method.invoke(classInstance, 42, array);110});111pairs.add(pair);112}113{114Method method = getMethod(this, "smethod", Integer.class);115Pair<Executable, Callable<?>> pair = new Pair<>(method,116() -> method.invoke(classInstance, 100));117pairs.add(pair);118}119return pairs;120}121}122123124