    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;
    020
    021import org.apache.commons.lang3.ArrayUtils;
    022
    023/**
    024 * Exception constants.
    025 *
    026 * @since 6.0 (intended to replace the InstructionConstant interface)
    027 */
    028public final class ExceptionConst {
    029
    030    /**
    031     * Enum corresponding to the various Exception Class arrays, used by
    032     * {@link ExceptionConst#createExceptions(EXCS, Class...)}
    033     */
    034    public enum EXCS {
    035        EXCS_CLASS_AND_INTERFACE_RESOLUTION, EXCS_FIELD_AND_METHOD_RESOLUTION, EXCS_INTERFACE_METHOD_RESOLUTION, EXCS_STRING_RESOLUTION, EXCS_ARRAY_EXCEPTION,
    036    }
    037
    038    /**
    039     * The mother of all exceptions
    040     */
    041    public static final Class<Throwable> THROWABLE = Throwable.class;
    042
    043    /**
    044     * Super class of any run-time exception
    045     */
    046    public static final Class<RuntimeException> RUNTIME_EXCEPTION = RuntimeException.class;
    047
    048    /**
    049     * Super class of any linking exception (aka Linkage Error)
    050     */
    051    public static final Class<LinkageError> LINKING_EXCEPTION = LinkageError.class;
    052    /**
    053     * Linking Exceptions
    054     */
    055    public static final Class<ClassCircularityError> CLASS_CIRCULARITY_ERROR = ClassCircularityError.class;
    056    public static final Class<ClassFormatError> CLASS_FORMAT_ERROR = ClassFormatError.class;
    057    public static final Class<ExceptionInInitializerError> EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class;
    058    public static final Class<IncompatibleClassChangeError> INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class;
    059    public static final Class<AbstractMethodError> ABSTRACT_METHOD_ERROR = AbstractMethodError.class;
    060    public static final Class<IllegalAccessError> ILLEGAL_ACCESS_ERROR = IllegalAccessError.class;
    061    public static final Class<InstantiationError> INSTANTIATION_ERROR = InstantiationError.class;
    062    public static final Class<NoSuchFieldError> NO_SUCH_FIELD_ERROR = NoSuchFieldError.class;
    063    public static final Class<NoSuchMethodError> NO_SUCH_METHOD_ERROR = NoSuchMethodError.class;
    064    public static final Class<NoClassDefFoundError> NO_CLASS_DEF_FOUND_ERROR = NoClassDefFoundError.class;
    065    public static final Class<UnsatisfiedLinkError> UNSATISFIED_LINK_ERROR = UnsatisfiedLinkError.class;
    066
    067    public static final Class<VerifyError> VERIFY_ERROR = VerifyError.class;
    068    /* UnsupportedClassVersionError is new in JDK 1.2 */
    069//    public static final Class UnsupportedClassVersionError = UnsupportedClassVersionError.class;
    070    /**
    071     * Run-Time Exceptions
    072     */
    073    public static final Class<NullPointerException> NULL_POINTER_EXCEPTION = NullPointerException.class;
    074    public static final Class<ArrayIndexOutOfBoundsException> ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION = ArrayIndexOutOfBoundsException.class;
    075    public static final Class<ArithmeticException> ARITHMETIC_EXCEPTION = ArithmeticException.class;
    076    public static final Class<NegativeArraySizeException> NEGATIVE_ARRAY_SIZE_EXCEPTION = NegativeArraySizeException.class;
    077    public static final Class<ClassCastException> CLASS_CAST_EXCEPTION = ClassCastException.class;
    078
    079    public static final Class<IllegalMonitorStateException> ILLEGAL_MONITOR_STATE = IllegalMonitorStateException.class;
    080    /**
    081     * Pre-defined exception arrays according to chapters 5.1-5.4 of the Java Virtual Machine Specification
    082     */
    083    private static final Class<?>[] EXCS_CLASS_AND_INTERFACE_RESOLUTION = {NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR,
    084        EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR}; // Chapter 5.1
    085
    086    private static final Class<?>[] EXCS_FIELD_AND_METHOD_RESOLUTION = {NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR}; // Chapter 5.2
    087
    088    /**
    089     * Empty array.
    090     */
    091    private static final Class<?>[] EXCS_INTERFACE_METHOD_RESOLUTION = new Class[0]; // Chapter 5.3 (as below)
    092
    093    /**
    094     * Empty array.
    095     */
    096    private static final Class<?>[] EXCS_STRING_RESOLUTION = new Class[0];
    097
    098    // Chapter 5.4 (no errors but the ones that _always_ could happen! How stupid.)
    099    private static final Class<?>[] EXCS_ARRAY_EXCEPTION = {NULL_POINTER_EXCEPTION, ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION};
    100
    101    /**
    102     * Creates a copy of the specified Exception Class array combined with any additional Exception classes.
    103     *
    104     * @param type the basic array type
    105     * @param extraClasses additional classes, if any
    106     * @return the merged array
    107     */
    108    public static Class<?>[] createExceptions(final EXCS type, final Class<?>... extraClasses) {
    109        switch (type) {
    110        case EXCS_CLASS_AND_INTERFACE_RESOLUTION:
    111            return mergeExceptions(EXCS_CLASS_AND_INTERFACE_RESOLUTION, extraClasses);
    112        case EXCS_ARRAY_EXCEPTION:
    113            return mergeExceptions(EXCS_ARRAY_EXCEPTION, extraClasses);
    114        case EXCS_FIELD_AND_METHOD_RESOLUTION:
    115            return mergeExceptions(EXCS_FIELD_AND_METHOD_RESOLUTION, extraClasses);
    116        case EXCS_INTERFACE_METHOD_RESOLUTION:
    117            return mergeExceptions(EXCS_INTERFACE_METHOD_RESOLUTION, extraClasses);
    118        case EXCS_STRING_RESOLUTION:
    119            return mergeExceptions(EXCS_STRING_RESOLUTION, extraClasses);
    120        default:
    121            throw new AssertionError("Cannot happen; unexpected enum value: " + type);
    122        }
    123    }
    124
    125    // helper method to merge exception class arrays
    126    private static Class<?>[] mergeExceptions(final Class<?>[] input, final Class<?>... extraClasses) {
    127        return ArrayUtils.addAll(input, extraClasses);
    128    }
    129
    130}