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  package org.apache.commons.javaflow.bytecode.transformation.bcel.analyser;
18  
19  import org.apache.bcel.generic.*;
20  import java.util.HashSet;
21  import java.util.Hashtable;
22  
23  /**
24   * This class allows easy access to ExceptionHandler objects.
25   * 
26   * WARNING! These classes are a fork of the bcel verifier.
27   *
28   * @version $Id: ExceptionHandlers.java 480487 2006-11-29 08:54:42Z bayard $
29   * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
30   */
31  public class ExceptionHandlers{
32  	/**
33  	 * The ExceptionHandler instances.
34  	 * Key: InstructionHandle objects, Values: HashSet<ExceptionHandler> instances.
35  	 */
36  	private Hashtable exceptionhandlers;
37  	 
38  	/**
39  	 * Constructor. Creates a new ExceptionHandlers instance.
40  	 */
41  	public ExceptionHandlers(MethodGen mg){
42  		exceptionhandlers = new Hashtable();
43  		CodeExceptionGen[] cegs = mg.getExceptionHandlers();
44  		for (int i=0; i<cegs.length; i++){
45  			ExceptionHandler eh = new ExceptionHandler(cegs[i].getCatchType(), cegs[i].getHandlerPC());
46  			for (InstructionHandle ih=cegs[i].getStartPC(); ih != cegs[i].getEndPC().getNext(); ih=ih.getNext()){
47  				HashSet hs;
48  				hs = (HashSet) exceptionhandlers.get(ih);
49  				if (hs == null){
50  					hs = new HashSet();
51  					exceptionhandlers.put(ih, hs);
52  				}
53  				hs.add(eh);
54  			}
55  		}
56  	}
57  	
58  	/**
59  	 * Returns all the ExceptionHandler instances representing exception
60  	 * handlers that protect the instruction ih.
61  	 */
62  	public ExceptionHandler[] getExceptionHandlers(InstructionHandle ih){
63  		HashSet hs = (HashSet) exceptionhandlers.get(ih);
64  		if (hs == null) return new ExceptionHandler[0];
65  		else{
66  			ExceptionHandler[] ret = new ExceptionHandler[hs.size()];
67  			return (ExceptionHandler[]) (hs.toArray(ret));
68  		}
69  	}
70  
71  }