File: VarHandleDexTest.java

package info (click to toggle)
android-platform-dalvik 10.0.0%2Br36-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 26,132 kB
  • sloc: java: 270,758; cpp: 8,766; sh: 2,004; javascript: 976; ansic: 534; awk: 368; makefile: 26
file content (243 lines) | stat: -rw-r--r-- 13,876 bytes parent folder | download | duplicates (5)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2017 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 java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.VarHandle;

public class VarHandleDexTest {

    // A static field to access.
    private static boolean bsValue = false;

    // An instance field to access.
    private Float fValue = Float.valueOf(99.9f);

    public static void main(String[] args) throws Throwable {
        // This code is entirely nonsense. It is just to exercise
        // signature polymorphic methods in dx.
        VarHandleDexTest t = new VarHandleDexTest();
        {
            VarHandle vb = MethodHandles.lookup().findStaticVarHandle(t.getClass(), "bsValue", boolean.class);
            boolean newValue = true;
            boolean expectedValue = false;

            boolean b0 = (boolean) vb.compareAndExchangeAcquire(t, expectedValue, newValue);
            vb.compareAndExchangeAcquire(t, expectedValue, newValue);
            boolean b1 = (boolean) vb.compareAndExchange(t, expectedValue, newValue);
            vb.compareAndExchange(t, expectedValue, newValue);
            boolean b2 = (boolean) vb.compareAndExchangeRelease(t, expectedValue, newValue);
            vb.compareAndExchangeRelease(t, expectedValue, newValue);

            boolean r0 = vb.compareAndSet(t, expectedValue, newValue);
            vb.compareAndSet(t, expectedValue, newValue);
            boolean r1 = vb.weakCompareAndSetAcquire(t, expectedValue, newValue);
            vb.weakCompareAndSetAcquire(t, expectedValue, newValue);
            boolean r2 = vb.weakCompareAndSet(t, expectedValue, newValue);
            vb.weakCompareAndSet(t, expectedValue, newValue);
            boolean r3 = vb.weakCompareAndSetPlain(t, expectedValue, newValue);
            vb.weakCompareAndSetPlain(t, expectedValue, newValue);
            boolean r4 = vb.weakCompareAndSetRelease(t, expectedValue, newValue);
            vb.weakCompareAndSetRelease(t, expectedValue, newValue);

            boolean b3 = (boolean) vb.getAndAddAcquire(t, expectedValue, newValue);
            vb.getAndAddAcquire(t, expectedValue, newValue);
            boolean b4 = (boolean) vb.getAndAdd(t, expectedValue, newValue);
            vb.getAndAdd(t, expectedValue, newValue);
            boolean b5 = (boolean) vb.getAndAddRelease(t, expectedValue, newValue);
            vb.getAndAddRelease(t, expectedValue, newValue);
            boolean b6 = (boolean) vb.getAndBitwiseAndAcquire(t, expectedValue, newValue);
            vb.getAndBitwiseAndAcquire(t, expectedValue, newValue);
            boolean b7 = (boolean) vb.getAndBitwiseAnd(t, expectedValue, newValue);
            vb.getAndBitwiseAnd(t, expectedValue, newValue);
            boolean b8 = (boolean) vb.getAndBitwiseAndRelease(t, expectedValue, newValue);
            vb.getAndBitwiseAndRelease(t, expectedValue, newValue);
            boolean b9 = (boolean) vb.getAndBitwiseOrAcquire(t, expectedValue, newValue);
            vb.getAndBitwiseOrAcquire(t, expectedValue, newValue);
            boolean b10 = (boolean) vb.getAndBitwiseOr(t, expectedValue, newValue);
            vb.getAndBitwiseOr(t, expectedValue, newValue);
            boolean b11 = (boolean) vb.getAndBitwiseOrRelease(t, expectedValue, newValue);
            vb.getAndBitwiseOrRelease(t, expectedValue, newValue);
            boolean b12 = (boolean) vb.getAndBitwiseXorAcquire(t, expectedValue, newValue);
            vb.getAndBitwiseXorAcquire(t, expectedValue, newValue);
            boolean b13 = (boolean) vb.getAndBitwiseXor(t, expectedValue, newValue);
            vb.getAndBitwiseXor(t, expectedValue, newValue);
            boolean b14 = (boolean) vb.getAndBitwiseXorRelease(t, expectedValue, newValue);
            vb.getAndBitwiseXorRelease(t, expectedValue, newValue);

            boolean b15 = (boolean) vb.getAndSetAcquire(t, newValue);
            vb.getAndSetAcquire(t, newValue);
            boolean b16 = (boolean) vb.getAndSet(t, newValue);
            vb.getAndSet(t, newValue);
            boolean b17 = (boolean) vb.getAndSetRelease(t, newValue);
            vb.getAndSetRelease(t, newValue);

            boolean b18 = (boolean) vb.get(t);
            vb.get(t);
            boolean b19 = (boolean) vb.getAcquire(t);
            vb.getAcquire(t);
            boolean b20 = (boolean) vb.getOpaque(t);
            vb.getOpaque(t);
            boolean b21 = (boolean) vb.getVolatile(t);
            vb.getVolatile(t);

            vb.set(t, newValue);
            vb.setOpaque(t, newValue);
            vb.setRelease(t, newValue);
            vb.setVolatile(t, newValue);
        }
        {
            VarHandle vf = MethodHandles.lookup().findStaticVarHandle(t.getClass(), "fValue", Float.class);
            Float newValue = Float.valueOf(1.1f);
            Float expectedValue = Float.valueOf(2.2e-6f);

            Float f0 = (Float) vf.compareAndExchangeAcquire(t, expectedValue, newValue);
            vf.compareAndExchangeAcquire(t, expectedValue, newValue);
            Float f1 = (Float) vf.compareAndExchange(t, expectedValue, newValue);
            vf.compareAndExchange(t, expectedValue, newValue);
            Float f2 = (Float) vf.compareAndExchangeRelease(t, expectedValue, newValue);
            vf.compareAndExchangeRelease(t, expectedValue, newValue);

            boolean r0 = vf.compareAndSet(t, expectedValue, newValue);
            vf.compareAndSet(t, expectedValue, newValue);
            boolean r1 = vf.weakCompareAndSetAcquire(t, expectedValue, newValue);
            vf.weakCompareAndSetAcquire(t, expectedValue, newValue);
            boolean r2 = vf.weakCompareAndSet(t, expectedValue, newValue);
            vf.weakCompareAndSet(t, expectedValue, newValue);
            boolean r3 = vf.weakCompareAndSetPlain(t, expectedValue, newValue);
            vf.weakCompareAndSetPlain(t, expectedValue, newValue);
            boolean r4 = vf.weakCompareAndSetRelease(t, expectedValue, newValue);
            vf.weakCompareAndSetRelease(t, expectedValue, newValue);

            Float f3 = (Float) vf.getAndAddAcquire(t, expectedValue, newValue);
            vf.getAndAddAcquire(t, expectedValue, newValue);
            Float f4 = (Float) vf.getAndAdd(t, expectedValue, newValue);
            vf.getAndAdd(t, expectedValue, newValue);
            Float f5 = (Float) vf.getAndAddRelease(t, expectedValue, newValue);
            vf.getAndAddRelease(t, expectedValue, newValue);
            Float f6 = (Float) vf.getAndBitwiseAndAcquire(t, expectedValue, newValue);
            vf.getAndBitwiseAndAcquire(t, expectedValue, newValue);
            Float f7 = (Float) vf.getAndBitwiseAnd(t, expectedValue, newValue);
            vf.getAndBitwiseAnd(t, expectedValue, newValue);
            Float f8 = (Float) vf.getAndBitwiseAndRelease(t, expectedValue, newValue);
            vf.getAndBitwiseAndRelease(t, expectedValue, newValue);
            Float f9 = (Float) vf.getAndBitwiseOrAcquire(t, expectedValue, newValue);
            vf.getAndBitwiseOrAcquire(t, expectedValue, newValue);
            Float f10 = (Float) vf.getAndBitwiseOr(t, expectedValue, newValue);
            vf.getAndBitwiseOr(t, expectedValue, newValue);
            Float f11 = (Float) vf.getAndBitwiseOrRelease(t, expectedValue, newValue);
            vf.getAndBitwiseOrRelease(t, expectedValue, newValue);
            Float f12 = (Float) vf.getAndBitwiseXorAcquire(t, expectedValue, newValue);
            vf.getAndBitwiseXorAcquire(t, expectedValue, newValue);
            Float f13 = (Float) vf.getAndBitwiseXor(t, expectedValue, newValue);
            vf.getAndBitwiseXor(t, expectedValue, newValue);
            Float f14 = (Float) vf.getAndBitwiseXorRelease(t, expectedValue, newValue);
            vf.getAndBitwiseXorRelease(t, expectedValue, newValue);

            Float f15 = (Float) vf.getAndSetAcquire(t, newValue);
            vf.getAndSetAcquire(t, newValue);
            Float f16 = (Float) vf.getAndSet(t, newValue);
            vf.getAndSet(t, newValue);
            Float f17 = (Float) vf.getAndSetRelease(t, newValue);
            vf.getAndSetRelease(t, newValue);

            Float f18 = (Float) vf.get(t);
            vf.get(t);
            Float f19 = (Float) vf.getAcquire(t);
            vf.getAcquire(t);
            Float f20 = (Float) vf.getOpaque(t);
            vf.getOpaque(t);
            Float f21 = (Float) vf.getVolatile(t);
            vf.getVolatile(t);

            vf.set(t, newValue);
            vf.setOpaque(t, newValue);
            vf.setRelease(t, newValue);
            vf.setVolatile(t, newValue);
        }
        {
            String[] words = { "okay", "stevie", "bring", "your", "three", "friends", "up" };
            VarHandle vw = MethodHandles.arrayElementVarHandle(words.getClass());
            String newValue = "four";
            String expectedValue = "three";
            int index = 4;

            String s0 = (String) vw.compareAndExchangeAcquire(words, index, expectedValue, newValue);
            vw.compareAndExchangeAcquire(words, index, expectedValue, newValue);
            String s1 = (String) vw.compareAndExchange(words, index, expectedValue, newValue);
            vw.compareAndExchange(words, index, expectedValue, newValue);
            String s2 = (String) vw.compareAndExchangeRelease(words, index, expectedValue, newValue);
            vw.compareAndExchangeRelease(words, index, expectedValue, newValue);

            boolean r0 = vw.compareAndSet(words, index, expectedValue, newValue);
            vw.compareAndSet(words, index, expectedValue, newValue);
            boolean r1 = vw.weakCompareAndSetAcquire(words, index, expectedValue, newValue);
            vw.weakCompareAndSetAcquire(words, index, expectedValue, newValue);
            boolean r2 = vw.weakCompareAndSet(words, index, expectedValue, newValue);
            vw.weakCompareAndSet(words, index, expectedValue, newValue);
            boolean r3 = vw.weakCompareAndSetPlain(words, index, expectedValue, newValue);
            vw.weakCompareAndSetPlain(words, index, expectedValue, newValue);
            boolean r4 = vw.weakCompareAndSetRelease(words, index, expectedValue, newValue);
            vw.weakCompareAndSetRelease(words, index, expectedValue, newValue);

            String s3 = (String) vw.getAndAddAcquire(words, index, expectedValue, newValue);
            vw.getAndAddAcquire(words, index, expectedValue, newValue);
            String s4 = (String) vw.getAndAdd(words, index, expectedValue, newValue);
            vw.getAndAdd(words, index, expectedValue, newValue);
            String s5 = (String) vw.getAndAddRelease(words, index, expectedValue, newValue);
            vw.getAndAddRelease(words, index, expectedValue, newValue);
            String s6 = (String) vw.getAndBitwiseAndAcquire(words, index, expectedValue, newValue);
            vw.getAndBitwiseAndAcquire(words, index, expectedValue, newValue);
            String s7 = (String) vw.getAndBitwiseAnd(words, index, expectedValue, newValue);
            vw.getAndBitwiseAnd(words, index, expectedValue, newValue);
            String s8 = (String) vw.getAndBitwiseAndRelease(words, index, expectedValue, newValue);
            vw.getAndBitwiseAndRelease(words, index, expectedValue, newValue);
            String s9 = (String) vw.getAndBitwiseOrAcquire(words, index, expectedValue, newValue);
            vw.getAndBitwiseOrAcquire(words, index, expectedValue, newValue);
            String s10 = (String) vw.getAndBitwiseOr(words, index, expectedValue, newValue);
            vw.getAndBitwiseOr(words, index, expectedValue, newValue);
            String s11 = (String) vw.getAndBitwiseOrRelease(words, index, expectedValue, newValue);
            vw.getAndBitwiseOrRelease(words, index, expectedValue, newValue);
            String s12 = (String) vw.getAndBitwiseXorAcquire(words, index, expectedValue, newValue);
            vw.getAndBitwiseXorAcquire(words, index, expectedValue, newValue);
            String s13 = (String) vw.getAndBitwiseXor(words, index, expectedValue, newValue);
            vw.getAndBitwiseXor(words, index, expectedValue, newValue);
            String s14 = (String) vw.getAndBitwiseXorRelease(words, index, expectedValue, newValue);
            vw.getAndBitwiseXorRelease(words, index, expectedValue, newValue);

            String s15 = (String) vw.getAndSetAcquire(words, index, newValue);
            vw.getAndSetAcquire(words, index, newValue);
            String s16 = (String) vw.getAndSet(words, index, newValue);
            vw.getAndSet(words, index, newValue);
            String s17 = (String) vw.getAndSetRelease(words, index, newValue);
            vw.getAndSetRelease(words, index, newValue);

            String s18 = (String) vw.get(words, index);
            vw.get(words, index);
            String s19 = (String) vw.getAcquire(words, index);
            vw.getAcquire(words, index);
            String s20 = (String) vw.getOpaque(words, index);
            vw.getOpaque(words, index);
            String s21 = (String) vw.getVolatile(words, index);
            vw.getVolatile(words, index);

            vw.set(words, index, newValue);
            vw.setOpaque(words, index, newValue);
            vw.setRelease(words, index, newValue);
            vw.setVolatile(words, index, newValue);
        }
    }
}