|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +public class A { |
| 4 | + static String source(String tag) { return tag; } |
| 5 | + |
| 6 | + static void sink(Object o) { } |
| 7 | + |
| 8 | + static class MyException extends RuntimeException { |
| 9 | + String msg; |
| 10 | + MyException(String msg) { |
| 11 | + this.msg = msg; |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + static class MySubException extends MyException { |
| 16 | + MySubException(String msg) { |
| 17 | + super(msg); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + static class MyOtherException extends RuntimeException { |
| 22 | + String msg; |
| 23 | + MyOtherException(String msg) { |
| 24 | + this.msg = msg; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + void throwSome(int i) { |
| 29 | + if (i == 1) throw new MyException(source("my")); |
| 30 | + if (i == 2) throw new MySubException(source("sub")); |
| 31 | + try { |
| 32 | + if (i == 3) throw new MyOtherException(source("other")); |
| 33 | + } catch (ClassCastException e) { |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + void foo(int i) { |
| 38 | + try { |
| 39 | + try { |
| 40 | + throwSome(i); |
| 41 | + } finally { |
| 42 | + } |
| 43 | + } catch (MySubException e) { |
| 44 | + sink(e.msg); // $ hasValueFlow=sub SPURIOUS: hasValueFlow=my |
| 45 | + } catch (MyException e) { |
| 46 | + sink(e.msg); // $ hasValueFlow=my SPURIOUS: hasValueFlow=sub |
| 47 | + } catch (MyOtherException e) { |
| 48 | + sink(e.msg); // $ hasValueFlow=other |
| 49 | + } catch (Exception e) { |
| 50 | + sink(((MyOtherException)e).msg); // $ SPURIOUS: hasValueFlow=other |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void throwArg(String msg) { |
| 55 | + throw new MyException(msg); |
| 56 | + } |
| 57 | + |
| 58 | + void catchSummary() { |
| 59 | + try { |
| 60 | + throwArg(source("arg")); |
| 61 | + } catch (MyException e) { |
| 62 | + sink(e.msg); // $ hasValueFlow=arg |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void runCallback(Runnable r) { |
| 67 | + r.run(); |
| 68 | + } |
| 69 | + |
| 70 | + void catchCallback() { |
| 71 | + try { |
| 72 | + runCallback(() -> { throw new MyException(source("cb")); }); |
| 73 | + } catch (MyException e) { |
| 74 | + sink(e.msg); // $ hasValueFlow=cb |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + List<String> l = Arrays.asList(new String[] { "s" }); |
| 79 | + l.forEach(s -> { throw new MyException(source("cb2")); }); |
| 80 | + } catch (MyException e) { |
| 81 | + sink(e.msg); // $ hasValueFlow=cb2 |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + void catchRuntimeException() { |
| 86 | + try { |
| 87 | + throw new RuntimeException(source("rte")); |
| 88 | + } catch (RuntimeException e) { |
| 89 | + sink(e.getMessage()); // $ hasValueFlow=rte |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments