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      /**
53       * The mother of all exceptions
54       */
55      public static final Class<Throwable> THROWABLE = Throwable.class;
56  
57      /**
58       * Super class of any run-time exception
59       */
60      public static final Class<RuntimeException> RUNTIME_EXCEPTION = RuntimeException.class;
61  
62      /**
63       * Super class of any linking exception (aka Linkage Error)
64       */
65      public static final Class<LinkageError> LINKING_EXCEPTION = LinkageError.class;
66  
67      /** Exception class: ClassCircularityError. */
68      public static final Class<ClassCircularityError> CLASS_CIRCULARITY_ERROR = ClassCircularityError.class;
69  
70      /**
71       * Linking Exceptions.
72       */
73  
74      /** Exception class: ClassFormatError. */
75      public static final Class<ClassFormatError> CLASS_FORMAT_ERROR = ClassFormatError.class;
76  
77      /** Exception class: ExceptionInInitializerError. */
78      public static final Class<ExceptionInInitializerError> EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class;
79  
80      /** Exception class: IncompatibleClassChangeError. */
81      public static final Class<IncompatibleClassChangeError> INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class;
82  
83      /** Exception class: AbstractMethodError. */
84      public static final Class<AbstractMethodError> ABSTRACT_METHOD_ERROR = AbstractMethodError.class;
85  
86      /** Exception class: IllegalAccessError. */
87      public static final Class<IllegalAccessError> ILLEGAL_ACCESS_ERROR = IllegalAccessError.class;
88  
89      /** Exception class: InstantiationError. */
90      public static final Class<InstantiationError> INSTANTIATION_ERROR = InstantiationError.class;
91  
92      /** Exception class: NoSuchFieldError. */
93      public static final Class<NoSuchFieldError> NO_SUCH_FIELD_ERROR = NoSuchFieldError.class;
94  
95      /** Exception class: NoSuchMethodError. */
96      public static final Class<NoSuchMethodError> NO_SUCH_METHOD_ERROR = NoSuchMethodError.class;
97  
98      /** Exception class: NoClassDefFoundError. */
99      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 }