1 /*
2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 import java.lang.reflect.Field;
26 import sun.hotspot.WhiteBox;
27 
28 //
29 // Test primitive type class mirror objects are cached when open archive heap
30 // objects are mapped.
31 //
32 public class PrimitiveTypesApp {
main(String[] args)33     public static void main(String[] args) {
34         WhiteBox wb = WhiteBox.getWhiteBox();
35         if (!wb.areOpenArchiveHeapObjectsMapped()) {
36             System.out.println("Archived open_archive_heap objects are not mapped.");
37             System.out.println("This may happen during normal operation. Test Skipped.");
38             return;
39         }
40 
41         FieldsTest ft = new FieldsTest();
42         ft.testBoolean(wb);
43         ft.testByte(wb);
44         ft.testChar(wb);
45         ft.testInt(wb);
46         ft.testShort(wb);
47         ft.testLong(wb);
48         ft.testFloat(wb);
49         ft.testDouble(wb);
50     }
51 }
52 
53 class FieldsTest {
54     public boolean f_boolean;
55     public byte f_byte;
56     public char f_char;
57     public int f_int;
58     public short f_short;
59     public long f_long;
60     public float f_float;
61     public double f_double;
62 
FieldsTest()63     FieldsTest() {
64         f_byte = 1;
65         f_boolean = false;
66         f_char = 'a';
67         f_int = 1;
68         f_short = 100;
69         f_long = 2018L;
70         f_float = 1.0f;
71         f_double = 2.5;
72     }
73 
testBoolean(WhiteBox wb)74     void testBoolean(WhiteBox wb) {
75         try {
76             Field f = this.getClass().getDeclaredField("f_boolean");
77             f.setBoolean(this, true);
78             if (!f_boolean) {
79                 throw new RuntimeException("FAILED. Field f_boolean has unexpected value: " + f_boolean);
80             }
81             checkPrimitiveType(wb, f, Boolean.TYPE);
82         } catch (NoSuchFieldException nsfe) {
83             throw new RuntimeException(nsfe);
84         } catch (IllegalAccessException iae) {
85             throw new RuntimeException(iae);
86         }
87     }
88 
testByte(WhiteBox wb)89     void testByte(WhiteBox wb) {
90         try {
91             Field f = this.getClass().getDeclaredField("f_byte");
92             f.setByte(this, (byte)9);
93             if (f_byte != (byte)9) {
94                 throw new RuntimeException("FAILED. Field f_byte has unexpected value: " + f_byte);
95             }
96             checkPrimitiveType(wb, f, Byte.TYPE);
97         } catch (NoSuchFieldException nsfe) {
98             throw new RuntimeException(nsfe);
99         } catch (IllegalAccessException iae) {
100             throw new RuntimeException(iae);
101         }
102     }
103 
testChar(WhiteBox wb)104     void testChar(WhiteBox wb) {
105         try {
106             Field f = this.getClass().getDeclaredField("f_char");
107             f.setChar(this, 'b');
108             if (f_char != 'b') {
109                 throw new RuntimeException("FAILED. Field f_char has unexpected value: " + f_char);
110             }
111             checkPrimitiveType(wb, f, Character.TYPE);
112         } catch (NoSuchFieldException nsfe) {
113             throw new RuntimeException(nsfe);
114         } catch (IllegalAccessException iae) {
115             throw new RuntimeException(iae);
116         }
117     }
118 
testInt(WhiteBox wb)119     void testInt(WhiteBox wb) {
120         try {
121             Field f = this.getClass().getDeclaredField("f_int");
122             f.setInt(this, 9999);
123             if (f_int != 9999) {
124                 throw new RuntimeException("FAILED. Field f_int has unexpected value: " + f_int);
125             }
126             checkPrimitiveType(wb, f, Integer.TYPE);
127         } catch (NoSuchFieldException nsfe) {
128             throw new RuntimeException(nsfe);
129         } catch (IllegalAccessException iae) {
130             throw new RuntimeException(iae);
131         }
132     }
133 
testShort(WhiteBox wb)134     void testShort(WhiteBox wb) {
135         try {
136             Field f = this.getClass().getDeclaredField("f_short");
137             f.setShort(this, (short)99);
138             if (f_short != 99) {
139                 throw new RuntimeException("FAILED. Field f_short has unexpected value: " + f_short);
140             }
141             checkPrimitiveType(wb, f, Short.TYPE);
142         } catch (NoSuchFieldException nsfe) {
143             throw new RuntimeException(nsfe);
144         } catch (IllegalAccessException iae) {
145             throw new RuntimeException(iae);
146         }
147     }
148 
testLong(WhiteBox wb)149     void testLong(WhiteBox wb) {
150         try {
151             Field f = this.getClass().getDeclaredField("f_long");
152             f.setLong(this, 99L);
153             if (f_long != 99L) {
154                 throw new RuntimeException("FAILED. Field f_long has unexpected value: " + f_long);
155             }
156             checkPrimitiveType(wb, f, Long.TYPE);
157         } catch (NoSuchFieldException nsfe) {
158             throw new RuntimeException(nsfe);
159         } catch (IllegalAccessException iae) {
160             throw new RuntimeException(iae);
161         }
162     }
163 
testFloat(WhiteBox wb)164     void testFloat(WhiteBox wb) {
165         try {
166             Field f = this.getClass().getDeclaredField("f_float");
167             f.setFloat(this, 9.9f);
168             if (f_float != 9.9f) {
169                 throw new RuntimeException("FAILED. Field f_float has unexpected value: " + f_float);
170             }
171             checkPrimitiveType(wb, f, Float.TYPE);
172         } catch (NoSuchFieldException nsfe) {
173             throw new RuntimeException(nsfe);
174         } catch (IllegalAccessException iae) {
175             throw new RuntimeException(iae);
176         }
177     }
178 
testDouble(WhiteBox wb)179     void testDouble(WhiteBox wb) {
180         try {
181             Field f = this.getClass().getDeclaredField("f_double");
182             f.setDouble(this, 9.9);
183             if (f_double != 9.9) {
184                 throw new RuntimeException("FAILED. Field f_double has unexpected value: " + f_double);
185             }
186             checkPrimitiveType(wb, f, Double.TYPE);
187         } catch (NoSuchFieldException nsfe) {
188             throw new RuntimeException(nsfe);
189         } catch (IllegalAccessException iae) {
190             throw new RuntimeException(iae);
191         }
192     }
193 
checkPrimitiveType(WhiteBox wb, Field f, Class t)194     void checkPrimitiveType(WhiteBox wb, Field f, Class t) {
195         Class c = f.getType();
196         if (!(c.isPrimitive() && c == t)) {
197             throw new RuntimeException("FAILED. " + c + " is not primitive type " + t);
198         }
199         if (wb.isShared(c)) {
200             System.out.println(c + " is cached, expected");
201         } else {
202             throw new RuntimeException("FAILED. " + c + " is not cached.");
203         }
204     }
205 }
206