View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.verifier.structurals;
19  
20  
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.bcel.generic.CodeExceptionGen;
27  import org.apache.bcel.generic.InstructionHandle;
28  import org.apache.bcel.generic.MethodGen;
29  
30  /**
31   * This class allows easy access to ExceptionHandler objects.
32   *
33   */
34  public class ExceptionHandlers{
35      /**
36       * The ExceptionHandler instances.
37       * Key: InstructionHandle objects, Values: HashSet<ExceptionHandler> instances.
38       */
39      private final Map<InstructionHandle, Set<ExceptionHandler>> exceptionHandlers;
40  
41      /**
42       * Constructor. Creates a new ExceptionHandlers instance.
43       */
44      public ExceptionHandlers(final MethodGen mg) {
45          exceptionHandlers = new HashMap<>();
46          final CodeExceptionGen[] cegs = mg.getExceptionHandlers();
47          for (final CodeExceptionGen ceg : cegs) {
48              final ExceptionHandlerurals/ExceptionHandler.html#ExceptionHandler">ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC());
49              for (InstructionHandle ih=ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih=ih.getNext()) {
50                  Set<ExceptionHandler> hs;
51                  hs = exceptionHandlers.get(ih);
52                  if (hs == null) {
53                      hs = new HashSet<>();
54                      exceptionHandlers.put(ih, hs);
55                  }
56                  hs.add(eh);
57              }
58          }
59      }
60  
61      /**
62       * Returns all the ExceptionHandler instances representing exception
63       * handlers that protect the instruction ih.
64       */
65      public ExceptionHandler[] getExceptionHandlers(final InstructionHandle ih) {
66          final Set<ExceptionHandler> hsSet = exceptionHandlers.get(ih);
67          if (hsSet == null) {
68              return new ExceptionHandler[0];
69          }
70          return hsSet.toArray(new ExceptionHandler[hsSet.size()]);
71      }
72  
73  }