1 /*
2  * Copyright (C) 2010 Google Inc.
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 /**
18  * Test code generation for field accesses, based on libcore's FieldAccessBenchmark.
19  */
20 public class Main {
21     private static class Inner {
22         public int publicInnerIntVal;
23         protected int protectedInnerIntVal;
24         private int privateInnerIntVal;
25         int packageInnerIntVal;
26     }
27     int intVal = 42;
28     final int finalIntVal = 42;
29     static int staticIntVal = 42;
30     static final int staticFinalIntVal = 42;
31 
timeField(int reps)32     public int timeField(int reps) {
33         int result = 0;
34         for (int rep = 0; rep < reps; ++rep) {
35             result = intVal;
36         }
37         return result;
38     }
timeFieldFinal(int reps)39     public int timeFieldFinal(int reps) {
40         int result = 0;
41         for (int rep = 0; rep < reps; ++rep) {
42             result = finalIntVal;
43         }
44         return result;
45     }
timeFieldStatic(int reps)46     public int timeFieldStatic(int reps) {
47         int result = 0;
48         for (int rep = 0; rep < reps; ++rep) {
49             result = staticIntVal;
50         }
51         return result;
52     }
timeFieldStaticFinal(int reps)53     public int timeFieldStaticFinal(int reps) {
54         int result = 0;
55         for (int rep = 0; rep < reps; ++rep) {
56             result = staticFinalIntVal;
57         }
58         return result;
59     }
timeFieldCached(int reps)60     public int timeFieldCached(int reps) {
61         int result = 0;
62         int cachedIntVal = this.intVal;
63         for (int rep = 0; rep < reps; ++rep) {
64             result = cachedIntVal;
65         }
66         return result;
67     }
timeFieldPrivateInnerClassPublicField(int reps)68     public int timeFieldPrivateInnerClassPublicField(int reps) {
69         int result = 0;
70         Inner inner = new Inner();
71         for (int rep = 0; rep < reps; ++rep) {
72             result = inner.publicInnerIntVal;
73         }
74         return result;
75     }
timeFieldPrivateInnerClassProtectedField(int reps)76     public int timeFieldPrivateInnerClassProtectedField(int reps) {
77         int result = 0;
78         Inner inner = new Inner();
79         for (int rep = 0; rep < reps; ++rep) {
80             result = inner.protectedInnerIntVal;
81         }
82         return result;
83     }
timeFieldPrivateInnerClassPrivateField(int reps)84     public int timeFieldPrivateInnerClassPrivateField(int reps) {
85         int result = 0;
86         Inner inner = new Inner();
87         for (int rep = 0; rep < reps; ++rep) {
88             result = inner.privateInnerIntVal;
89         }
90         return result;
91     }
timeFieldPrivateInnerClassPackageField(int reps)92     public int timeFieldPrivateInnerClassPackageField(int reps) {
93         int result = 0;
94         Inner inner = new Inner();
95         for (int rep = 0; rep < reps; ++rep) {
96             result = inner.packageInnerIntVal;
97         }
98         return result;
99     }
100 
main(String args[])101     public static void main(String args[]) {
102         System.out.println("Starting test");
103         Main i = new Main();
104         i.timeField(100);
105         i.timeFieldFinal(100);
106         i.timeFieldStatic(100);
107         i.timeFieldStaticFinal(100);
108         i.timeFieldCached(100);
109         i.timeFieldPrivateInnerClassPublicField(100);
110         i.timeFieldPrivateInnerClassProtectedField(100);
111         i.timeFieldPrivateInnerClassPrivateField(100);
112         i.timeFieldPrivateInnerClassPackageField(100);
113         System.out.println("Test complete");
114     }
115 }
116