1 /* 2 * Copyright (C) 2022 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 static class NoPreloadHolder { 20 static Object o = null; 21 22 static { o.toString()23 o.toString(); 24 } 25 $noinline$doCall()26 static void $noinline$doCall() { 27 } 28 } 29 main(String[] args)30 public static void main(String[] args) { 31 try { 32 NoPreloadHolder.$noinline$doCall(); 33 throw new Error("Expected ExceptionInInitializerError"); 34 } catch (ExceptionInInitializerError e) { 35 // expected 36 check(e, mainLine); 37 } 38 } 39 40 public static int mainLine = 32; 41 check(ExceptionInInitializerError ie, int mainLine)42 static void check(ExceptionInInitializerError ie, int mainLine) { 43 StackTraceElement[] trace = ie.getStackTrace(); 44 assertEquals(trace.length, 1); 45 checkElement(trace[0], "Main", "main", "Main.java", mainLine); 46 } 47 checkElement(StackTraceElement element, String declaringClass, String methodName, String fileName, int lineNumber)48 static void checkElement(StackTraceElement element, 49 String declaringClass, String methodName, 50 String fileName, int lineNumber) { 51 assertEquals(declaringClass, element.getClassName()); 52 assertEquals(methodName, element.getMethodName()); 53 assertEquals(fileName, element.getFileName()); 54 assertEquals(lineNumber, element.getLineNumber()); 55 } 56 assertEquals(Object expected, Object actual)57 static void assertEquals(Object expected, Object actual) { 58 if (!expected.equals(actual)) { 59 String msg = "Expected \"" + expected + "\" but got \"" + actual + "\""; 60 throw new AssertionError(msg); 61 } 62 } 63 assertEquals(int expected, int actual)64 static void assertEquals(int expected, int actual) { 65 if (expected != actual) { 66 throw new AssertionError("Expected " + expected + " got " + actual); 67 } 68 } 69 } 70