1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal;
  23 
  24 /**
  25  * Exception constants.
  26  * @since 6.0 (intended to replace the InstructionConstant interface)
  27  */
  28 public final class ExceptionConst {
  29 
  30     /** The mother of all exceptions
  31      */
  32     public static final Class<Throwable> THROWABLE = Throwable.class;
  33     /** Super class of any run-time exception
  34      */
  35     public static final Class<RuntimeException> RUNTIME_EXCEPTION = RuntimeException.class;
  36     /** Super class of any linking exception (aka Linkage Error)
  37      */
  38     public static final Class<LinkageError> LINKING_EXCEPTION = LinkageError.class;
  39     /** Linking Exceptions
  40      */
  41     public static final Class<ClassCircularityError> CLASS_CIRCULARITY_ERROR = ClassCircularityError.class;
  42     public static final Class<ClassFormatError> CLASS_FORMAT_ERROR = ClassFormatError.class;
  43     public static final Class<ExceptionInInitializerError> EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class;
  44     public static final Class<IncompatibleClassChangeError> INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class;
  45     public static final Class<AbstractMethodError> ABSTRACT_METHOD_ERROR = AbstractMethodError.class;
  46     public static final Class<IllegalAccessError> ILLEGAL_ACCESS_ERROR = IllegalAccessError.class;
  47     public static final Class<InstantiationError> INSTANTIATION_ERROR = InstantiationError.class;
  48     public static final Class<NoSuchFieldError> NO_SUCH_FIELD_ERROR = NoSuchFieldError.class;
  49     public static final Class<NoSuchMethodError> NO_SUCH_METHOD_ERROR = NoSuchMethodError.class;
  50     public static final Class<NoClassDefFoundError> NO_CLASS_DEF_FOUND_ERROR = NoClassDefFoundError.class;
  51     public static final Class<UnsatisfiedLinkError> UNSATISFIED_LINK_ERROR = UnsatisfiedLinkError.class;
  52     public static final Class<VerifyError> VERIFY_ERROR = VerifyError.class;
  53     /* UnsupportedClassVersionError is new in JDK 1.2 */
  54 //    public static final Class UnsupportedClassVersionError = UnsupportedClassVersionError.class;
  55     /** Run-Time Exceptions
  56      */
  57     public static final Class<NullPointerException> NULL_POINTER_EXCEPTION = NullPointerException.class;
  58     public static final Class<ArrayIndexOutOfBoundsException> ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION
  59                                                             = ArrayIndexOutOfBoundsException.class;
  60     public static final Class<ArithmeticException> ARITHMETIC_EXCEPTION = ArithmeticException.class;
  61     public static final Class<NegativeArraySizeException> NEGATIVE_ARRAY_SIZE_EXCEPTION = NegativeArraySizeException.class;
  62     public static final Class<ClassCastException> CLASS_CAST_EXCEPTION = ClassCastException.class;
  63     public static final Class<IllegalMonitorStateException> ILLEGAL_MONITOR_STATE = IllegalMonitorStateException.class;
  64 
  65     /**
  66      * Pre-defined exception arrays according to chapters 5.1-5.4 of the Java Virtual
  67      * Machine Specification
  68      */
  69     private static final Class<?>[] EXCS_CLASS_AND_INTERFACE_RESOLUTION = {
  70             NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR,
  71             EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR
  72     }; // Chapter 5.1
  73     private static final Class<?>[] EXCS_FIELD_AND_METHOD_RESOLUTION = {
  74             NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR
  75     }; // Chapter 5.2
  76     private static final Class<?>[] EXCS_INTERFACE_METHOD_RESOLUTION = new Class<?>[0]; // Chapter 5.3 (as below)
  77     private static final Class<?>[] EXCS_STRING_RESOLUTION = new Class<?>[0];
  78     // Chapter 5.4 (no errors but the ones that _always_ could happen! How stupid.)
  79     private static final Class<?>[] EXCS_ARRAY_EXCEPTION = {
  80             NULL_POINTER_EXCEPTION, ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION
  81     };
  82 
  83     /**
  84      * Enum corresponding to the various Exception Class arrays,
  85      * used by {@link ExceptionConst#createExceptions(EXCS, Class...)}
  86      */
  87     public enum EXCS {
  88         EXCS_CLASS_AND_INTERFACE_RESOLUTION,
  89         EXCS_FIELD_AND_METHOD_RESOLUTION,
  90         EXCS_INTERFACE_METHOD_RESOLUTION,
  91         EXCS_STRING_RESOLUTION,
  92         EXCS_ARRAY_EXCEPTION,
  93     }
  94 
  95     // helper method to merge exception class arrays
  96     private static Class<?>[] mergeExceptions(final Class<?>[] input, final Class<?> ... extraClasses) {
  97         final int extraLen = extraClasses == null ? 0 : extraClasses.length;
  98         final Class<?>[] excs = new Class<?>[input.length + extraLen];
  99         System.arraycopy(input, 0, excs, 0, input.length);
 100         if (extraLen > 0) {
 101             System.arraycopy(extraClasses, 0, excs, input.length, extraLen);
 102         }
 103         return excs;
 104     }
 105 
 106     /**
 107      * Creates a copy of the specified Exception Class array combined with any additional Exception classes.
 108      * @param type the basic array type
 109      * @param extraClasses additional classes, if any
 110      * @return the merged array
 111      */
 112     public static Class<?>[] createExceptions(final EXCS type, final Class<?> ... extraClasses) {
 113         switch (type) {
 114         case EXCS_CLASS_AND_INTERFACE_RESOLUTION:
 115             return mergeExceptions(EXCS_CLASS_AND_INTERFACE_RESOLUTION, extraClasses);
 116         case EXCS_ARRAY_EXCEPTION:
 117             return mergeExceptions(EXCS_ARRAY_EXCEPTION, extraClasses);
 118         case EXCS_FIELD_AND_METHOD_RESOLUTION:
 119             return mergeExceptions(EXCS_FIELD_AND_METHOD_RESOLUTION, extraClasses);
 120         case EXCS_INTERFACE_METHOD_RESOLUTION:
 121             return mergeExceptions(EXCS_INTERFACE_METHOD_RESOLUTION, extraClasses);
 122         case EXCS_STRING_RESOLUTION:
 123             return mergeExceptions(EXCS_STRING_RESOLUTION, extraClasses);
 124         default:
 125             throw new AssertionError("Cannot happen; unexpected enum value: " + type);
 126         }
 127     }
 128 
 129 
 130 }