001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.bcel.verifier.structurals; 019 020 021import java.util.HashMap; 022import java.util.HashSet; 023import java.util.Map; 024import java.util.Set; 025 026import org.apache.bcel.generic.CodeExceptionGen; 027import org.apache.bcel.generic.InstructionHandle; 028import org.apache.bcel.generic.MethodGen; 029 030/** 031 * This class allows easy access to ExceptionHandler objects. 032 * 033 * @version $Id: ExceptionHandlers.java 1749603 2016-06-21 20:50:19Z ggregory $ 034 */ 035public class ExceptionHandlers{ 036 /** 037 * The ExceptionHandler instances. 038 * Key: InstructionHandle objects, Values: HashSet<ExceptionHandler> instances. 039 */ 040 private final Map<InstructionHandle, Set<ExceptionHandler>> exceptionhandlers; 041 042 /** 043 * Constructor. Creates a new ExceptionHandlers instance. 044 */ 045 public ExceptionHandlers(final MethodGen mg) { 046 exceptionhandlers = new HashMap<>(); 047 final CodeExceptionGen[] cegs = mg.getExceptionHandlers(); 048 for (final CodeExceptionGen ceg : cegs) { 049 final ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC()); 050 for (InstructionHandle ih=ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih=ih.getNext()) { 051 Set<ExceptionHandler> hs; 052 hs = exceptionhandlers.get(ih); 053 if (hs == null) { 054 hs = new HashSet<>(); 055 exceptionhandlers.put(ih, hs); 056 } 057 hs.add(eh); 058 } 059 } 060 } 061 062 /** 063 * Returns all the ExceptionHandler instances representing exception 064 * handlers that protect the instruction ih. 065 */ 066 public ExceptionHandler[] getExceptionHandlers(final InstructionHandle ih) { 067 final Set<ExceptionHandler> hsSet = exceptionhandlers.get(ih); 068 if (hsSet == null) { 069 return new ExceptionHandler[0]; 070 } 071 return hsSet.toArray(new ExceptionHandler[hsSet.size()]); 072 } 073 074}