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