comparison test/compiler/types/correctness/execution/MethodHandleDelegate.java @ 17881:534fbe3d90f0

8038418: New tests development for type profiling and speculation Reviewed-by: roland, iignatyev Contributed-by: Pavel Punegov <pavel.punegov@oracle.com>
author iignatyev
date Fri, 11 Apr 2014 18:46:35 +0400
parents
children
comparison
equal deleted inserted replaced
17880:e1fc9306859a 17881:534fbe3d90f0
1 /*
2 * Copyright (c) 2014, 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
24 package execution;
25
26 import hierarchies.TypeHierarchy;
27 import scenarios.Scenario;
28
29 import java.lang.invoke.MethodHandle;
30 import java.lang.invoke.MethodHandles;
31 import java.lang.invoke.MethodType;
32
33 /**
34 * Executes test scenario using {@link MethodHandle#invoke(Object...)}.
35 * Delegates execution to the given {@link Execution} by creating
36 * new test scenario, see {@link Scenario}
37 */
38 public class MethodHandleDelegate<T extends TypeHierarchy.I, R> implements Execution<T, R> {
39 private final Execution<T, R> delegate;
40
41 public MethodHandleDelegate(Execution<T, R> delegate) {
42 this.delegate = delegate;
43 }
44
45 @Override
46 public void execute(Scenario<T, R> scenario) {
47 delegate.execute(new MHScenario<T, R>(scenario));
48 }
49
50 @Override
51 public String getName() {
52 return "MethodHandleDelegate # " + delegate.getName();
53 }
54
55 private static class MHScenario<T extends TypeHierarchy.I, R> extends Scenario<T, R> {
56 private final Scenario<T, R> scenario;
57 private static final MethodHandle METHOD_HANDLE_RUN;
58
59 static {
60 MethodHandles.Lookup lookup = MethodHandles.lookup();
61 MethodType methodType = MethodType.methodType(Object.class, TypeHierarchy.I.class);
62
63 try {
64 METHOD_HANDLE_RUN = lookup.findVirtual(Scenario.class, "run", methodType);
65 } catch (NoSuchMethodException | IllegalAccessException e) {
66 System.err.println("Failed to get target method run() with " + e);
67 e.printStackTrace();
68 throw new RuntimeException(e);
69 }
70 }
71
72 /**
73 * Constructor
74 *
75 * @param scenario test scenario to be executed
76 */
77 private MHScenario(Scenario<T, R> scenario) {
78 super("MethodHandle::" + scenario.getName(), scenario.profilingType, scenario.hierarchy);
79 this.scenario = scenario;
80 }
81
82 /**
83 * Runs {@link Scenario#run(T)} with {@link MethodHandle#invoke(Object...)}
84 *
85 * @param t subject of the test
86 * @return result of the underlying {@link Scenario#run(T)} invocation
87 */
88 @SuppressWarnings("unchecked")
89 @Override
90 public R run(T t) {
91 try {
92 return (R) METHOD_HANDLE_RUN.invoke(scenario, t);
93 } catch (Throwable thr) {
94 System.err.println(scenario.getName()
95 + " failed to invoke target method run() with " + thr);
96 throw new RuntimeException("Invocation failed", thr);
97 }
98 }
99
100 @Override
101 public void check(R r, T t) {
102 scenario.check(r, t);
103 }
104 }
105 }