• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.VarHandle;
19 import java.lang.invoke.WrongMethodTypeException;
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22 
23 public class VarHandleTypeConversionTests {
24     public static class VoidReturnTypeTest extends VarHandleUnitTest {
25         private int i;
26         private static final VarHandle vh;
27 
28         static {
29             try {
30                 Class<?> cls = VoidReturnTypeTest.class;
31                 vh = MethodHandles.lookup().findVarHandle(cls, "i", int.class);
32             } catch (Exception e) {
33                 throw new RuntimeException(e);
34             }
35         }
36 
37         @Override
doTest()38         protected void doTest() {
39             // Void is always okay for a return type.
40             vh.setVolatile(this, 33);
41             vh.get(this);
42             vh.getAndSet(this, 200000);
43             vh.getAndBitwiseXor(this, 0x5a5a5a5a);
44             vh.getAndAdd(this, 99);
45 
46             // Return type of these is boolean (JLS9 S15.12), but check anyway.
47             vh.compareAndSet(this, 33, 44);
48             vh.compareAndSet(this, 27, 16);
49             vh.weakCompareAndSet(this, 17, 19);
50         }
51 
main(String[] args)52         public static void main(String[] args) {
53             new VoidReturnTypeTest().run();
54         }
55     }
56 
57     //
58     // Tests that a null reference as a boxed primitive type argument
59     // throws a NullPointerException. These vary the VarHandle type
60     // with each primitive for coverage.
61     //
62 
63     public static class BoxedNullBooleanThrowsNPETest extends VarHandleUnitTest {
64         private static boolean z;
65         private static final VarHandle vh;
66 
67         static {
68             try {
69                 Class<?> cls = BoxedNullBooleanThrowsNPETest.class;
70                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "z", boolean.class);
71             } catch (Exception e) {
72                 throw new RuntimeException(e);
73             }
74         }
75 
76         @Override
doTest()77         protected void doTest() {
78             Boolean newValue = null;
79             try {
80                 vh.getAndSet(newValue);
81                 failUnreachable();
82             } catch (NullPointerException ex) {
83             }
84         }
85 
main(String[] args)86         public static void main(String[] args) {
87             new BoxedNullBooleanThrowsNPETest().run();
88         }
89     }
90 
91     public static class BoxedNullByteThrowsNPETest extends VarHandleUnitTest {
92         private byte b;
93         private static final VarHandle vh;
94 
95         static {
96             try {
97                 Class<?> cls = BoxedNullByteThrowsNPETest.class;
98                 vh = MethodHandles.lookup().findVarHandle(cls, "b", byte.class);
99             } catch (Exception e) {
100                 throw new RuntimeException(e);
101             }
102         }
103 
104         @Override
doTest()105         protected void doTest() {
106             Byte newValue = null;
107             try {
108                 vh.getAndSet(this, newValue);
109                 failUnreachable();
110             } catch (NullPointerException ex) {
111             }
112         }
113 
main(String[] args)114         public static void main(String[] args) {
115             new BoxedNullByteThrowsNPETest().run();
116         }
117     }
118 
119     public static class BoxedNullCharacterThrowsNPETest extends VarHandleUnitTest {
120         private static final VarHandle vh;
121 
122         static {
123             try {
124                 vh = MethodHandles.arrayElementVarHandle(char[].class);
125             } catch (Exception e) {
126                 throw new RuntimeException(e);
127             }
128         }
129 
130         @Override
doTest()131         protected void doTest() {
132             char[] values = new char[3];
133             Character newValue = null;
134             try {
135                 vh.getAndSet(values, 0, newValue);
136                 failUnreachable();
137             } catch (NullPointerException ex) {
138             }
139         }
140 
main(String[] args)141         public static void main(String[] args) {
142             new BoxedNullCharacterThrowsNPETest().run();
143         }
144     }
145 
146     public static class BoxedNullShortThrowsNPETest extends VarHandleUnitTest {
147         private static final VarHandle vh;
148 
149         static {
150             try {
151                 Class<?> cls = BoxedNullShortThrowsNPETest.class;
152                 vh = MethodHandles.byteArrayViewVarHandle(short[].class, ByteOrder.LITTLE_ENDIAN);
153             } catch (Exception e) {
154                 throw new RuntimeException(e);
155             }
156         }
157 
158         @Override
doTest()159         protected void doTest() {
160             byte[] bytes = new byte[2 * Short.SIZE];
161             int index = VarHandleUnitTestHelpers.alignedOffset_short(bytes, 0);
162             Short newValue = null;
163             try {
164                 vh.set(bytes, index, newValue);
165                 failUnreachable();
166             } catch (NullPointerException ex) {
167             }
168         }
169 
main(String[] args)170         public static void main(String[] args) {
171             new BoxedNullShortThrowsNPETest().run();
172         }
173     }
174 
175     public static class BoxedNullIntegerThrowsNPETest extends VarHandleUnitTest {
176         private static final VarHandle vh;
177 
178         static {
179             try {
180                 vh = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.BIG_ENDIAN);
181             } catch (Exception e) {
182                 throw new RuntimeException(e);
183             }
184         }
185 
186         @Override
doTest()187         protected void doTest() {
188             byte[] bytes = new byte[2 * Integer.SIZE];
189             int index = VarHandleUnitTestHelpers.alignedOffset_int(bytes, 0);
190             Integer newValue = null;
191             try {
192                 vh.setVolatile(bytes, index, newValue);
193                 failUnreachable();
194             } catch (NullPointerException ex) {
195             }
196         }
197 
main(String[] args)198         public static void main(String[] args) {
199             new BoxedNullIntegerThrowsNPETest().run();
200         }
201     }
202 
203     public static class BoxedNullLongThrowsNPETest extends VarHandleUnitTest {
204         private static final VarHandle vh;
205 
206         static {
207             try {
208                 Class<?> cls = BoxedNullLongThrowsNPETest.class;
209                 vh = MethodHandles.byteBufferViewVarHandle(long[].class, ByteOrder.LITTLE_ENDIAN);
210             } catch (Exception e) {
211                 throw new RuntimeException(e);
212             }
213         }
214 
215         @Override
doTest()216         protected void doTest() {
217             ByteBuffer bb = ByteBuffer.allocateDirect(2 * Long.SIZE);
218             int index = VarHandleUnitTestHelpers.alignedOffset_long(bb, 0);
219             Long newValue = null;
220             try {
221                 vh.getAndAdd(bb, index, newValue);
222                 failUnreachable();
223             } catch (NullPointerException ex) {
224             }
225         }
226 
main(String[] args)227         public static void main(String[] args) {
228             new BoxedNullLongThrowsNPETest().run();
229         }
230     }
231 
232     public static class BoxedNullFloatThrowsNPETest extends VarHandleUnitTest {
233         private static final VarHandle vh;
234 
235         static {
236             try {
237                 Class<?> cls = BoxedNullFloatThrowsNPETest.class;
238                 vh = MethodHandles.byteBufferViewVarHandle(float[].class, ByteOrder.BIG_ENDIAN);
239             } catch (Exception e) {
240                 throw new RuntimeException(e);
241             }
242         }
243 
244         @Override
doTest()245         protected void doTest() {
246             ByteBuffer bb = ByteBuffer.allocate(2 * Float.SIZE);
247             int index = VarHandleUnitTestHelpers.alignedOffset_float(bb, 0);
248             Float newValue = null;
249             try {
250                 vh.set(bb, index, newValue);
251                 failUnreachable();
252             } catch (NullPointerException ex) {
253             }
254         }
255 
main(String[] args)256         public static void main(String[] args) {
257             new BoxedNullFloatThrowsNPETest().run();
258         }
259     }
260 
261     public static class BoxedNullDoubleThrowsNPETest extends VarHandleUnitTest {
262         private double d;
263         private static final VarHandle vh;
264 
265         static {
266             try {
267                 vh = MethodHandles.byteBufferViewVarHandle(double[].class, ByteOrder.LITTLE_ENDIAN);
268             } catch (Exception e) {
269                 throw new RuntimeException(e);
270             }
271         }
272 
273         @Override
doTest()274         protected void doTest() {
275             byte[] bytes = new byte[3 * Double.SIZE];
276             int offset = 1;
277             ByteBuffer bb = ByteBuffer.wrap(bytes, offset, bytes.length - offset);
278             int index = VarHandleUnitTestHelpers.alignedOffset_double(bb, 0);
279             Double newValue = null;
280             try {
281                 vh.set(bb, index, newValue);
282                 failUnreachable();
283             } catch (NullPointerException ex) {
284             }
285         }
286 
main(String[] args)287         public static void main(String[] args) {
288             new BoxedNullDoubleThrowsNPETest().run();
289         }
290     }
291 
292     public static class WideningBooleanArgumentTest extends VarHandleUnitTest {
293         private static boolean v;
294         private static final VarHandle vh;
295 
296         static {
297             try {
298                 Class<?> cls = WideningBooleanArgumentTest.class;
299                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", boolean.class);
300             } catch (Exception e) {
301                 throw new RuntimeException(e);
302             }
303         }
304 
305         @Override
doTest()306         protected void doTest() {
307             vh.set(true);
308             try {
309                 vh.set((byte) 3);
310                 failUnreachable();
311             } catch (WrongMethodTypeException ex) {
312             }
313             try {
314                 vh.set('c');
315                 failUnreachable();
316             } catch (WrongMethodTypeException ex) {
317             }
318             try {
319                 vh.set((short) 1);
320                 failUnreachable();
321             } catch (WrongMethodTypeException ex) {
322             }
323             try {
324                 vh.set((int) 1);
325                 failUnreachable();
326             } catch (WrongMethodTypeException ex) {
327             }
328             try {
329                 vh.set((long) 1);
330                 failUnreachable();
331             } catch (WrongMethodTypeException ex) {
332             }
333             try {
334                 vh.set((float) 1.0f);
335                 failUnreachable();
336             } catch (WrongMethodTypeException ex) {
337             }
338             try {
339                 vh.set((double) 1.0);
340                 failUnreachable();
341             } catch (WrongMethodTypeException ex) {
342             }
343         }
344 
main(String[] args)345         public static void main(String[] args) {
346             new WideningBooleanArgumentTest().run();
347         }
348     }
349 
350     public static class WideningByteArgumentTest extends VarHandleUnitTest {
351         private static byte v;
352         private static final VarHandle vh;
353 
354         static {
355             try {
356                 Class<?> cls = WideningByteArgumentTest.class;
357                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", byte.class);
358             } catch (Exception e) {
359                 throw new RuntimeException(e);
360             }
361         }
362 
363         @Override
doTest()364         protected void doTest() {
365             try {
366                 vh.set(true);
367                 failUnreachable();
368             } catch (WrongMethodTypeException ex) {
369             }
370             vh.set((byte) 3);
371             try {
372                 vh.set('c');
373                 failUnreachable();
374             } catch (WrongMethodTypeException ex) {
375             }
376             try {
377                 vh.set((short) 1);
378                 failUnreachable();
379             } catch (WrongMethodTypeException ex) {
380             }
381             try {
382                 vh.set((int) 1);
383                 failUnreachable();
384             } catch (WrongMethodTypeException ex) {
385             }
386             try {
387                 vh.set((long) 1);
388                 failUnreachable();
389             } catch (WrongMethodTypeException ex) {
390             }
391             try {
392                 vh.set((float) 1.0f);
393                 failUnreachable();
394             } catch (WrongMethodTypeException ex) {
395             }
396             try {
397                 vh.set((double) 1.0);
398                 failUnreachable();
399             } catch (WrongMethodTypeException ex) {
400             }
401         }
402 
main(String[] args)403         public static void main(String[] args) {
404             new WideningByteArgumentTest().run();
405         }
406     }
407 
408     public static class WideningCharacterArgumentTest extends VarHandleUnitTest {
409         private static char v;
410         private static final VarHandle vh;
411 
412         static {
413             try {
414                 Class<?> cls = WideningCharacterArgumentTest.class;
415                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", char.class);
416             } catch (Exception e) {
417                 throw new RuntimeException(e);
418             }
419         }
420 
421         @Override
doTest()422         protected void doTest() {
423             try {
424                 vh.set(true);
425                 failUnreachable();
426             } catch (WrongMethodTypeException ex) {
427             }
428             try {
429                 vh.set((byte) 3);
430                 failUnreachable();
431             } catch (WrongMethodTypeException ex) {
432             }
433             vh.set('c');
434             try {
435                 vh.set((short) 1);
436                 failUnreachable();
437             } catch (WrongMethodTypeException ex) {
438             }
439             try {
440                 vh.set((int) 1);
441                 failUnreachable();
442             } catch (WrongMethodTypeException ex) {
443             }
444             try {
445                 vh.set((long) 1);
446                 failUnreachable();
447             } catch (WrongMethodTypeException ex) {
448             }
449             try {
450                 vh.set((float) 1.0f);
451                 failUnreachable();
452             } catch (WrongMethodTypeException ex) {
453             }
454             try {
455                 vh.set((double) 1.0);
456                 failUnreachable();
457             } catch (WrongMethodTypeException ex) {
458             }
459         }
460 
main(String[] args)461         public static void main(String[] args) {
462             new WideningCharacterArgumentTest().run();
463         }
464     }
465 
466     public static class WideningShortArgumentTest extends VarHandleUnitTest {
467         private static short v;
468         private static final VarHandle vh;
469 
470         static {
471             try {
472                 Class<?> cls = WideningShortArgumentTest.class;
473                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", short.class);
474             } catch (Exception e) {
475                 throw new RuntimeException(e);
476             }
477         }
478 
479         @Override
doTest()480         protected void doTest() {
481             try {
482                 vh.set(true);
483                 failUnreachable();
484             } catch (WrongMethodTypeException ex) {
485             }
486             vh.set((byte) 3);
487             try {
488                 vh.set('c');
489                 failUnreachable();
490             } catch (WrongMethodTypeException ex) {
491             }
492             vh.set((short) 1);
493             try {
494                 vh.set((int) 1);
495                 failUnreachable();
496             } catch (WrongMethodTypeException ex) {
497             }
498             try {
499                 vh.set((long) 1);
500                 failUnreachable();
501             } catch (WrongMethodTypeException ex) {
502             }
503             try {
504                 vh.set((float) 1.0f);
505                 failUnreachable();
506             } catch (WrongMethodTypeException ex) {
507             }
508             try {
509                 vh.set((double) 1.0);
510                 failUnreachable();
511             } catch (WrongMethodTypeException ex) {
512             }
513         }
514 
main(String[] args)515         public static void main(String[] args) {
516             new WideningShortArgumentTest().run();
517         }
518     }
519 
520     public static class WideningIntegerArgumentTest extends VarHandleUnitTest {
521         private static int v;
522         private static final VarHandle vh;
523 
524         static {
525             try {
526                 Class<?> cls = WideningIntegerArgumentTest.class;
527                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", int.class);
528             } catch (Exception e) {
529                 throw new RuntimeException(e);
530             }
531         }
532 
533         @Override
doTest()534         protected void doTest() {
535             try {
536                 vh.set(true);
537                 failUnreachable();
538             } catch (WrongMethodTypeException ex) {
539             }
540             vh.set((byte) 3);
541             vh.set('c');
542             vh.set((char) 0x8fff);
543             assertEquals(0x8fff, v);
544             vh.set((short) 1);
545             vh.set((int) 1);
546             try {
547                 vh.set((long) 1);
548                 failUnreachable();
549             } catch (WrongMethodTypeException ex) {
550             }
551             try {
552                 vh.set((float) 1.0f);
553                 failUnreachable();
554             } catch (WrongMethodTypeException ex) {
555             }
556             try {
557                 vh.set((double) 1.0);
558                 failUnreachable();
559             } catch (WrongMethodTypeException ex) {
560             }
561         }
562 
main(String[] args)563         public static void main(String[] args) {
564             new WideningIntegerArgumentTest().run();
565         }
566     }
567 
568     public static class WideningLongArgumentTest extends VarHandleUnitTest {
569         private static long v;
570         private static final VarHandle vh;
571 
572         static {
573             try {
574                 Class<?> cls = WideningLongArgumentTest.class;
575                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", long.class);
576             } catch (Exception e) {
577                 throw new RuntimeException(e);
578             }
579         }
580 
581         @Override
doTest()582         protected void doTest() {
583             try {
584                 vh.set(true);
585                 failUnreachable();
586             } catch (WrongMethodTypeException ex) {
587             }
588             vh.set((byte) 3);
589             vh.set('c');
590             vh.set((short) 1);
591             vh.set((int) 1);
592             vh.set((long) 1);
593             try {
594                 vh.set((float) 1.0f);
595                 failUnreachable();
596             } catch (WrongMethodTypeException ex) {
597             }
598             try {
599                 vh.set((double) 1.0);
600                 failUnreachable();
601             } catch (WrongMethodTypeException ex) {
602             }
603         }
604 
main(String[] args)605         public static void main(String[] args) {
606             new WideningLongArgumentTest().run();
607         }
608     }
609 
610     public static class WideningFloatArgumentTest extends VarHandleUnitTest {
611         private static float v;
612         private static final VarHandle vh;
613 
614         static {
615             try {
616                 Class<?> cls = WideningFloatArgumentTest.class;
617                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", float.class);
618             } catch (Exception e) {
619                 throw new RuntimeException(e);
620             }
621         }
622 
623         @Override
doTest()624         protected void doTest() {
625             try {
626                 vh.set(true);
627                 failUnreachable();
628             } catch (WrongMethodTypeException ex) {
629             }
630             vh.set((byte) 3);
631             vh.set('c');
632             vh.set((short) 1);
633             vh.set((int) 1);
634             vh.set((long) 1);
635             vh.set((float) 1.0f);
636             try {
637                 vh.set((double) 1.0);
638                 failUnreachable();
639             } catch (WrongMethodTypeException ex) {
640             }
641         }
642 
main(String[] args)643         public static void main(String[] args) {
644             new WideningFloatArgumentTest().run();
645         }
646     }
647 
648     public static class WideningDoubleArgumentTest extends VarHandleUnitTest {
649         private static double v;
650         private static final VarHandle vh;
651 
652         static {
653             try {
654                 Class<?> cls = WideningDoubleArgumentTest.class;
655                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", double.class);
656             } catch (Exception e) {
657                 throw new RuntimeException(e);
658             }
659         }
660 
661         @Override
doTest()662         protected void doTest() {
663             try {
664                 vh.set(true);
665                 failUnreachable();
666             } catch (WrongMethodTypeException ex) {
667             }
668             vh.set((byte) 3);
669             vh.set('c');
670             vh.set((short) 1);
671             vh.set((int) 1);
672             vh.set((long) 1);
673             vh.set((float) 1.0f);
674             vh.set((double) 1.0);
675         }
676 
main(String[] args)677         public static void main(String[] args) {
678             new WideningDoubleArgumentTest().run();
679         }
680     }
681 
682     public static class WideningBooleanReturnValueTest extends VarHandleUnitTest {
683         private static boolean v;
684         private static final VarHandle vh;
685 
686         static {
687             try {
688                 Class<?> cls = WideningBooleanReturnValueTest.class;
689                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", boolean.class);
690             } catch (Exception e) {
691                 throw new RuntimeException(e);
692             }
693         }
694 
695         @Override
doTest()696         protected void doTest() {
697             vh.set(true);
698             vh.get();
699             boolean z = (boolean) vh.get();
700             try {
701                 byte b = (byte) vh.get();
702                 failUnreachable();
703             } catch (WrongMethodTypeException ex) {
704             }
705             try {
706                 char c = (char) vh.get();
707                 failUnreachable();
708             } catch (WrongMethodTypeException ex) {
709             }
710             try {
711                 short s = (short) vh.get();
712                 failUnreachable();
713             } catch (WrongMethodTypeException ex) {
714             }
715             try {
716                 int i = (int) vh.get();
717                 failUnreachable();
718             } catch (WrongMethodTypeException ex) {
719             }
720             try {
721                 long j = (long) vh.get();
722                 failUnreachable();
723             } catch (WrongMethodTypeException ex) {
724             }
725             try {
726                 float f = (float) vh.get();
727                 failUnreachable();
728             } catch (WrongMethodTypeException ex) {
729             }
730             try {
731                 double d = (double) vh.get();
732                 failUnreachable();
733             } catch (WrongMethodTypeException ex) {
734             }
735         }
736 
main(String[] args)737         public static void main(String[] args) {
738             new WideningBooleanReturnValueTest().run();
739         }
740     }
741 
742     public static class WideningByteReturnValueTest extends VarHandleUnitTest {
743         private static byte v;
744         private static final VarHandle vh;
745 
746         static {
747             try {
748                 Class<?> cls = WideningByteReturnValueTest.class;
749                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", byte.class);
750             } catch (Exception e) {
751                 throw new RuntimeException(e);
752             }
753         }
754 
755         @Override
doTest()756         protected void doTest() {
757             vh.set((byte) 3);
758             vh.get();
759             try {
760                 boolean z = (boolean) vh.get();
761                 failUnreachable();
762             } catch (WrongMethodTypeException ex) {
763             }
764 
765             byte b = (byte) vh.get();
766             try {
767                 char c = (char) vh.get();
768                 failUnreachable();
769             } catch (WrongMethodTypeException ex) {
770             }
771             short s = (short) vh.get();
772             int i = (int) vh.get();
773             long j = (long) vh.get();
774             float f = (float) vh.get();
775             double d = (double) vh.get();
776         }
777 
main(String[] args)778         public static void main(String[] args) {
779             new WideningByteReturnValueTest().run();
780         }
781     }
782 
783     public static class WideningCharacterReturnValueTest extends VarHandleUnitTest {
784         private static char v;
785         private static final VarHandle vh;
786 
787         static {
788             try {
789                 Class<?> cls = WideningCharacterReturnValueTest.class;
790                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", char.class);
791             } catch (Exception e) {
792                 throw new RuntimeException(e);
793             }
794         }
795 
main(String[] args)796         public static void main(String[] args) {
797             new WideningCharacterReturnValueTest().run();
798         }
799 
800         @Override
doTest()801         protected void doTest() {
802             vh.set('c');
803             vh.get();
804             try {
805                 boolean z = (boolean) vh.get();
806                 failUnreachable();
807             } catch (WrongMethodTypeException ex) {
808             }
809             try {
810                 byte b = (byte) vh.get();
811                 failUnreachable();
812             } catch (WrongMethodTypeException ex) {
813             }
814             char c = (char) vh.get();
815             try {
816                 short s = (short) vh.get();
817                 failUnreachable();
818             } catch (WrongMethodTypeException ex) {
819             }
820             int i = (int) vh.get();
821             long j = (long) vh.get();
822             float f = (float) vh.get();
823             double d = (double) vh.get();
824         }
825     }
826 
827     public static class WideningShortReturnValueTest extends VarHandleUnitTest {
828         private static short v;
829         private static final VarHandle vh;
830 
831         static {
832             try {
833                 Class<?> cls = WideningShortReturnValueTest.class;
834                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", short.class);
835             } catch (Exception e) {
836                 throw new RuntimeException(e);
837             }
838         }
839 
main(String[] args)840         public static void main(String[] args) {
841             new WideningShortReturnValueTest().run();
842         }
843 
844         @Override
doTest()845         protected void doTest() {
846             vh.set((short) 8888);
847             vh.get();
848             try {
849                 boolean z = (boolean) vh.get();
850                 failUnreachable();
851             } catch (WrongMethodTypeException ex) {
852             }
853             try {
854                 byte b = (byte) vh.get();
855                 failUnreachable();
856             } catch (WrongMethodTypeException ex) {
857             }
858             try {
859                 char c = (char) vh.get();
860                 failUnreachable();
861             } catch (WrongMethodTypeException ex) {
862             }
863             short s = (short) vh.get();
864             int i = (int) vh.get();
865             long j = (long) vh.get();
866             float f = (float) vh.get();
867             double d = (double) vh.get();
868         }
869     }
870 
871     public static class WideningIntegerReturnValueTest extends VarHandleUnitTest {
872         private static int v;
873         private static final VarHandle vh;
874 
875         static {
876             try {
877                 Class<?> cls = WideningIntegerReturnValueTest.class;
878                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", int.class);
879             } catch (Exception e) {
880                 throw new RuntimeException(e);
881             }
882         }
883 
main(String[] args)884         public static void main(String[] args) {
885             new WideningIntegerReturnValueTest().run();
886         }
887 
888         @Override
doTest()889         protected void doTest() {
890             vh.set(0x1234fedc);
891             vh.get();
892             try {
893                 boolean z = (boolean) vh.get();
894                 failUnreachable();
895             } catch (WrongMethodTypeException ex) {
896             }
897             try {
898                 byte b = (byte) vh.get();
899                 failUnreachable();
900             } catch (WrongMethodTypeException ex) {
901             }
902             try {
903                 char c = (char) vh.get();
904                 failUnreachable();
905             } catch (WrongMethodTypeException ex) {
906             }
907             try {
908                 short s = (short) vh.get();
909                 failUnreachable();
910             } catch (WrongMethodTypeException ex) {
911             }
912             int i = (int) vh.get();
913             long j = (long) vh.get();
914             float f = (float) vh.get();
915             double d = (double) vh.get();
916         }
917     }
918 
919     public static class WideningLongReturnValueTest extends VarHandleUnitTest {
920         private static long v;
921         private static final VarHandle vh;
922 
923         static {
924             try {
925                 Class<?> cls = WideningLongReturnValueTest.class;
926                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", long.class);
927             } catch (Exception e) {
928                 throw new RuntimeException(e);
929             }
930         }
931 
main(String[] args)932         public static void main(String[] args) {
933             new WideningLongReturnValueTest().run();
934         }
935 
936         @Override
doTest()937         protected void doTest() {
938             vh.set(0xfedcba987654321l);
939             vh.get();
940             try {
941                 boolean z = (boolean) vh.get();
942                 failUnreachable();
943             } catch (WrongMethodTypeException ex) {
944             }
945             try {
946                 byte b = (byte) vh.get();
947                 failUnreachable();
948             } catch (WrongMethodTypeException ex) {
949             }
950             try {
951                 char c = (char) vh.get();
952                 failUnreachable();
953             } catch (WrongMethodTypeException ex) {
954             }
955             try {
956                 short s = (short) vh.get();
957                 failUnreachable();
958             } catch (WrongMethodTypeException ex) {
959             }
960             try {
961                 int i = (int) vh.get();
962                 failUnreachable();
963             } catch (WrongMethodTypeException ex) {
964             }
965             long j = (long) vh.get();
966             float f = (float) vh.get();
967             double d = (double) vh.get();
968         }
969     }
970 
971     public static class WideningFloatReturnValueTest extends VarHandleUnitTest {
972         private static float v;
973         private static final VarHandle vh;
974 
975         static {
976             try {
977                 Class<?> cls = WideningFloatReturnValueTest.class;
978                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", float.class);
979             } catch (Exception e) {
980                 throw new RuntimeException(e);
981             }
982         }
983 
main(String[] args)984         public static void main(String[] args) {
985             new WideningFloatReturnValueTest().run();
986         }
987 
988         @Override
doTest()989         protected void doTest() {
990             vh.set(7.77e20f);
991             vh.get();
992             try {
993                 boolean z = (boolean) vh.get();
994                 failUnreachable();
995             } catch (WrongMethodTypeException ex) {
996             }
997             try {
998                 byte b = (byte) vh.get();
999                 failUnreachable();
1000             } catch (WrongMethodTypeException ex) {
1001             }
1002             try {
1003                 char c = (char) vh.get();
1004                 failUnreachable();
1005             } catch (WrongMethodTypeException ex) {
1006             }
1007             try {
1008                 short s = (short) vh.get();
1009                 failUnreachable();
1010             } catch (WrongMethodTypeException ex) {
1011             }
1012             try {
1013                 int i = (int) vh.get();
1014                 failUnreachable();
1015             } catch (WrongMethodTypeException ex) {
1016             }
1017             try {
1018                 long j = (long) vh.get();
1019                 failUnreachable();
1020             } catch (WrongMethodTypeException ex) {
1021             }
1022             float f = (float) vh.get();
1023             double d = (double) vh.get();
1024         }
1025     }
1026 
1027     public static class WideningDoubleReturnValueTest extends VarHandleUnitTest {
1028         private static double v;
1029         private static final VarHandle vh;
1030 
1031         static {
1032             try {
1033                 Class<?> cls = WideningDoubleReturnValueTest.class;
1034                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "v", double.class);
1035             } catch (Exception e) {
1036                 throw new RuntimeException(e);
1037             }
1038         }
1039 
main(String[] args)1040         public static void main(String[] args) {
1041             new WideningDoubleReturnValueTest().run();
1042         }
1043 
1044         @Override
doTest()1045         protected void doTest() {
1046             vh.set(Math.E);
1047             vh.get();
1048             try {
1049                 boolean z = (boolean) vh.get();
1050                 failUnreachable();
1051             } catch (WrongMethodTypeException ex) {
1052             }
1053             try {
1054                 byte b = (byte) vh.get();
1055                 failUnreachable();
1056             } catch (WrongMethodTypeException ex) {
1057             }
1058             try {
1059                 char c = (char) vh.get();
1060                 failUnreachable();
1061             } catch (WrongMethodTypeException ex) {
1062             }
1063             try {
1064                 short s = (short) vh.get();
1065                 failUnreachable();
1066             } catch (WrongMethodTypeException ex) {
1067             }
1068             try {
1069                 int i = (int) vh.get();
1070                 failUnreachable();
1071             } catch (WrongMethodTypeException ex) {
1072             }
1073             try {
1074                 long j = (long) vh.get();
1075                 failUnreachable();
1076             } catch (WrongMethodTypeException ex) {
1077             }
1078             try {
1079                 float f = (float) vh.get();
1080                 failUnreachable();
1081             } catch (WrongMethodTypeException ex) {
1082             }
1083             double d = (double) vh.get();
1084         }
1085     }
1086 
1087     public static class SubtypeTest extends VarHandleUnitTest {
1088         private static final Widget INITIAL_VALUE = Widget.ONE;
1089         private static final VarHandle vh;
1090         private Widget w = INITIAL_VALUE;
1091 
1092         static {
1093             try {
1094                 vh = MethodHandles.lookup().findVarHandle(SubtypeTest.class, "w", Widget.class);
1095             } catch (Exception e) {
1096                 throw new RuntimeException(e);
1097             }
1098         }
1099 
main(String[] args)1100         public static void main(String[] args) {
1101             new SubtypeTest().run();
1102         }
1103 
1104         // A sub-type of the Widget class
1105         public static class WidgetChild extends Widget {
1106             private int weight;
1107 
WidgetChild(int requistionNumber, int weight)1108             public WidgetChild(int requistionNumber, int weight) {
1109                 super(requistionNumber);
1110                 this.weight = weight;
1111             }
1112 
1113             @Override
equals(Object o)1114             public boolean equals(Object o) {
1115                 if (this == o) {
1116                     return true;
1117                 }
1118                 if (o instanceof WidgetChild == false) {
1119                     return false;
1120                 }
1121                 WidgetChild wc = (WidgetChild) o;
1122                 return (requisitionNumber == wc.requisitionNumber && weight == wc.weight);
1123             }
1124 
1125             public static final WidgetChild ONE = new WidgetChild(1, 100);
1126             public static final WidgetChild TWO = new WidgetChild(2, 2000);
1127         }
1128 
1129         @Override
doTest()1130         public void doTest() {
1131             assertEquals(INITIAL_VALUE, vh.getVolatile(this));
1132             vh.setVolatile(this, null);
1133             Widget rw = (Widget) vh.compareAndExchange(this, null, WidgetChild.ONE);
1134             assertEquals(null, rw);
1135             assertEquals(WidgetChild.ONE, this.w);
1136             WidgetChild rwc =
1137                     (WidgetChild)
1138                             vh.compareAndExchangeRelease(this, WidgetChild.ONE, WidgetChild.TWO);
1139             assertEquals(WidgetChild.TWO, w);
1140             rwc = (WidgetChild) vh.compareAndExchangeAcquire(this, WidgetChild.TWO, Widget.ONE);
1141             assertEquals(Widget.ONE, w);
1142             assertEquals(false, (boolean) vh.compareAndSet(this, null, null));
1143             assertEquals(true, vh.compareAndSet(this, Widget.ONE, Widget.TWO));
1144             assertEquals(Widget.TWO, w);
1145             vh.set(this, null);
1146             assertEquals(null, (Widget) vh.get(this));
1147             vh.setRelease(this, WidgetChild.ONE);
1148             assertEquals(WidgetChild.ONE, (WidgetChild) vh.getAcquire(this));
1149             assertEquals(WidgetChild.ONE, w);
1150             vh.setOpaque(this, WidgetChild.TWO);
1151             assertEquals(WidgetChild.TWO, vh.getOpaque(this));
1152             assertEquals(WidgetChild.TWO, w);
1153             vh.setVolatile(this, null);
1154             assertEquals(null, (Widget) vh.getVolatile(this));
1155             assertEquals(null, w);
1156             assertEquals(null, (WidgetChild) vh.getAndSet(this, WidgetChild.ONE));
1157             assertEquals(WidgetChild.ONE, w);
1158             assertEquals(WidgetChild.ONE, (WidgetChild) vh.getAndSetRelease(this, WidgetChild.TWO));
1159             assertEquals(WidgetChild.TWO, (WidgetChild) vh.getAndSetAcquire(this, WidgetChild.ONE));
1160             try {
1161                 WidgetChild result = (WidgetChild) vh.getAndAdd(this, WidgetChild.ONE);
1162                 failUnreachable();
1163             } catch (UnsupportedOperationException e) {
1164             }
1165             try {
1166                 WidgetChild result = (WidgetChild) vh.getAndAddAcquire(this, 1);
1167                 failUnreachable();
1168             } catch (UnsupportedOperationException e) {
1169             }
1170             try {
1171                 WidgetChild result = (WidgetChild) vh.getAndAddRelease(this, 1);
1172                 failUnreachable();
1173             } catch (UnsupportedOperationException e) {
1174             }
1175             try {
1176                 WidgetChild result = (WidgetChild) vh.getAndBitwiseAnd(this, 1);
1177                 failUnreachable();
1178             } catch (UnsupportedOperationException e) {
1179             }
1180             try {
1181                 WidgetChild result = (WidgetChild) vh.getAndBitwiseAndAcquire(this, 1);
1182                 failUnreachable();
1183             } catch (UnsupportedOperationException e) {
1184             }
1185             try {
1186                 WidgetChild result = (WidgetChild) vh.getAndBitwiseAndRelease(this, 1);
1187                 failUnreachable();
1188             } catch (UnsupportedOperationException e) {
1189             }
1190             try {
1191                 WidgetChild result = (WidgetChild) vh.getAndBitwiseOr(this, 1);
1192                 failUnreachable();
1193             } catch (UnsupportedOperationException e) {
1194             }
1195             try {
1196                 WidgetChild result = (WidgetChild) vh.getAndBitwiseOrAcquire(this, 1);
1197                 failUnreachable();
1198             } catch (UnsupportedOperationException e) {
1199             }
1200             try {
1201                 WidgetChild result = (WidgetChild) vh.getAndBitwiseOrRelease(this, 1);
1202                 failUnreachable();
1203             } catch (UnsupportedOperationException e) {
1204             }
1205             try {
1206                 WidgetChild result = (WidgetChild) vh.getAndBitwiseXor(this, 1);
1207                 failUnreachable();
1208             } catch (UnsupportedOperationException e) {
1209             }
1210             try {
1211                 WidgetChild result = (WidgetChild) vh.getAndBitwiseXorAcquire(this, 1);
1212                 failUnreachable();
1213             } catch (UnsupportedOperationException e) {
1214             }
1215             try {
1216                 WidgetChild result = (WidgetChild) vh.getAndBitwiseXorRelease(this, 1);
1217                 failUnreachable();
1218             } catch (UnsupportedOperationException e) {
1219             }
1220             try {
1221                 boolean result = vh.compareAndSet(this, new Object(), Widget.ONE);
1222                 failUnreachable();
1223             } catch (ClassCastException e) {
1224             }
1225         }
1226     }
1227 
1228     public static class SupertypeTest extends VarHandleUnitTest {
1229         private Widget w = null;
1230         private static final VarHandle vh;
1231 
1232         static {
1233             try {
1234                 vh = MethodHandles.lookup().findVarHandle(SupertypeTest.class, "w", Widget.class);
1235             } catch (Exception e) {
1236                 throw new RuntimeException(e);
1237             }
1238         }
1239 
main(String[] args)1240         public static void main(String[] args) {
1241             new SupertypeTest().run();
1242         }
1243 
1244         @Override
doTest()1245         public void doTest() {
1246             assertEquals(null, (Object) vh.get(this));
1247             vh.set(this, Widget.ONE);
1248             assertEquals(Widget.ONE, vh.getVolatile(this));
1249             try {
1250                 vh.setVolatile(this, new Object());
1251             } catch (ClassCastException e) {
1252             }
1253         }
1254     }
1255 
1256     public static class InterfaceTest extends VarHandleUnitTest {
1257         public interface ParentInterface {
getValue()1258             int getValue();
1259         }
1260 
1261         public interface ChildInterface extends ParentInterface {
setValue(int newValue)1262             void setValue(int newValue);
1263         }
1264 
1265         public class A implements ParentInterface {
1266             protected int value = 0;
getValue()1267             public int getValue() { return value; }
1268         }
1269 
1270         public class B extends A implements ChildInterface {
setValue(int newValue)1271             public void setValue(int newValue) { value = newValue; }
1272         }
1273 
1274         private ParentInterface pi;
1275         private A obj;
1276 
1277         private VarHandle vh_pi;
1278         private VarHandle vh_obj;
1279         {
1280             try {
1281                 vh_pi = MethodHandles.lookup().findVarHandle(InterfaceTest.class, "pi",
1282                                                              InterfaceTest.ParentInterface.class);
1283                 vh_obj = MethodHandles.lookup().findVarHandle(InterfaceTest.class, "obj",
1284                                                               InterfaceTest.A.class);
1285             } catch (Exception e) {
1286                 throw new RuntimeException(e);
1287             }
1288         }
1289 
main(String[] args)1290         public static void main(String[] args) {
1291             new InterfaceTest().run();
1292         }
1293 
1294         @Override
doTest()1295         public void doTest() {
1296             // Tests using VarHandle to field of type ParentInterface.
1297             {
1298                 pi = (ParentInterface) new A();
1299                 ParentInterface p = (ParentInterface) vh_pi.get(this);
1300                 Object o = (Object) vh_pi.get(this);
1301                 try {
1302                     ChildInterface c = (ChildInterface) vh_pi.get(this);
1303                     failUnreachable();
1304                 } catch (ClassCastException expected) {}
1305             }
1306             {
1307                 pi = new B();
1308                 ParentInterface p = (ParentInterface) vh_pi.get(this);
1309                 B b = (B) vh_pi.get(this);
1310                 Object o = (Object) vh_pi.get(this);
1311                 ChildInterface c = (ChildInterface) vh_pi.get(this);
1312             }
1313             {
1314                 pi = null;
1315                 ParentInterface p = (ParentInterface) vh_pi.get(this);
1316                 B b = (B) vh_pi.get(this);
1317                 Object o = (Object) vh_pi.get(this);
1318                 ChildInterface c = (ChildInterface) vh_pi.get(this);
1319             }
1320 
1321             // Tests using VarHandle to field of type A.
1322             {
1323                 obj = new A();
1324                 ParentInterface p = (ParentInterface) vh_obj.get(this);
1325                 Object o = (Object) vh_obj.get(this);
1326                 A a = (A) vh_obj.get(this);
1327                 try {
1328                     B b = (B) vh_obj.get(this);
1329                     failUnreachable();
1330                 } catch (ClassCastException e) {}
1331                 try {
1332                     ChildInterface c = (ChildInterface) vh_obj.get(this);
1333                     failUnreachable();
1334                 } catch (ClassCastException e) {}
1335             }
1336             {
1337                 obj = new B();
1338                 ParentInterface p = (ParentInterface) vh_obj.get(this);
1339                 Object o = (Object) vh_obj.get(this);
1340                 A a = (A) vh_obj.get(this);
1341                 B b = (B) vh_obj.get(this);
1342                 ChildInterface c = (ChildInterface) vh_obj.get(this);
1343             }
1344             {
1345                 obj = null;
1346                 ParentInterface p = (ParentInterface) vh_obj.get(this);
1347                 Object o = (Object) vh_obj.get(this);
1348                 A a = (A) vh_obj.get(this);
1349                 B b = (B) vh_obj.get(this);
1350                 ChildInterface c = (ChildInterface) vh_obj.get(this);
1351             }
1352         }
1353     }
1354 
1355     public static class ImplicitBoxingIntegerTest extends VarHandleUnitTest {
1356         private static Integer field;
1357         private static final VarHandle vh;
1358 
1359         static {
1360             try {
1361                 vh =
1362                         MethodHandles.lookup()
1363                                 .findStaticVarHandle(
1364                                         ImplicitBoxingIntegerTest.class, "field", Integer.class);
1365             } catch (Exception e) {
1366                 throw new RuntimeException(e);
1367             }
1368         }
1369 
main(String[] args)1370         public static void main(String[] args) {
1371             new ImplicitBoxingIntegerTest().run();
1372         }
1373 
1374         @Override
doTest()1375         public void doTest() {
1376             try {
1377                 vh.set(true);
1378                 failUnreachable();
1379             } catch (WrongMethodTypeException e) {
1380             }
1381             try {
1382                 vh.set((byte) 0);
1383                 failUnreachable();
1384             } catch (WrongMethodTypeException e) {
1385             }
1386             try {
1387                 vh.set('A');
1388                 failUnreachable();
1389             } catch (WrongMethodTypeException e) {
1390             }
1391             try {
1392                 vh.set((short) 1);
1393                 failUnreachable();
1394             } catch (WrongMethodTypeException e) {
1395             }
1396             vh.set(2);
1397             try {
1398                 vh.setRelease(Long.MAX_VALUE);
1399                 failUnreachable();
1400             } catch (WrongMethodTypeException e) {
1401             }
1402             try {
1403                 vh.setRelease(Float.MAX_VALUE);
1404                 failUnreachable();
1405             } catch (WrongMethodTypeException e) {
1406             }
1407             try {
1408                 vh.setRelease(Double.MAX_VALUE);
1409                 failUnreachable();
1410             } catch (WrongMethodTypeException e) {
1411             }
1412             vh.set(null);
1413             vh.set(Integer.valueOf(Integer.MAX_VALUE));
1414         }
1415     }
1416 
main(String[] args)1417     public static void main(String[] args) {
1418         VoidReturnTypeTest.main(args);
1419 
1420         BoxedNullBooleanThrowsNPETest.main(args);
1421         BoxedNullByteThrowsNPETest.main(args);
1422         BoxedNullCharacterThrowsNPETest.main(args);
1423         BoxedNullShortThrowsNPETest.main(args);
1424         BoxedNullIntegerThrowsNPETest.main(args);
1425         BoxedNullLongThrowsNPETest.main(args);
1426         BoxedNullFloatThrowsNPETest.main(args);
1427         BoxedNullDoubleThrowsNPETest.main(args);
1428 
1429         WideningBooleanArgumentTest.main(args);
1430         WideningByteArgumentTest.main(args);
1431         WideningCharacterArgumentTest.main(args);
1432         WideningShortArgumentTest.main(args);
1433         WideningIntegerArgumentTest.main(args);
1434         WideningLongArgumentTest.main(args);
1435         WideningFloatArgumentTest.main(args);
1436         WideningDoubleArgumentTest.main(args);
1437 
1438         WideningBooleanReturnValueTest.main(args);
1439         WideningByteReturnValueTest.main(args);
1440         WideningCharacterReturnValueTest.main(args);
1441         WideningShortReturnValueTest.main(args);
1442         WideningIntegerReturnValueTest.main(args);
1443         WideningLongReturnValueTest.main(args);
1444         WideningFloatReturnValueTest.main(args);
1445         WideningDoubleReturnValueTest.main(args);
1446 
1447         SubtypeTest.main(args);
1448         SupertypeTest.main(args);
1449         InterfaceTest.main(args);
1450 
1451         ImplicitBoxingIntegerTest.main(args);
1452     }
1453 }
1454