1 /*
2  * Copyright (C) 2018 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 import java.io.*;
18 
19 /**
20  * Regression test for b/75971227 (code sinking with exceptions).
21  */
22 public class Main {
23 
24   public static class N {
25     int x;
26   }
27 
28   private int f;
29 
doit(N n1)30   public int doit(N n1) throws FileNotFoundException {
31     int x = 1;
32     N n3 = new N();
33     try {
34       if (n1.x == 0) {
35         f = 11;
36         x = 3;
37       } else {
38         f = x;
39       }
40       throw new FileNotFoundException("n3" + n3.x);
41     } catch (NullPointerException e) {
42     }
43     return x;
44   }
45 
46 
main(String[] args)47   public static void main(String[] args) {
48     N n = new N();
49     Main t = new Main();
50     int x = 0;
51 
52     // Main 1, null pointer argument.
53     t.f = 0;
54     try {
55       x = t.doit(null);
56     } catch (FileNotFoundException e) {
57       x = -1;
58     }
59     if (x != 1 || t.f != 0) {
60       throw new Error("Main 1: x=" + x + " f=" + t.f);
61     }
62 
63     // Main 2, n.x is 0.
64     n.x = 0;
65     try {
66       x = t.doit(n);
67     } catch (FileNotFoundException e) {
68       x = -1;
69     }
70     if (x != -1 || t.f != 11) {
71       throw new Error("Main 2: x=" + x + " f=" + t.f);
72     }
73 
74     // Main 3, n.x is not 0.
75     n.x = 1;
76     try {
77       x = t.doit(n);
78     } catch (FileNotFoundException e) {
79       x = -1;
80     }
81     if (x != -1 || t.f != 1) {
82       throw new Error("Main 3: x=" + x + " f=" + t.f);
83     }
84 
85     System.out.println("passed");
86   }
87 }
88