ExceptionConst.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel;

  18. import org.apache.commons.lang3.ArrayUtils;

  19. /**
  20.  * Exception constants.
  21.  *
  22.  * @since 6.0 (intended to replace the InstructionConstant interface)
  23.  */
  24. public final class ExceptionConst {

  25.     /**
  26.      * Enum corresponding to the various Exception Class arrays, used by
  27.      * {@link ExceptionConst#createExceptions(EXCS, Class...)}
  28.      */
  29.     public enum EXCS {
  30.         EXCS_CLASS_AND_INTERFACE_RESOLUTION, EXCS_FIELD_AND_METHOD_RESOLUTION, EXCS_INTERFACE_METHOD_RESOLUTION, EXCS_STRING_RESOLUTION, EXCS_ARRAY_EXCEPTION,
  31.     }

  32.     /**
  33.      * The mother of all exceptions
  34.      */
  35.     public static final Class<Throwable> THROWABLE = Throwable.class;

  36.     /**
  37.      * Super class of any run-time exception
  38.      */
  39.     public static final Class<RuntimeException> RUNTIME_EXCEPTION = RuntimeException.class;

  40.     /**
  41.      * Super class of any linking exception (aka Linkage Error)
  42.      */
  43.     public static final Class<LinkageError> LINKING_EXCEPTION = LinkageError.class;
  44.     /**
  45.      * Linking Exceptions
  46.      */
  47.     public static final Class<ClassCircularityError> CLASS_CIRCULARITY_ERROR = ClassCircularityError.class;
  48.     public static final Class<ClassFormatError> CLASS_FORMAT_ERROR = ClassFormatError.class;
  49.     public static final Class<ExceptionInInitializerError> EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class;
  50.     public static final Class<IncompatibleClassChangeError> INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class;
  51.     public static final Class<AbstractMethodError> ABSTRACT_METHOD_ERROR = AbstractMethodError.class;
  52.     public static final Class<IllegalAccessError> ILLEGAL_ACCESS_ERROR = IllegalAccessError.class;
  53.     public static final Class<InstantiationError> INSTANTIATION_ERROR = InstantiationError.class;
  54.     public static final Class<NoSuchFieldError> NO_SUCH_FIELD_ERROR = NoSuchFieldError.class;
  55.     public static final Class<NoSuchMethodError> NO_SUCH_METHOD_ERROR = NoSuchMethodError.class;
  56.     public static final Class<NoClassDefFoundError> NO_CLASS_DEF_FOUND_ERROR = NoClassDefFoundError.class;
  57.     public static final Class<UnsatisfiedLinkError> UNSATISFIED_LINK_ERROR = UnsatisfiedLinkError.class;

  58.     public static final Class<VerifyError> VERIFY_ERROR = VerifyError.class;
  59.     /* UnsupportedClassVersionError is new in JDK 1.2 */
  60. //    public static final Class UnsupportedClassVersionError = UnsupportedClassVersionError.class;
  61.     /**
  62.      * Run-Time Exceptions
  63.      */
  64.     public static final Class<NullPointerException> NULL_POINTER_EXCEPTION = NullPointerException.class;
  65.     public static final Class<ArrayIndexOutOfBoundsException> ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION = ArrayIndexOutOfBoundsException.class;
  66.     public static final Class<ArithmeticException> ARITHMETIC_EXCEPTION = ArithmeticException.class;
  67.     public static final Class<NegativeArraySizeException> NEGATIVE_ARRAY_SIZE_EXCEPTION = NegativeArraySizeException.class;
  68.     public static final Class<ClassCastException> CLASS_CAST_EXCEPTION = ClassCastException.class;

  69.     public static final Class<IllegalMonitorStateException> ILLEGAL_MONITOR_STATE = IllegalMonitorStateException.class;
  70.     /**
  71.      * Pre-defined exception arrays according to chapters 5.1-5.4 of the Java Virtual Machine Specification
  72.      */
  73.     private static final Class<?>[] EXCS_CLASS_AND_INTERFACE_RESOLUTION = {NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR,
  74.         EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR}; // Chapter 5.1

  75.     private static final Class<?>[] EXCS_FIELD_AND_METHOD_RESOLUTION = {NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR}; // Chapter 5.2

  76.     /**
  77.      * Empty array.
  78.      */
  79.     private static final Class<?>[] EXCS_INTERFACE_METHOD_RESOLUTION = new Class[0]; // Chapter 5.3 (as below)

  80.     /**
  81.      * Empty array.
  82.      */
  83.     private static final Class<?>[] EXCS_STRING_RESOLUTION = new Class[0];

  84.     // Chapter 5.4 (no errors but the ones that _always_ could happen! How stupid.)
  85.     private static final Class<?>[] EXCS_ARRAY_EXCEPTION = {NULL_POINTER_EXCEPTION, ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION};

  86.     /**
  87.      * Creates a copy of the specified Exception Class array combined with any additional Exception classes.
  88.      *
  89.      * @param type the basic array type
  90.      * @param extraClasses additional classes, if any
  91.      * @return the merged array
  92.      */
  93.     public static Class<?>[] createExceptions(final EXCS type, final Class<?>... extraClasses) {
  94.         switch (type) {
  95.         case EXCS_CLASS_AND_INTERFACE_RESOLUTION:
  96.             return mergeExceptions(EXCS_CLASS_AND_INTERFACE_RESOLUTION, extraClasses);
  97.         case EXCS_ARRAY_EXCEPTION:
  98.             return mergeExceptions(EXCS_ARRAY_EXCEPTION, extraClasses);
  99.         case EXCS_FIELD_AND_METHOD_RESOLUTION:
  100.             return mergeExceptions(EXCS_FIELD_AND_METHOD_RESOLUTION, extraClasses);
  101.         case EXCS_INTERFACE_METHOD_RESOLUTION:
  102.             return mergeExceptions(EXCS_INTERFACE_METHOD_RESOLUTION, extraClasses);
  103.         case EXCS_STRING_RESOLUTION:
  104.             return mergeExceptions(EXCS_STRING_RESOLUTION, extraClasses);
  105.         default:
  106.             throw new AssertionError("Cannot happen; unexpected enum value: " + type);
  107.         }
  108.     }

  109.     // helper method to merge exception class arrays
  110.     private static Class<?>[] mergeExceptions(final Class<?>[] input, final Class<?>... extraClasses) {
  111.         return ArrayUtils.addAll(input, extraClasses);
  112.     }

  113. }