Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 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 | // Note that $opt$ is a marker for the optimizing compiler to ensure |
| 18 | // it does compile the method. |
| 19 | |
| 20 | public class Main { |
| 21 | public static void main(String[] args) { |
| 22 | Error error = null; |
| 23 | try { |
| 24 | $opt$TestInvokeStatic(); |
| 25 | } catch (Error e) { |
| 26 | error = e; |
| 27 | } |
| 28 | System.out.println(error); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 29 | |
| 30 | $opt$TestInvokeNew(); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 31 | |
| 32 | int result = $opt$TestInvokeIntParameter(42); |
| 33 | if (result != 42) { |
| 34 | throw new Error("Different value returned: " + result); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | $opt$TestInvokeObjectParameter(new Object()); |
| 39 | |
| 40 | Object a = new Object(); |
| 41 | Object b = $opt$TestInvokeObjectParameter(a); |
| 42 | if (a != b) { |
| 43 | throw new Error("Different object returned " + a + " " + b); |
| 44 | } |
| 45 | |
| 46 | result = $opt$TestInvokeWith2Parameters(10, 9); |
| 47 | if (result != 1) { |
| 48 | throw new Error("Unexpected result: " + result); |
| 49 | } |
| 50 | |
| 51 | result = $opt$TestInvokeWith3Parameters(10, 9, 1); |
| 52 | if (result != 0) { |
| 53 | throw new Error("Unexpected result: " + result); |
| 54 | } |
| 55 | |
| 56 | result = $opt$TestInvokeWith5Parameters(10000, 1000, 100, 10, 1); |
| 57 | if (result != 8889) { |
| 58 | throw new Error("Unexpected result: " + result); |
| 59 | } |
| 60 | |
| 61 | result = $opt$TestInvokeWith7Parameters(100, 6, 5, 4, 3, 2, 1); |
| 62 | if (result != 79) { |
| 63 | throw new Error("Unexpected result: " + result); |
| 64 | } |
| 65 | |
| 66 | Main m = new Main(); |
| 67 | if (m.$opt$TestThisParameter(m) != m) { |
| 68 | throw new Error("Unexpected value returned"); |
| 69 | } |
| 70 | |
| 71 | if (m.$opt$TestOtherParameter(new Main()) == m) { |
| 72 | throw new Error("Unexpected value returned"); |
| 73 | } |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 74 | |
| 75 | if (m.$opt$TestReturnNewObject(m) == m) { |
| 76 | throw new Error("Unexpected value returned"); |
| 77 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int $opt$TestInvokeIntParameter(int param) { |
| 81 | return param; |
| 82 | } |
| 83 | |
| 84 | static Object $opt$TestInvokeObjectParameter(Object a) { |
| 85 | forceGCStaticMethod(); |
| 86 | return a; |
| 87 | } |
| 88 | |
| 89 | static int $opt$TestInvokeWith2Parameters(int a, int b) { |
| 90 | return a - b; |
| 91 | } |
| 92 | |
| 93 | static int $opt$TestInvokeWith3Parameters(int a, int b, int c) { |
| 94 | return a - b - c; |
| 95 | } |
| 96 | |
| 97 | static int $opt$TestInvokeWith5Parameters(int a, int b, int c, int d, int e) { |
| 98 | return a - b - c - d - e; |
| 99 | } |
| 100 | |
| 101 | static int $opt$TestInvokeWith7Parameters(int a, int b, int c, int d, int e, int f, int g) { |
| 102 | return a - b - c - d - e - f - g; |
| 103 | } |
| 104 | |
| 105 | Object $opt$TestThisParameter(Object other) { |
| 106 | forceGCStaticMethod(); |
| 107 | return other; |
| 108 | } |
| 109 | |
| 110 | Object $opt$TestOtherParameter(Object other) { |
| 111 | forceGCStaticMethod(); |
| 112 | return other; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 115 | Object $opt$TestReturnNewObject(Object other) { |
| 116 | Object o = new Object(); |
| 117 | forceGCStaticMethod(); |
| 118 | return o; |
| 119 | } |
| 120 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 121 | public static void $opt$TestInvokeStatic() { |
| 122 | printStaticMethod(); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 123 | printStaticMethodWith2Args(1, 2); |
| 124 | printStaticMethodWith5Args(1, 2, 3, 4, 5); |
| 125 | printStaticMethodWith7Args(1, 2, 3, 4, 5, 6, 7); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 126 | forceGCStaticMethod(); |
| 127 | throwStaticMethod(); |
| 128 | } |
| 129 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 130 | public static void $opt$TestInvokeNew() { |
| 131 | Object o = new Object(); |
| 132 | forceGCStaticMethod(); |
| 133 | printStaticMethodWithObjectArg(o); |
| 134 | forceGCStaticMethod(); |
| 135 | } |
| 136 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 137 | public static void printStaticMethod() { |
| 138 | System.out.println("In static method"); |
| 139 | } |
| 140 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 141 | public static void printStaticMethodWith2Args(int a, int b) { |
| 142 | System.out.println("In static method with 2 args " + a + " " + b); |
| 143 | } |
| 144 | |
| 145 | public static void printStaticMethodWith5Args(int a, int b, int c, int d, int e) { |
| 146 | System.out.println("In static method with 5 args " |
| 147 | + a + " " + b + " " + c + " " + d + " " + e); |
| 148 | } |
| 149 | |
| 150 | public static void printStaticMethodWith7Args(int a, int b, int c, int d, int e, int f, int g) { |
| 151 | System.out.println("In static method with 7 args " |
| 152 | + a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g); |
| 153 | } |
| 154 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 155 | public static void printStaticMethodWithObjectArg(Object a) { |
| 156 | System.out.println("In static method with object arg " + a.getClass()); |
| 157 | } |
| 158 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 159 | public static void forceGCStaticMethod() { |
| 160 | Runtime.getRuntime().gc(); |
| 161 | Runtime.getRuntime().gc(); |
| 162 | Runtime.getRuntime().gc(); |
| 163 | Runtime.getRuntime().gc(); |
| 164 | Runtime.getRuntime().gc(); |
| 165 | Runtime.getRuntime().gc(); |
| 166 | System.out.println("Forced GC"); |
| 167 | } |
| 168 | |
| 169 | public static void throwStaticMethod() { |
| 170 | throw new Error("Error"); |
| 171 | } |
| 172 | } |