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.util.ArrayList;
30import java.util.List;
31import java.util.concurrent.Callable;
32import java.util.function.Predicate;
33import java.util.stream.Collectors;
34
35/**
36 * This is a helper class that provides tests with methods
37 */
38public class PoolHelper extends MethodHolder {
39    private static final List<Pair<Executable, Callable<?>>> METHODS;
40
41    /**
42     * Filters only those methods who belong to Klass or its internal class
43     * Internal and named as "method" or is a constructor
44     */
45    public static final Predicate<Executable> METHOD_FILTER = executable -> {
46        String methodName = executable.getName();
47        String className = executable.getDeclaringClass().getName();
48        return className.matches(".*(Klass)(\\$Internal)?") &&
49                (methodName.equals("method") ||
50                        methodName.equals(className)); // if method is <init>
51    };
52
53    static {
54        METHODS = new ArrayList<>();
55        List<MethodHolder> holders = new ArrayList<>();
56        holders.add(new compiler.compilercontrol.share.pool.sub.Klass());
57        holders.add(new compiler.compilercontrol.share.pool.sub.KlassDup());
58        holders.add(new compiler.compilercontrol.share.pool.subpack.Klass());
59        holders.add(new compiler.compilercontrol.share.pool.subpack.KlassDup());
60        holders.add(new compiler.compilercontrol.share.pool.sub.Klass.Internal());
61        holders.add(new compiler.compilercontrol.share.pool.subpack.KlassDup.Internal());
62        for (MethodHolder holder : holders) {
63            METHODS.addAll(holder.getAllMethods());
64        }
65    }
66
67    /**
68     * Gets all methods from the pool using specified filter
69     *
70     * @param filter method filter
71     * @return pairs of Executable and appropriate Callable
72     */
73    public List<Pair<Executable, Callable<?>>> getAllMethods(
74            Predicate<Executable> filter) {
75        return getAllMethods().stream()
76                .filter(pair -> filter.test(pair.first))
77                .collect(Collectors.toList());
78    }
79
80    /**
81     * Gets all methods from the pool
82     *
83     * @return pairs of Executable and appropriate Callable
84     */
85    @Override
86    public List<Pair<Executable, Callable<?>>> getAllMethods() {
87        return METHODS;
88    }
89}
90