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    public ExceptionHandlers(final MethodGen mg) {
049        exceptionHandlers = new HashMap<>();
050        final CodeExceptionGen[] cegs = mg.getExceptionHandlers();
051        for (final CodeExceptionGen ceg : cegs) {
052            final ExceptionHandler eh = new ExceptionHandler(ceg.getCatchType(), ceg.getHandlerPC());
053            for (InstructionHandle ih = ceg.getStartPC(); ih != ceg.getEndPC().getNext(); ih = ih.getNext()) {
054                exceptionHandlers.computeIfAbsent(ih, k -> new HashSet<>()).add(eh);
055            }
056        }
057    }
058
059    /**
060     * Returns all the ExceptionHandler instances representing exception handlers that protect the instruction ih.
061     */
062    public ExceptionHandler[] getExceptionHandlers(final InstructionHandle ih) {
063        final Set<ExceptionHandler> hsSet = exceptionHandlers.get(ih);
064        if (hsSet == null) {
065            return EMPTY_ARRAY;
066        }
067        return hsSet.toArray(ExceptionHandler.EMPTY_ARRAY);
068    }
069
070}