    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 */
    034public class ExceptionHandlers{
    035    /**
    036     * The ExceptionHandler instances.
    037     * Key: InstructionHandle objects, Values: HashSet<ExceptionHandler> instances.
    038     */
    039    private final Map<InstructionHandle, Set<ExceptionHandler>> exceptionhandlers;
    040
    041    /**
    042     * Constructor. Creates a new ExceptionHandlers instance.
    043     */
    044    public ExceptionHandlers(final MethodGen mg) {
    045        exceptionhandlers = new HashMap<>();
    046        final CodeExceptionGen[] cegs = mg.getExceptionHandlers();
    047        for (final CodeExceptionGen ceg : cegs) {
    048            final ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC());
    049            for (InstructionHandle ih=ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih=ih.getNext()) {
    050                Set<ExceptionHandler> hs;
    051                hs = exceptionhandlers.get(ih);
    052                if (hs == null) {
    053                    hs = new HashSet<>();
    054                    exceptionhandlers.put(ih, hs);
    055                }
    056                hs.add(eh);
    057            }
    058        }
    059    }
    060
    061    /**
    062     * Returns all the ExceptionHandler instances representing exception
    063     * handlers that protect the instruction ih.
    064     */
    065    public ExceptionHandler[] getExceptionHandlers(final InstructionHandle ih) {
    066        final Set<ExceptionHandler> hsSet = exceptionhandlers.get(ih);
    067        if (hsSet == null) {
    068            return new ExceptionHandler[0];
    069        }
    070        return hsSet.toArray(new ExceptionHandler[hsSet.size()]);
    071    }
    072
    073}