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