ExceptionHandlers xref

View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    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  package org.apache.bcel.verifier.structurals;
    18
    19
    20  import java.util.HashSet;
    21  import java.util.Hashtable;
    22  import java.util.Set;
    23  import org.apache.bcel.generic.CodeExceptionGen;
    24  import org.apache.bcel.generic.InstructionHandle;
    25  import org.apache.bcel.generic.MethodGen;
    26
    27  /***
    28   * This class allows easy access to ExceptionHandler objects.
    29   *
    30   * @version $Id: ExceptionHandlers.java 386056 2006-03-15 11:31:56Z tcurdt $
    31   * @author Enver Haase
    32   */
    33  public class ExceptionHandlers{
    34  	/***
    35  	 * The ExceptionHandler instances.
    36  	 * Key: InstructionHandle objects, Values: HashSet<ExceptionHandler> instances.
    37  	 */
    38  	private Hashtable exceptionhandlers;
    39
    40  	/***
    41  	 * Constructor. Creates a new ExceptionHandlers instance.
    42  	 */
    43  	public ExceptionHandlers(MethodGen mg){
    44  		exceptionhandlers = new Hashtable();
    45  		CodeExceptionGen[] cegs = mg.getExceptionHandlers();
    46  		for (int i=0; i<cegs.length; i++){
    47  			ExceptionHandler eh = new ExceptionHandler(cegs[i].getCatchType(), cegs[i].getHandlerPC());
    48  			for (InstructionHandle ih=cegs[i].getStartPC(); ih != cegs[i].getEndPC().getNext(); ih=ih.getNext()){
    49  				Set hs;
    50  				hs = (Set) exceptionhandlers.get(ih);
    51  				if (hs == null){
    52  					hs = new HashSet();
    53  					exceptionhandlers.put(ih, hs);
    54  				}
    55  				hs.add(eh);
    56  			}
    57  		}
    58  	}
    59
    60  	/***
    61  	 * Returns all the ExceptionHandler instances representing exception
    62  	 * handlers that protect the instruction ih.
    63  	 */
    64  	public ExceptionHandler[] getExceptionHandlers(InstructionHandle ih){
    65  		Set hs = (Set) exceptionhandlers.get(ih);
    66  		if (hs == null) {
    67              return new ExceptionHandler[0];
    68          } else{
    69  			ExceptionHandler[] ret = new ExceptionHandler[hs.size()];
    70  			return (ExceptionHandler[]) (hs.toArray(ret));
    71  		}
    72  	}
    73
    74  }