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