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
17public 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
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     */
37    static public void nullCheck(InstField nully) {
38        System.out.println("InstField.nullCheck");
39        try {
40            int x = nully.mInt1;
41            assert(false);
42        } catch (NullPointerException npe) {
43            // good
44        }
45        try {
46            long l = nully.mLong1;
47            assert(false);
48        } catch (NullPointerException npe) {
49            // good
50        }
51        try {
52            nully.mInt1 = 5;
53            assert(false);
54        } catch (NullPointerException npe) {
55            // good
56        }
57        try {
58            nully.mLong1 = 17L;
59            assert(false);
60        } catch (NullPointerException npe) {
61            // good
62        }
63    }
64
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
87    public void checkFields() {
88        System.out.println("InstField check...");
89        assert(mBoolean1);
90        assert(!mBoolean2);
91        assert(mByte1 == 127);
92        assert(mByte2 == -128);
93        assert(mChar1 == 32767);
94        assert(mChar2 == 65535);
95        assert(mShort1 == 32767);
96        assert(mShort2 == -32768);
97        assert(mInt1 == 65537);
98        assert(mInt2 == -65537);
99        assert(mFloat1 > 3.141f && mFloat1 < 3.142f);
100        assert(mFloat2 < mFloat1);
101        assert(mLong1 == 1234605616436508552L);
102        assert(mLong2 == -1234605616436508552L);
103        assert(mDouble1 > 3.141592653 && mDouble1 < 3.141592654);
104        assert(mDouble2 > mDouble1);
105        assert(mVolatileLong1 == 1234605616436508551L);
106        assert(mVolatileLong2 == -1234605616436508551L);
107    }
108}
109