TestBuilder.java revision 12158:0fe2815ffa74
1/*
2 * Copyright (c) 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 selectionresolution;
25
26import jdk.internal.org.objectweb.asm.Opcodes;
27
28import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
29import static jdk.internal.org.objectweb.asm.Opcodes.ACC_STATIC;
30
31class TestBuilder extends Builder {
32    private final ClassConstruct testClass;
33    private final Method mainMethod;
34
35    public TestBuilder(int classId, SelectionResolutionTestCase testcase) {
36        super(testcase);
37
38        // Make a public class Test that contains all our test methods
39        testClass = new Clazz("Test", null, -1, ACC_PUBLIC);
40
41        // Add a main method
42        mainMethod = testClass.addMethod("main", "([Ljava/lang/String;)V", ACC_PUBLIC + ACC_STATIC);
43
44    }
45
46    public ClassConstruct getMainTestClass() {
47        mainMethod.done();
48        return testClass;
49    }
50
51    public void addTest(ClassConstruct clazz, ClassBuilder.ExecutionMode execMode) {
52        Method m = clazz.addMethod("test", "()Ljava/lang/Integer;", ACC_PUBLIC + ACC_STATIC, execMode);
53        m.defaultInvoke(getInvokeInstruction(testcase.invoke),
54                    getName(testcase.methodref),
55                    getName(testcase.objectref),
56                    testcase.hier.isInterface(testcase.methodref));
57
58        mainMethod.makeStaticCall(clazz.getName(), "test", "()Ljava/lang/Integer;", false).done();
59    }
60
61    private static int getInvokeInstruction(SelectionResolutionTestCase.InvokeInstruction instr) {
62        switch (instr) {
63            case INVOKESTATIC:
64                return Opcodes.INVOKESTATIC;
65            case INVOKESPECIAL:
66                return Opcodes.INVOKESPECIAL;
67            case INVOKEINTERFACE:
68                return Opcodes.INVOKEINTERFACE;
69            case INVOKEVIRTUAL:
70                return Opcodes.INVOKEVIRTUAL;
71            default:
72                throw new AssertionError(instr.name());
73        }
74    }
75}
76