1 /*
2  * Copyright (C) 2014 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 // Regression test for 22460222, the sub class.
18 // The field gaps order was wrong. If there were two gaps of different sizes,
19 // and the larger one was needed, it wouldn't be found.
20 
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Method;
23 
24 class GapOrder extends GapOrderBase {
25   // The base class is 9 bytes. The entire class should be packed as:
26   //
27   //    00: oooo oooo
28   //    08: b-ss rrrr
29   //    16: rrrr iiii
30   //    24: dddd dddd
31   //
32   // The problem was, the packer wasn't finding the gap where iiii should go,
33   // because the gap where ss goes was given priority. Instead it packed as:
34   //    00: oooo oooo
35   //    08: b--- rrrr
36   //    16: rrrr ----
37   //    24: dddd dddd
38   //    32: iiii ss
39   public Object r1;
40   public Object r2;
41   public double d;
42   public int i;
43   public short s;
44 
CheckField(String fieldName, int expected)45   static private void CheckField(String fieldName, int expected) {
46     Field field = null;
47     try {
48       field = GapOrder.class.getField(fieldName);
49     } catch (ReflectiveOperationException e) {
50       System.out.println(fieldName + " not found in GapOrder.");
51       return;
52     }
53 
54     int actual = -1;
55     try {
56       Method getOffset = Field.class.getMethod("getOffset");
57       actual = (Integer)getOffset.invoke(field);
58     } catch (ReflectiveOperationException e) {
59       System.out.println("Unable to get field offset for " + fieldName + ":" + e);
60       return;
61     }
62 
63     if (actual != expected) {
64       System.out.println(
65           String.format("GapOrder.%s has offset %d, but expected %d",
66             fieldName, actual, expected));
67     }
68   }
69 
Check()70   static public void Check() {
71     CheckField("r1", 12);
72     CheckField("r2", 16);
73     CheckField("d", 24);
74     CheckField("i", 20);
75     CheckField("s", 10);
76   }
77 }
78 
79