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 {
main(String[] args)18     public static void main(String[] args) throws Exception {
19         // Inline methods that sometimes throw.
20         $noinline$assertEquals(-1000, $noinline$testThrowsWithZero(0));
21         $noinline$assertEquals(1, $noinline$testThrowsWithZero(1));
22 
23         // Tests that we can correctly inline even when the throw is not caught.
24         try {
25             $noinline$testThrowNotCaught(0);
26             unreachable();
27         } catch (Error expected) {
28         }
29     }
30 
$noinline$assertEquals(int expected, int result)31     public static void $noinline$assertEquals(int expected, int result) {
32         if (expected != result) {
33             throw new Error("Expected: " + expected + ", found: " + result);
34         }
35     }
36 
$noinline$testThrowsWithZero(int value)37     private static int $noinline$testThrowsWithZero(int value) {
38         try {
39             return $inline$throwsWithZeroOrReturns(value);
40         } catch (Error e) {
41             return -1000;
42         }
43     }
44 
$inline$throwsWithZeroOrReturns(int value)45     private static int $inline$throwsWithZeroOrReturns(int value) {
46         if (value == 0) {
47             throw new Error("Zero!");
48         } else {
49             return value;
50         }
51     }
52 
$noinline$testThrowNotCaught(int value)53     private static int $noinline$testThrowNotCaught(int value) {
54         try {
55             return $inline$throwsWithZeroOrReturns(value);
56         } catch (Exception e) {
57             return -1000;
58         }
59     }
60 
unreachable()61     private static void unreachable() throws Exception{
62         throw new Exception("Unreachable");
63     }
64 }
65