summaryrefslogtreecommitdiff
path: root/test/952-invoke-custom/src/TestInvocationKinds.java
blob: 7b88c18c66950d0b2abd0c48623322a9b9ca3601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import annotations.BootstrapMethod;
import annotations.CalledByIndy;
import java.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class TestInvocationKinds extends TestBase {
    private static int static_field;
    private double instance_field;

    static CallSite lookupStaticFieldGetter(
            MethodHandles.Lookup lookup, String name, MethodType methodType) throws Throwable {
        // methodType = "()LfieldType;"
        MethodHandle mh =
                lookup.findStaticGetter(TestInvocationKinds.class, name, methodType.returnType());
        return new ConstantCallSite(mh);
    }

    @CalledByIndy(
        bootstrapMethod =
                @BootstrapMethod(
                    enclosingType = TestInvocationKinds.class,
                    name = "lookupStaticFieldSetter"
                ),
        fieldOrMethodName = "static_field",
        returnType = void.class,
        parameterTypes = {int.class}
    )
    private static void setStaticField(int value) {
        assertNotReached();
    }

    static CallSite lookupStaticFieldSetter(
            MethodHandles.Lookup lookup, String name, MethodType methodType) throws Throwable {
        // methodType = "(LfieldType;)V"
        MethodHandle mh =
                lookup.findStaticSetter(
                        TestInvocationKinds.class, name, methodType.parameterType(0));
        return new ConstantCallSite(mh);
    }

    @CalledByIndy(
        bootstrapMethod =
                @BootstrapMethod(
                    enclosingType = TestInvocationKinds.class,
                    name = "lookupStaticFieldGetter"
                ),
        fieldOrMethodName = "static_field",
        returnType = int.class,
        parameterTypes = {}
    )
    private static int getStaticField() {
        assertNotReached();
        return 0;
    }

    static CallSite lookupInstanceFieldSetter(
            MethodHandles.Lookup lookup, String name, MethodType methodType) throws Throwable {
        // methodType = "(Lreceiver;LfieldType;)V"
        MethodHandle mh =
                lookup.findSetter(methodType.parameterType(0), name, methodType.parameterType(1));
        return new ConstantCallSite(mh);
    }

    @CalledByIndy(
        bootstrapMethod =
                @BootstrapMethod(
                    enclosingType = TestInvocationKinds.class,
                    name = "lookupInstanceFieldSetter"
                ),
        fieldOrMethodName = "instance_field",
        returnType = void.class,
        parameterTypes = {TestInvocationKinds.class, double.class}
    )
    private static void setInstanceField(TestInvocationKinds instance, double value) {
        assertNotReached();
        instance.instance_field = Double.NaN;
    }

    static CallSite lookupInstanceFieldGetter(
            MethodHandles.Lookup lookup, String name, MethodType methodType) throws Throwable {
        // methodType = "(Lreceiver;)LfieldType;"
        MethodHandle mh =
                lookup.findGetter(methodType.parameterType(0), name, methodType.returnType());
        return new ConstantCallSite(mh);
    }

    @CalledByIndy(
        bootstrapMethod =
                @BootstrapMethod(
                    enclosingType = TestInvocationKinds.class,
                    name = "lookupInstanceFieldGetter"
                ),
        fieldOrMethodName = "instance_field",
        returnType = double.class,
        parameterTypes = {TestInvocationKinds.class}
    )
    private static double getInstanceField(TestInvocationKinds instance) {
        assertNotReached();
        return Double.NaN;
    }

    private static void testStaticFieldAccessors() {
        System.out.println("testStaticFieldAccessors");
        setStaticField(3);
        assertEquals(static_field, 3);
        setStaticField(4);
        assertEquals(static_field, 4);
        assertEquals(static_field, getStaticField());
        static_field = Integer.MAX_VALUE;
        assertEquals(Integer.MAX_VALUE, getStaticField());
    }

    private static void testInstanceFieldAccessors() {
        System.out.println("testInstanceFieldAccessors");
        TestInvocationKinds instance = new TestInvocationKinds();
        instance.instance_field = Double.MIN_VALUE;
        setInstanceField(instance, Math.PI);
        assertEquals(Math.PI, instance.instance_field);
        instance.instance_field = Math.E;
        assertEquals(Math.E, getInstanceField(instance));
    }

    private static CallSite lookupVirtual(
            MethodHandles.Lookup lookup, String name, MethodType methodType) throws Throwable {
        // To get the point-of-use and invokedynamic to work the methodType here has the
        // receiver type as the leading paramter which needs to be dropped for findVirtual().
        MethodType mt = methodType.dropParameterTypes(0, 1);
        MethodHandle mh = lookup.findVirtual(TestInvocationKinds.class, name, mt);
        return new ConstantCallSite(mh);
    }

    @CalledByIndy(
        bootstrapMethod =
                @BootstrapMethod(enclosingType = TestInvocationKinds.class, name = "lookupVirtual"),
        fieldOrMethodName = "getMaxIntegerValue",
        returnType = int.class,
        parameterTypes = {TestInvocationKinds.class, int.class, int.class}
    )
    private static int maxIntegerValue(TestInvocationKinds receiver, int x, int y) {
        assertNotReached();
        return 0;
    }

    public int getMaxIntegerValue(int x, int y) {
        return x > y ? x : y;
    }

    static void testInvokeVirtual() {
        System.out.print("testInvokeVirtual => max(77, -3) = ");
        TestInvocationKinds receiver = new TestInvocationKinds();
        int result = maxIntegerValue(receiver, 77, -3);
        System.out.println(result);
    }

    static class Widget {
        int value;
        public Widget(int value) {}
    }

    private static CallSite lookupConstructor(
            MethodHandles.Lookup lookup, String name, MethodType methodType) throws Throwable {
        // methodType = (constructorParams);classToBeConstructed
        Class<?> cls = methodType.returnType();
        MethodType constructorMethodType = methodType.changeReturnType(void.class);
        MethodHandle mh = lookup.findConstructor(cls, constructorMethodType);
        return new ConstantCallSite(mh);
    }

    @CalledByIndy(
        bootstrapMethod =
                @BootstrapMethod(
                    enclosingType = TestInvocationKinds.class,
                    name = "lookupConstructor"
                ),
        fieldOrMethodName = "unused",
        returnType = Widget.class,
        parameterTypes = {int.class}
    )
    private static Widget makeWidget(int v) {
        assertNotReached();
        return null;
    }

    static void testConstructor() {
        System.out.print("testConstructor => ");
        Widget receiver = makeWidget(3);
        assertEquals(Widget.class, receiver.getClass());
        System.out.println(receiver.getClass());
    }

    public static void test() {
        System.out.println(TestInvocationKinds.class.getName());
        testStaticFieldAccessors();
        testInstanceFieldAccessors();
        testInvokeVirtual();
        testConstructor();
    }
}