1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.verifier.structurals;
18
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.bcel.generic.CodeExceptionGen;
25 import org.apache.bcel.generic.InstructionHandle;
26 import org.apache.bcel.generic.MethodGen;
27
28
29
30
31 public class ExceptionHandlers {
32
33
34
35
36 private static final ExceptionHandler[] EMPTY_ARRAY = {};
37
38
39
40
41 private final Map<InstructionHandle, Set<ExceptionHandler>> exceptionHandlers;
42
43
44
45
46 public ExceptionHandlers(final MethodGen mg) {
47 exceptionHandlers = new HashMap<>();
48 final CodeExceptionGen[] cegs = mg.getExceptionHandlers();
49 for (final CodeExceptionGen ceg : cegs) {
50 final ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC());
51 for (InstructionHandle ih = ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih = ih.getNext()) {
52 exceptionHandlers.computeIfAbsent(ih, k -> new HashSet<>()).add(eh);
53 }
54 }
55 }
56
57
58
59
60 public ExceptionHandler[] getExceptionHandlers(final InstructionHandle ih) {
61 final Set<ExceptionHandler> hsSet = exceptionHandlers.get(ih);
62 if (hsSet == null) {
63 return EMPTY_ARRAY;
64 }
65 return hsSet.toArray(ExceptionHandler.EMPTY_ARRAY);
66 }
67
68 }