001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.verifier.structurals;
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 */
033public class ExceptionHandlers {
034
035    /**
036     * Empty array.
037     */
038    private static final ExceptionHandler[] EMPTY_ARRAY = {};
039
040    /**
041     * The ExceptionHandler instances. Key: InstructionHandle objects, Values: HashSet<ExceptionHandler> instances.
042     */
043    private final Map<InstructionHandle, Set<ExceptionHandler>> exceptionHandlers;
044
045    /**
046     * Constructs a new ExceptionHandlers instance.
047     *
048     * @param mg the method generator.
049     */
050    public ExceptionHandlers(final MethodGen mg) {
051        exceptionHandlers = new HashMap<>();
052        final CodeExceptionGen[] cegs = mg.getExceptionHandlers();
053        for (final CodeExceptionGen ceg : cegs) {
054            final ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC());
055            for (InstructionHandle ih = ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih = ih.getNext()) {
056                exceptionHandlers.computeIfAbsent(ih, k -> new HashSet<>()).add(eh);
057            }
058        }
059    }
060
061    /**
062     * Returns all the ExceptionHandler instances representing exception handlers that protect the instruction ih.
063     *
064     * @param ih the instruction handle.
065     * @return array of exception handlers.
066     */
067    public ExceptionHandler[] getExceptionHandlers(final InstructionHandle ih) {
068        final Set<ExceptionHandler> hsSet = exceptionHandlers.get(ih);
069        if (hsSet == null) {
070            return EMPTY_ARRAY;
071        }
072        return hsSet.toArray(ExceptionHandler.EMPTY_ARRAY);
073    }
074
075}