xref: /aosp_15_r20/dalvik/dx/tests/141-invoke-polymorphic-varhandles/VarHandleDexTest.java (revision 055d459012065f78d96b68be8421640240ddf631)
1*055d4590SKeyi Gui /*
2*055d4590SKeyi Gui  * Copyright (C) 2017 The Android Open Source Project
3*055d4590SKeyi Gui  *
4*055d4590SKeyi Gui  * Licensed under the Apache License, Version 2.0 (the "License");
5*055d4590SKeyi Gui  * you may not use this file except in compliance with the License.
6*055d4590SKeyi Gui  * You may obtain a copy of the License at
7*055d4590SKeyi Gui  *
8*055d4590SKeyi Gui  *      http://www.apache.org/licenses/LICENSE-2.0
9*055d4590SKeyi Gui  *
10*055d4590SKeyi Gui  * Unless required by applicable law or agreed to in writing, software
11*055d4590SKeyi Gui  * distributed under the License is distributed on an "AS IS" BASIS,
12*055d4590SKeyi Gui  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*055d4590SKeyi Gui  * See the License for the specific language governing permissions and
14*055d4590SKeyi Gui  * limitations under the License.
15*055d4590SKeyi Gui  */
16*055d4590SKeyi Gui 
17*055d4590SKeyi Gui import java.lang.invoke.MethodHandles;
18*055d4590SKeyi Gui import java.lang.invoke.MethodHandles.Lookup;
19*055d4590SKeyi Gui import java.lang.invoke.VarHandle;
20*055d4590SKeyi Gui 
21*055d4590SKeyi Gui public class VarHandleDexTest {
22*055d4590SKeyi Gui 
23*055d4590SKeyi Gui     // A static field to access.
24*055d4590SKeyi Gui     private static boolean bsValue = false;
25*055d4590SKeyi Gui 
26*055d4590SKeyi Gui     // An instance field to access.
27*055d4590SKeyi Gui     private Float fValue = Float.valueOf(99.9f);
28*055d4590SKeyi Gui 
main(String[] args)29*055d4590SKeyi Gui     public static void main(String[] args) throws Throwable {
30*055d4590SKeyi Gui         // This code is entirely nonsense. It is just to exercise
31*055d4590SKeyi Gui         // signature polymorphic methods in dx.
32*055d4590SKeyi Gui         VarHandleDexTest t = new VarHandleDexTest();
33*055d4590SKeyi Gui         {
34*055d4590SKeyi Gui             VarHandle vb = MethodHandles.lookup().findStaticVarHandle(t.getClass(), "bsValue", boolean.class);
35*055d4590SKeyi Gui             boolean newValue = true;
36*055d4590SKeyi Gui             boolean expectedValue = false;
37*055d4590SKeyi Gui 
38*055d4590SKeyi Gui             boolean b0 = (boolean) vb.compareAndExchangeAcquire(t, expectedValue, newValue);
39*055d4590SKeyi Gui             vb.compareAndExchangeAcquire(t, expectedValue, newValue);
40*055d4590SKeyi Gui             boolean b1 = (boolean) vb.compareAndExchange(t, expectedValue, newValue);
41*055d4590SKeyi Gui             vb.compareAndExchange(t, expectedValue, newValue);
42*055d4590SKeyi Gui             boolean b2 = (boolean) vb.compareAndExchangeRelease(t, expectedValue, newValue);
43*055d4590SKeyi Gui             vb.compareAndExchangeRelease(t, expectedValue, newValue);
44*055d4590SKeyi Gui 
45*055d4590SKeyi Gui             boolean r0 = vb.compareAndSet(t, expectedValue, newValue);
46*055d4590SKeyi Gui             vb.compareAndSet(t, expectedValue, newValue);
47*055d4590SKeyi Gui             boolean r1 = vb.weakCompareAndSetAcquire(t, expectedValue, newValue);
48*055d4590SKeyi Gui             vb.weakCompareAndSetAcquire(t, expectedValue, newValue);
49*055d4590SKeyi Gui             boolean r2 = vb.weakCompareAndSet(t, expectedValue, newValue);
50*055d4590SKeyi Gui             vb.weakCompareAndSet(t, expectedValue, newValue);
51*055d4590SKeyi Gui             boolean r3 = vb.weakCompareAndSetPlain(t, expectedValue, newValue);
52*055d4590SKeyi Gui             vb.weakCompareAndSetPlain(t, expectedValue, newValue);
53*055d4590SKeyi Gui             boolean r4 = vb.weakCompareAndSetRelease(t, expectedValue, newValue);
54*055d4590SKeyi Gui             vb.weakCompareAndSetRelease(t, expectedValue, newValue);
55*055d4590SKeyi Gui 
56*055d4590SKeyi Gui             boolean b3 = (boolean) vb.getAndAddAcquire(t, expectedValue, newValue);
57*055d4590SKeyi Gui             vb.getAndAddAcquire(t, expectedValue, newValue);
58*055d4590SKeyi Gui             boolean b4 = (boolean) vb.getAndAdd(t, expectedValue, newValue);
59*055d4590SKeyi Gui             vb.getAndAdd(t, expectedValue, newValue);
60*055d4590SKeyi Gui             boolean b5 = (boolean) vb.getAndAddRelease(t, expectedValue, newValue);
61*055d4590SKeyi Gui             vb.getAndAddRelease(t, expectedValue, newValue);
62*055d4590SKeyi Gui             boolean b6 = (boolean) vb.getAndBitwiseAndAcquire(t, expectedValue, newValue);
63*055d4590SKeyi Gui             vb.getAndBitwiseAndAcquire(t, expectedValue, newValue);
64*055d4590SKeyi Gui             boolean b7 = (boolean) vb.getAndBitwiseAnd(t, expectedValue, newValue);
65*055d4590SKeyi Gui             vb.getAndBitwiseAnd(t, expectedValue, newValue);
66*055d4590SKeyi Gui             boolean b8 = (boolean) vb.getAndBitwiseAndRelease(t, expectedValue, newValue);
67*055d4590SKeyi Gui             vb.getAndBitwiseAndRelease(t, expectedValue, newValue);
68*055d4590SKeyi Gui             boolean b9 = (boolean) vb.getAndBitwiseOrAcquire(t, expectedValue, newValue);
69*055d4590SKeyi Gui             vb.getAndBitwiseOrAcquire(t, expectedValue, newValue);
70*055d4590SKeyi Gui             boolean b10 = (boolean) vb.getAndBitwiseOr(t, expectedValue, newValue);
71*055d4590SKeyi Gui             vb.getAndBitwiseOr(t, expectedValue, newValue);
72*055d4590SKeyi Gui             boolean b11 = (boolean) vb.getAndBitwiseOrRelease(t, expectedValue, newValue);
73*055d4590SKeyi Gui             vb.getAndBitwiseOrRelease(t, expectedValue, newValue);
74*055d4590SKeyi Gui             boolean b12 = (boolean) vb.getAndBitwiseXorAcquire(t, expectedValue, newValue);
75*055d4590SKeyi Gui             vb.getAndBitwiseXorAcquire(t, expectedValue, newValue);
76*055d4590SKeyi Gui             boolean b13 = (boolean) vb.getAndBitwiseXor(t, expectedValue, newValue);
77*055d4590SKeyi Gui             vb.getAndBitwiseXor(t, expectedValue, newValue);
78*055d4590SKeyi Gui             boolean b14 = (boolean) vb.getAndBitwiseXorRelease(t, expectedValue, newValue);
79*055d4590SKeyi Gui             vb.getAndBitwiseXorRelease(t, expectedValue, newValue);
80*055d4590SKeyi Gui 
81*055d4590SKeyi Gui             boolean b15 = (boolean) vb.getAndSetAcquire(t, newValue);
82*055d4590SKeyi Gui             vb.getAndSetAcquire(t, newValue);
83*055d4590SKeyi Gui             boolean b16 = (boolean) vb.getAndSet(t, newValue);
84*055d4590SKeyi Gui             vb.getAndSet(t, newValue);
85*055d4590SKeyi Gui             boolean b17 = (boolean) vb.getAndSetRelease(t, newValue);
86*055d4590SKeyi Gui             vb.getAndSetRelease(t, newValue);
87*055d4590SKeyi Gui 
88*055d4590SKeyi Gui             boolean b18 = (boolean) vb.get(t);
89*055d4590SKeyi Gui             vb.get(t);
90*055d4590SKeyi Gui             boolean b19 = (boolean) vb.getAcquire(t);
91*055d4590SKeyi Gui             vb.getAcquire(t);
92*055d4590SKeyi Gui             boolean b20 = (boolean) vb.getOpaque(t);
93*055d4590SKeyi Gui             vb.getOpaque(t);
94*055d4590SKeyi Gui             boolean b21 = (boolean) vb.getVolatile(t);
95*055d4590SKeyi Gui             vb.getVolatile(t);
96*055d4590SKeyi Gui 
97*055d4590SKeyi Gui             vb.set(t, newValue);
98*055d4590SKeyi Gui             vb.setOpaque(t, newValue);
99*055d4590SKeyi Gui             vb.setRelease(t, newValue);
100*055d4590SKeyi Gui             vb.setVolatile(t, newValue);
101*055d4590SKeyi Gui         }
102*055d4590SKeyi Gui         {
103*055d4590SKeyi Gui             VarHandle vf = MethodHandles.lookup().findStaticVarHandle(t.getClass(), "fValue", Float.class);
104*055d4590SKeyi Gui             Float newValue = Float.valueOf(1.1f);
105*055d4590SKeyi Gui             Float expectedValue = Float.valueOf(2.2e-6f);
106*055d4590SKeyi Gui 
107*055d4590SKeyi Gui             Float f0 = (Float) vf.compareAndExchangeAcquire(t, expectedValue, newValue);
108*055d4590SKeyi Gui             vf.compareAndExchangeAcquire(t, expectedValue, newValue);
109*055d4590SKeyi Gui             Float f1 = (Float) vf.compareAndExchange(t, expectedValue, newValue);
110*055d4590SKeyi Gui             vf.compareAndExchange(t, expectedValue, newValue);
111*055d4590SKeyi Gui             Float f2 = (Float) vf.compareAndExchangeRelease(t, expectedValue, newValue);
112*055d4590SKeyi Gui             vf.compareAndExchangeRelease(t, expectedValue, newValue);
113*055d4590SKeyi Gui 
114*055d4590SKeyi Gui             boolean r0 = vf.compareAndSet(t, expectedValue, newValue);
115*055d4590SKeyi Gui             vf.compareAndSet(t, expectedValue, newValue);
116*055d4590SKeyi Gui             boolean r1 = vf.weakCompareAndSetAcquire(t, expectedValue, newValue);
117*055d4590SKeyi Gui             vf.weakCompareAndSetAcquire(t, expectedValue, newValue);
118*055d4590SKeyi Gui             boolean r2 = vf.weakCompareAndSet(t, expectedValue, newValue);
119*055d4590SKeyi Gui             vf.weakCompareAndSet(t, expectedValue, newValue);
120*055d4590SKeyi Gui             boolean r3 = vf.weakCompareAndSetPlain(t, expectedValue, newValue);
121*055d4590SKeyi Gui             vf.weakCompareAndSetPlain(t, expectedValue, newValue);
122*055d4590SKeyi Gui             boolean r4 = vf.weakCompareAndSetRelease(t, expectedValue, newValue);
123*055d4590SKeyi Gui             vf.weakCompareAndSetRelease(t, expectedValue, newValue);
124*055d4590SKeyi Gui 
125*055d4590SKeyi Gui             Float f3 = (Float) vf.getAndAddAcquire(t, expectedValue, newValue);
126*055d4590SKeyi Gui             vf.getAndAddAcquire(t, expectedValue, newValue);
127*055d4590SKeyi Gui             Float f4 = (Float) vf.getAndAdd(t, expectedValue, newValue);
128*055d4590SKeyi Gui             vf.getAndAdd(t, expectedValue, newValue);
129*055d4590SKeyi Gui             Float f5 = (Float) vf.getAndAddRelease(t, expectedValue, newValue);
130*055d4590SKeyi Gui             vf.getAndAddRelease(t, expectedValue, newValue);
131*055d4590SKeyi Gui             Float f6 = (Float) vf.getAndBitwiseAndAcquire(t, expectedValue, newValue);
132*055d4590SKeyi Gui             vf.getAndBitwiseAndAcquire(t, expectedValue, newValue);
133*055d4590SKeyi Gui             Float f7 = (Float) vf.getAndBitwiseAnd(t, expectedValue, newValue);
134*055d4590SKeyi Gui             vf.getAndBitwiseAnd(t, expectedValue, newValue);
135*055d4590SKeyi Gui             Float f8 = (Float) vf.getAndBitwiseAndRelease(t, expectedValue, newValue);
136*055d4590SKeyi Gui             vf.getAndBitwiseAndRelease(t, expectedValue, newValue);
137*055d4590SKeyi Gui             Float f9 = (Float) vf.getAndBitwiseOrAcquire(t, expectedValue, newValue);
138*055d4590SKeyi Gui             vf.getAndBitwiseOrAcquire(t, expectedValue, newValue);
139*055d4590SKeyi Gui             Float f10 = (Float) vf.getAndBitwiseOr(t, expectedValue, newValue);
140*055d4590SKeyi Gui             vf.getAndBitwiseOr(t, expectedValue, newValue);
141*055d4590SKeyi Gui             Float f11 = (Float) vf.getAndBitwiseOrRelease(t, expectedValue, newValue);
142*055d4590SKeyi Gui             vf.getAndBitwiseOrRelease(t, expectedValue, newValue);
143*055d4590SKeyi Gui             Float f12 = (Float) vf.getAndBitwiseXorAcquire(t, expectedValue, newValue);
144*055d4590SKeyi Gui             vf.getAndBitwiseXorAcquire(t, expectedValue, newValue);
145*055d4590SKeyi Gui             Float f13 = (Float) vf.getAndBitwiseXor(t, expectedValue, newValue);
146*055d4590SKeyi Gui             vf.getAndBitwiseXor(t, expectedValue, newValue);
147*055d4590SKeyi Gui             Float f14 = (Float) vf.getAndBitwiseXorRelease(t, expectedValue, newValue);
148*055d4590SKeyi Gui             vf.getAndBitwiseXorRelease(t, expectedValue, newValue);
149*055d4590SKeyi Gui 
150*055d4590SKeyi Gui             Float f15 = (Float) vf.getAndSetAcquire(t, newValue);
151*055d4590SKeyi Gui             vf.getAndSetAcquire(t, newValue);
152*055d4590SKeyi Gui             Float f16 = (Float) vf.getAndSet(t, newValue);
153*055d4590SKeyi Gui             vf.getAndSet(t, newValue);
154*055d4590SKeyi Gui             Float f17 = (Float) vf.getAndSetRelease(t, newValue);
155*055d4590SKeyi Gui             vf.getAndSetRelease(t, newValue);
156*055d4590SKeyi Gui 
157*055d4590SKeyi Gui             Float f18 = (Float) vf.get(t);
158*055d4590SKeyi Gui             vf.get(t);
159*055d4590SKeyi Gui             Float f19 = (Float) vf.getAcquire(t);
160*055d4590SKeyi Gui             vf.getAcquire(t);
161*055d4590SKeyi Gui             Float f20 = (Float) vf.getOpaque(t);
162*055d4590SKeyi Gui             vf.getOpaque(t);
163*055d4590SKeyi Gui             Float f21 = (Float) vf.getVolatile(t);
164*055d4590SKeyi Gui             vf.getVolatile(t);
165*055d4590SKeyi Gui 
166*055d4590SKeyi Gui             vf.set(t, newValue);
167*055d4590SKeyi Gui             vf.setOpaque(t, newValue);
168*055d4590SKeyi Gui             vf.setRelease(t, newValue);
169*055d4590SKeyi Gui             vf.setVolatile(t, newValue);
170*055d4590SKeyi Gui         }
171*055d4590SKeyi Gui         {
172*055d4590SKeyi Gui             String[] words = { "okay", "stevie", "bring", "your", "three", "friends", "up" };
173*055d4590SKeyi Gui             VarHandle vw = MethodHandles.arrayElementVarHandle(words.getClass());
174*055d4590SKeyi Gui             String newValue = "four";
175*055d4590SKeyi Gui             String expectedValue = "three";
176*055d4590SKeyi Gui             int index = 4;
177*055d4590SKeyi Gui 
178*055d4590SKeyi Gui             String s0 = (String) vw.compareAndExchangeAcquire(words, index, expectedValue, newValue);
179*055d4590SKeyi Gui             vw.compareAndExchangeAcquire(words, index, expectedValue, newValue);
180*055d4590SKeyi Gui             String s1 = (String) vw.compareAndExchange(words, index, expectedValue, newValue);
181*055d4590SKeyi Gui             vw.compareAndExchange(words, index, expectedValue, newValue);
182*055d4590SKeyi Gui             String s2 = (String) vw.compareAndExchangeRelease(words, index, expectedValue, newValue);
183*055d4590SKeyi Gui             vw.compareAndExchangeRelease(words, index, expectedValue, newValue);
184*055d4590SKeyi Gui 
185*055d4590SKeyi Gui             boolean r0 = vw.compareAndSet(words, index, expectedValue, newValue);
186*055d4590SKeyi Gui             vw.compareAndSet(words, index, expectedValue, newValue);
187*055d4590SKeyi Gui             boolean r1 = vw.weakCompareAndSetAcquire(words, index, expectedValue, newValue);
188*055d4590SKeyi Gui             vw.weakCompareAndSetAcquire(words, index, expectedValue, newValue);
189*055d4590SKeyi Gui             boolean r2 = vw.weakCompareAndSet(words, index, expectedValue, newValue);
190*055d4590SKeyi Gui             vw.weakCompareAndSet(words, index, expectedValue, newValue);
191*055d4590SKeyi Gui             boolean r3 = vw.weakCompareAndSetPlain(words, index, expectedValue, newValue);
192*055d4590SKeyi Gui             vw.weakCompareAndSetPlain(words, index, expectedValue, newValue);
193*055d4590SKeyi Gui             boolean r4 = vw.weakCompareAndSetRelease(words, index, expectedValue, newValue);
194*055d4590SKeyi Gui             vw.weakCompareAndSetRelease(words, index, expectedValue, newValue);
195*055d4590SKeyi Gui 
196*055d4590SKeyi Gui             String s3 = (String) vw.getAndAddAcquire(words, index, expectedValue, newValue);
197*055d4590SKeyi Gui             vw.getAndAddAcquire(words, index, expectedValue, newValue);
198*055d4590SKeyi Gui             String s4 = (String) vw.getAndAdd(words, index, expectedValue, newValue);
199*055d4590SKeyi Gui             vw.getAndAdd(words, index, expectedValue, newValue);
200*055d4590SKeyi Gui             String s5 = (String) vw.getAndAddRelease(words, index, expectedValue, newValue);
201*055d4590SKeyi Gui             vw.getAndAddRelease(words, index, expectedValue, newValue);
202*055d4590SKeyi Gui             String s6 = (String) vw.getAndBitwiseAndAcquire(words, index, expectedValue, newValue);
203*055d4590SKeyi Gui             vw.getAndBitwiseAndAcquire(words, index, expectedValue, newValue);
204*055d4590SKeyi Gui             String s7 = (String) vw.getAndBitwiseAnd(words, index, expectedValue, newValue);
205*055d4590SKeyi Gui             vw.getAndBitwiseAnd(words, index, expectedValue, newValue);
206*055d4590SKeyi Gui             String s8 = (String) vw.getAndBitwiseAndRelease(words, index, expectedValue, newValue);
207*055d4590SKeyi Gui             vw.getAndBitwiseAndRelease(words, index, expectedValue, newValue);
208*055d4590SKeyi Gui             String s9 = (String) vw.getAndBitwiseOrAcquire(words, index, expectedValue, newValue);
209*055d4590SKeyi Gui             vw.getAndBitwiseOrAcquire(words, index, expectedValue, newValue);
210*055d4590SKeyi Gui             String s10 = (String) vw.getAndBitwiseOr(words, index, expectedValue, newValue);
211*055d4590SKeyi Gui             vw.getAndBitwiseOr(words, index, expectedValue, newValue);
212*055d4590SKeyi Gui             String s11 = (String) vw.getAndBitwiseOrRelease(words, index, expectedValue, newValue);
213*055d4590SKeyi Gui             vw.getAndBitwiseOrRelease(words, index, expectedValue, newValue);
214*055d4590SKeyi Gui             String s12 = (String) vw.getAndBitwiseXorAcquire(words, index, expectedValue, newValue);
215*055d4590SKeyi Gui             vw.getAndBitwiseXorAcquire(words, index, expectedValue, newValue);
216*055d4590SKeyi Gui             String s13 = (String) vw.getAndBitwiseXor(words, index, expectedValue, newValue);
217*055d4590SKeyi Gui             vw.getAndBitwiseXor(words, index, expectedValue, newValue);
218*055d4590SKeyi Gui             String s14 = (String) vw.getAndBitwiseXorRelease(words, index, expectedValue, newValue);
219*055d4590SKeyi Gui             vw.getAndBitwiseXorRelease(words, index, expectedValue, newValue);
220*055d4590SKeyi Gui 
221*055d4590SKeyi Gui             String s15 = (String) vw.getAndSetAcquire(words, index, newValue);
222*055d4590SKeyi Gui             vw.getAndSetAcquire(words, index, newValue);
223*055d4590SKeyi Gui             String s16 = (String) vw.getAndSet(words, index, newValue);
224*055d4590SKeyi Gui             vw.getAndSet(words, index, newValue);
225*055d4590SKeyi Gui             String s17 = (String) vw.getAndSetRelease(words, index, newValue);
226*055d4590SKeyi Gui             vw.getAndSetRelease(words, index, newValue);
227*055d4590SKeyi Gui 
228*055d4590SKeyi Gui             String s18 = (String) vw.get(words, index);
229*055d4590SKeyi Gui             vw.get(words, index);
230*055d4590SKeyi Gui             String s19 = (String) vw.getAcquire(words, index);
231*055d4590SKeyi Gui             vw.getAcquire(words, index);
232*055d4590SKeyi Gui             String s20 = (String) vw.getOpaque(words, index);
233*055d4590SKeyi Gui             vw.getOpaque(words, index);
234*055d4590SKeyi Gui             String s21 = (String) vw.getVolatile(words, index);
235*055d4590SKeyi Gui             vw.getVolatile(words, index);
236*055d4590SKeyi Gui 
237*055d4590SKeyi Gui             vw.set(words, index, newValue);
238*055d4590SKeyi Gui             vw.setOpaque(words, index, newValue);
239*055d4590SKeyi Gui             vw.setRelease(words, index, newValue);
240*055d4590SKeyi Gui             vw.setVolatile(words, index, newValue);
241*055d4590SKeyi Gui         }
242*055d4590SKeyi Gui     }
243*055d4590SKeyi Gui }
244