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