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
036        /** Exception class and interface resolution. */
037        EXCS_CLASS_AND_INTERFACE_RESOLUTION,
038
039        /** Exception field and method resolution. */
040        EXCS_FIELD_AND_METHOD_RESOLUTION,
041
042        /** Exception interface method resolution. */
043        EXCS_INTERFACE_METHOD_RESOLUTION,
044
045        /** Exception string resolution. */
046        EXCS_STRING_RESOLUTION,
047
048        /** Exception array exception. */
049        EXCS_ARRAY_EXCEPTION,
050    }
051
052    /**
053     * The mother of all exceptions
054     */
055    public static final Class<Throwable> THROWABLE = Throwable.class;
056
057    /**
058     * Super class of any run-time exception
059     */
060    public static final Class<RuntimeException> RUNTIME_EXCEPTION = RuntimeException.class;
061
062    /**
063     * Super class of any linking exception (aka Linkage Error)
064     */
065    public static final Class<LinkageError> LINKING_EXCEPTION = LinkageError.class;
066
067    /** Exception class: ClassCircularityError. */
068    public static final Class<ClassCircularityError> CLASS_CIRCULARITY_ERROR = ClassCircularityError.class;
069
070    /**
071     * Linking Exceptions.
072     */
073
074    /** Exception class: ClassFormatError. */
075    public static final Class<ClassFormatError> CLASS_FORMAT_ERROR = ClassFormatError.class;
076
077    /** Exception class: ExceptionInInitializerError. */
078    public static final Class<ExceptionInInitializerError> EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class;
079
080    /** Exception class: IncompatibleClassChangeError. */
081    public static final Class<IncompatibleClassChangeError> INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class;
082
083    /** Exception class: AbstractMethodError. */
084    public static final Class<AbstractMethodError> ABSTRACT_METHOD_ERROR = AbstractMethodError.class;
085
086    /** Exception class: IllegalAccessError. */
087    public static final Class<IllegalAccessError> ILLEGAL_ACCESS_ERROR = IllegalAccessError.class;
088
089    /** Exception class: InstantiationError. */
090    public static final Class<InstantiationError> INSTANTIATION_ERROR = InstantiationError.class;
091
092    /** Exception class: NoSuchFieldError. */
093    public static final Class<NoSuchFieldError> NO_SUCH_FIELD_ERROR = NoSuchFieldError.class;
094
095    /** Exception class: NoSuchMethodError. */
096    public static final Class<NoSuchMethodError> NO_SUCH_METHOD_ERROR = NoSuchMethodError.class;
097
098    /** Exception class: NoClassDefFoundError. */
099    public static final Class<NoClassDefFoundError> NO_CLASS_DEF_FOUND_ERROR = NoClassDefFoundError.class;
100
101    /** Exception class: UnsatisfiedLinkError. */
102    public static final Class<UnsatisfiedLinkError> UNSATISFIED_LINK_ERROR = UnsatisfiedLinkError.class;
103
104    /** Exception class: VerifyError. */
105    public static final Class<VerifyError> VERIFY_ERROR = VerifyError.class;
106    /* UnsupportedClassVersionError is new in JDK 1.2 */
107//    public static final Class UnsupportedClassVersionError = UnsupportedClassVersionError.class;
108
109    /** Exception class: NullPointerException. */
110    public static final Class<NullPointerException> NULL_POINTER_EXCEPTION = NullPointerException.class;
111
112    /**
113     * Run-Time Exceptions.
114     */
115
116    /** Exception class: ArrayIndexOutOfBoundsException. */
117    public static final Class<ArrayIndexOutOfBoundsException> ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION = ArrayIndexOutOfBoundsException.class;
118
119    /** Exception class: ArithmeticException. */
120    public static final Class<ArithmeticException> ARITHMETIC_EXCEPTION = ArithmeticException.class;
121
122    /** Exception class: NegativeArraySizeException. */
123    public static final Class<NegativeArraySizeException> NEGATIVE_ARRAY_SIZE_EXCEPTION = NegativeArraySizeException.class;
124
125    /** Exception class: ClassCastException. */
126    public static final Class<ClassCastException> CLASS_CAST_EXCEPTION = ClassCastException.class;
127
128    /** Exception class: IllegalMonitorStateException. */
129    public static final Class<IllegalMonitorStateException> ILLEGAL_MONITOR_STATE = IllegalMonitorStateException.class;
130
131    /**
132     * Pre-defined exception arrays according to chapters 5.1-5.4 of the Java Virtual Machine Specification
133     */
134    private static final Class<?>[] EXCS_CLASS_AND_INTERFACE_RESOLUTION = {NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR,
135        EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR}; // Chapter 5.1
136
137    private static final Class<?>[] EXCS_FIELD_AND_METHOD_RESOLUTION = {NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR}; // Chapter 5.2
138
139    /**
140     * Empty array.
141     */
142    private static final Class<?>[] EXCS_INTERFACE_METHOD_RESOLUTION = new Class[0]; // Chapter 5.3 (as below)
143
144    /**
145     * Empty array.
146     */
147    private static final Class<?>[] EXCS_STRING_RESOLUTION = new Class[0];
148
149    // Chapter 5.4 (no errors but the ones that _always_ could happen! How stupid.)
150    private static final Class<?>[] EXCS_ARRAY_EXCEPTION = {NULL_POINTER_EXCEPTION, ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION};
151
152    /**
153     * Creates a copy of the specified Exception Class array combined with any additional Exception classes.
154     *
155     * @param type The basic array type.
156     * @param extraClasses additional classes, if any.
157     * @return The merged array.
158     */
159    public static Class<?>[] createExceptions(final EXCS type, final Class<?>... extraClasses) {
160        switch (type) {
161        case EXCS_CLASS_AND_INTERFACE_RESOLUTION:
162            return mergeExceptions(EXCS_CLASS_AND_INTERFACE_RESOLUTION, extraClasses);
163        case EXCS_ARRAY_EXCEPTION:
164            return mergeExceptions(EXCS_ARRAY_EXCEPTION, extraClasses);
165        case EXCS_FIELD_AND_METHOD_RESOLUTION:
166            return mergeExceptions(EXCS_FIELD_AND_METHOD_RESOLUTION, extraClasses);
167        case EXCS_INTERFACE_METHOD_RESOLUTION:
168            return mergeExceptions(EXCS_INTERFACE_METHOD_RESOLUTION, extraClasses);
169        case EXCS_STRING_RESOLUTION:
170            return mergeExceptions(EXCS_STRING_RESOLUTION, extraClasses);
171        default:
172            throw new AssertionError("Cannot happen; unexpected enum value: " + type);
173        }
174    }
175
176    // helper method to merge exception class arrays
177    private static Class<?>[] mergeExceptions(final Class<?>[] input, final Class<?>... extraClasses) {
178        return ArrayUtils.addAll(input, extraClasses);
179    }
180
181    /** Private constructor - utility class. */
182    public ExceptionConst() {
183    }
184
185}