1 /*
2  * Copyright (C) 2008 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 public class InstField {
18     public boolean mBoolean1, mBoolean2;
19     public byte mByte1, mByte2;
20     public char mChar1, mChar2;
21     public short mShort1, mShort2;
22     public int mInt1, mInt2;
23     public float mFloat1, mFloat2;
24     public long mLong1, mLong2;
25     public double mDouble1, mDouble2;
26     public volatile long mVolatileLong1, mVolatileLong2;
27 
run()28     public void run() {
29         assignFields();
30         checkFields();
31         InstField.nullCheck(null);
32     }
33 
34     /*
35      * Check access to instance fields through a null pointer.
36      */
nullCheck(InstField nully)37     static public void nullCheck(InstField nully) {
38         System.out.println("InstField.nullCheck");
39         try {
40             int x = nully.mInt1;
41             Main.assertTrue(false);
42         } catch (NullPointerException npe) {
43             // good
44         }
45         try {
46             long l = nully.mLong1;
47             Main.assertTrue(false);
48         } catch (NullPointerException npe) {
49             // good
50         }
51         try {
52             nully.mInt1 = 5;
53             Main.assertTrue(false);
54         } catch (NullPointerException npe) {
55             // good
56         }
57         try {
58             nully.mLong1 = 17L;
59             Main.assertTrue(false);
60         } catch (NullPointerException npe) {
61             // good
62         }
63     }
64 
assignFields()65     public void assignFields() {
66         System.out.println("InstField assign...");
67         mBoolean1 = true;
68         mBoolean2 = false;
69         mByte1 = 127;
70         mByte2 = -128;
71         mChar1 = 32767;
72         mChar2 = 65535;
73         mShort1 = 32767;
74         mShort2 = -32768;
75         mInt1 = 65537;
76         mInt2 = -65537;
77         mFloat1 = 3.1415f;
78         mFloat2 = -1.0f / 0.0f;                // -inf
79         mLong1 = 1234605616436508552L;     // 0x1122334455667788
80         mLong2 = -1234605616436508552L;
81         mDouble1 = 3.1415926535;
82         mDouble2 = 1.0 / 0.0;               // +inf
83         mVolatileLong1 = mLong1 - 1;
84         mVolatileLong2 = mLong2 + 1;
85     }
86 
checkFields()87     public void checkFields() {
88         System.out.println("InstField check...");
89         Main.assertTrue(mBoolean1);
90         Main.assertTrue(!mBoolean2);
91         Main.assertTrue(mByte1 == 127);
92         Main.assertTrue(mByte2 == -128);
93         Main.assertTrue(mChar1 == 32767);
94         Main.assertTrue(mChar2 == 65535);
95         Main.assertTrue(mShort1 == 32767);
96         Main.assertTrue(mShort2 == -32768);
97         Main.assertTrue(mInt1 == 65537);
98         Main.assertTrue(mInt2 == -65537);
99         Main.assertTrue(mFloat1 > 3.141f && mFloat1 < 3.142f);
100         Main.assertTrue(mFloat2 < mFloat1);
101         Main.assertTrue(mLong1 == 1234605616436508552L);
102         Main.assertTrue(mLong2 == -1234605616436508552L);
103         Main.assertTrue(mDouble1 > 3.141592653 && mDouble1 < 3.141592654);
104         Main.assertTrue(mDouble2 > mDouble1);
105         Main.assertTrue(mVolatileLong1 == 1234605616436508551L);
106         Main.assertTrue(mVolatileLong2 == -1234605616436508551L);
107     }
108 }
109