xref: /aosp_15_r20/art/test/542-inline-trycatch/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 public class Main {
18 
19   // The following tests make sure that we inline methods used inside try and catch
20   // blocks, provided they meet other inlining criteria. To do that, we rely on
21   // the compiler recognizing and enforcing the $inline$ and $noinline$ markers.
22 
23   // We expect a single block to always be inlined.
24 
$inline$SingleBlock(String str)25   private static int $inline$SingleBlock(String str) throws NumberFormatException {
26     return Integer.parseInt(str);
27   }
28 
29   // We expect a "simple" method with multiple blocks to always be inlined.
30 
$inline$MultipleBlocks(String str, boolean is_hex)31   private static int $inline$MultipleBlocks(String str, boolean is_hex)
32       throws NumberFormatException {
33     return is_hex ? Integer.parseInt(str, 16) : Integer.parseInt(str);
34   }
35 
testSingleBlockFromTry()36   public static void testSingleBlockFromTry() {
37     int val = 0;
38 
39     try {
40       val = $inline$SingleBlock("42");
41     } catch (NumberFormatException ex) {
42       unreachable();
43     }
44     assertEquals(42, val);
45 
46     try {
47       $inline$SingleBlock("xyz");
48       unreachable();
49     } catch (NumberFormatException ex) {}
50   }
51 
testSingleBlockFromCatch()52   public static void testSingleBlockFromCatch() {
53     int val = 0;
54 
55     try {
56       throwException();
57     } catch (Exception ex) {
58       val = $inline$SingleBlock("42");
59     }
60     assertEquals(42, val);
61   }
62 
testMultipleBlocksFromTry()63   public static void testMultipleBlocksFromTry() {
64     int val = 0;
65 
66     try {
67       val = $inline$MultipleBlocks("42", false);
68     } catch (NumberFormatException ex) {
69       unreachable();
70     }
71     assertEquals(42, val);
72 
73     try {
74       val = $inline$MultipleBlocks("20", true);
75     } catch (NumberFormatException ex) {
76       unreachable();
77     }
78     assertEquals(32, val);
79 
80     try {
81       $inline$MultipleBlocks("xyz", false);
82       unreachable();
83     } catch (NumberFormatException ex) {}
84 
85     try {
86       $inline$MultipleBlocks("xyz", true);
87       unreachable();
88     } catch (NumberFormatException ex) {}
89   }
90 
testMultipleBlocksFromCatch()91   public static void testMultipleBlocksFromCatch() {
92     int val = 0;
93 
94     try {
95       throwException();
96     } catch (Exception ex) {
97       val = $inline$MultipleBlocks("42", false);
98     }
99     assertEquals(42, val);
100 
101     try {
102       throwException();
103     } catch (Exception ex) {
104       val = $inline$MultipleBlocks("20", true);
105     }
106     assertEquals(32, val);
107   }
108 
main(String[] args)109   public static void main(String[] args) {
110     testSingleBlockFromTry();
111     testSingleBlockFromCatch();
112     testMultipleBlocksFromTry();
113     testMultipleBlocksFromCatch();
114   }
115 
assertEquals(int expected, int actual)116   private static void assertEquals(int expected, int actual) {
117     if (expected != actual) {
118       throw new AssertionError("Wrong result: " + expected + " != " + actual);
119     }
120   }
121 
unreachable()122   private static void unreachable() {
123     throw new Error("Unreachable");
124   }
125 
throwException()126   private static void throwException() throws Exception {
127     throw new Exception();
128   }
129 }
130