Nicolas Geoffray | 85af16e | 2020-05-12 15:36:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 Main { |
| 18 | |
| 19 | public static void main(String[] args) { |
| 20 | try { |
| 21 | $noinline$doTest(args); |
| 22 | throw new Error("Expected ArrayStoreException"); |
| 23 | } catch (ArrayStoreException e) { |
| 24 | // expected |
| 25 | check(e, mainLine, methodLine, "$noinline$doTest"); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public static void $noinline$doTest(String[] args) { |
| 30 | Object[] o = new String[2]; |
| 31 | o[0] = args; |
| 32 | } |
| 33 | |
| 34 | public static int mainLine = 21; |
| 35 | public static int methodLine = 31; |
| 36 | |
| 37 | static void check(ArrayStoreException ase, int mainLine, int methodLine, String methodName) { |
| 38 | StackTraceElement[] trace = ase.getStackTrace(); |
| 39 | checkElement(trace[0], "Main", methodName, "Main.java", methodLine); |
| 40 | checkElement(trace[1], "Main", "main", "Main.java", mainLine); |
| 41 | } |
| 42 | |
| 43 | static void checkElement(StackTraceElement element, |
| 44 | String declaringClass, String methodName, |
| 45 | String fileName, int lineNumber) { |
| 46 | assertEquals(declaringClass, element.getClassName()); |
| 47 | assertEquals(methodName, element.getMethodName()); |
| 48 | assertEquals(fileName, element.getFileName()); |
| 49 | assertEquals(lineNumber, element.getLineNumber()); |
| 50 | } |
| 51 | |
| 52 | static void assertEquals(Object expected, Object actual) { |
| 53 | if (!expected.equals(actual)) { |
| 54 | String msg = "Expected \"" + expected + "\" but got \"" + actual + "\""; |
| 55 | throw new AssertionError(msg); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void assertEquals(int expected, int actual) { |
| 60 | if (expected != actual) { |
| 61 | throw new AssertionError("Expected " + expected + " got " + actual); |
| 62 | } |
| 63 | } |
| 64 | } |