• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
18 package org.apache.bcel;
19 
20 /**
21  * Exception constants.
22  * @since 6.0 (intended to replace the InstructionConstant interface)
23  */
24 public final class ExceptionConst {
25 
26     /** The mother of all exceptions
27      */
28     public static final Class<Throwable> THROWABLE = Throwable.class;
29     /** Super class of any run-time exception
30      */
31     public static final Class<RuntimeException> RUNTIME_EXCEPTION = RuntimeException.class;
32     /** Super class of any linking exception (aka Linkage Error)
33      */
34     public static final Class<LinkageError> LINKING_EXCEPTION = LinkageError.class;
35     /** Linking Exceptions
36      */
37     public static final Class<ClassCircularityError> CLASS_CIRCULARITY_ERROR = ClassCircularityError.class;
38     public static final Class<ClassFormatError> CLASS_FORMAT_ERROR = ClassFormatError.class;
39     public static final Class<ExceptionInInitializerError> EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class;
40     public static final Class<IncompatibleClassChangeError> INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class;
41     public static final Class<AbstractMethodError> ABSTRACT_METHOD_ERROR = AbstractMethodError.class;
42     public static final Class<IllegalAccessError> ILLEGAL_ACCESS_ERROR = IllegalAccessError.class;
43     public static final Class<InstantiationError> INSTANTIATION_ERROR = InstantiationError.class;
44     public static final Class<NoSuchFieldError> NO_SUCH_FIELD_ERROR = NoSuchFieldError.class;
45     public static final Class<NoSuchMethodError> NO_SUCH_METHOD_ERROR = NoSuchMethodError.class;
46     public static final Class<NoClassDefFoundError> NO_CLASS_DEF_FOUND_ERROR = NoClassDefFoundError.class;
47     public static final Class<UnsatisfiedLinkError> UNSATISFIED_LINK_ERROR = UnsatisfiedLinkError.class;
48     public static final Class<VerifyError> VERIFY_ERROR = VerifyError.class;
49     /* UnsupportedClassVersionError is new in JDK 1.2 */
50 //    public static final Class UnsupportedClassVersionError = UnsupportedClassVersionError.class;
51     /** Run-Time Exceptions
52      */
53     public static final Class<NullPointerException> NULL_POINTER_EXCEPTION = NullPointerException.class;
54     public static final Class<ArrayIndexOutOfBoundsException> ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION
55                                                             = ArrayIndexOutOfBoundsException.class;
56     public static final Class<ArithmeticException> ARITHMETIC_EXCEPTION = ArithmeticException.class;
57     public static final Class<NegativeArraySizeException> NEGATIVE_ARRAY_SIZE_EXCEPTION = NegativeArraySizeException.class;
58     public static final Class<ClassCastException> CLASS_CAST_EXCEPTION = ClassCastException.class;
59     public static final Class<IllegalMonitorStateException> ILLEGAL_MONITOR_STATE = IllegalMonitorStateException.class;
60 
61     /**
62      * Pre-defined exception arrays according to chapters 5.1-5.4 of the Java Virtual
63      * Machine Specification
64      */
65     private static final Class<?>[] EXCS_CLASS_AND_INTERFACE_RESOLUTION = {
66             NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR,
67             EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR
68     }; // Chapter 5.1
69     private static final Class<?>[] EXCS_FIELD_AND_METHOD_RESOLUTION = {
70             NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR
71     }; // Chapter 5.2
72     private static final Class<?>[] EXCS_INTERFACE_METHOD_RESOLUTION = new Class[0]; // Chapter 5.3 (as below)
73     private static final Class<?>[] EXCS_STRING_RESOLUTION = new Class[0];
74     // Chapter 5.4 (no errors but the ones that _always_ could happen! How stupid.)
75     private static final Class<?>[] EXCS_ARRAY_EXCEPTION = {
76             NULL_POINTER_EXCEPTION, ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION
77     };
78 
79     /**
80      * Enum corresponding to the various Exception Class arrays,
81      * used by {@link ExceptionConst#createExceptions(EXCS, Class...)}
82      */
83     public enum EXCS {
84         EXCS_CLASS_AND_INTERFACE_RESOLUTION,
85         EXCS_FIELD_AND_METHOD_RESOLUTION,
86         EXCS_INTERFACE_METHOD_RESOLUTION,
87         EXCS_STRING_RESOLUTION,
88         EXCS_ARRAY_EXCEPTION,
89     }
90 
91     // helper method to merge exception class arrays
mergeExceptions(final Class<?>[] input, final Class<?> ... extraClasses)92     private static Class<?>[] mergeExceptions(final Class<?>[] input, final Class<?> ... extraClasses) {
93         final int extraLen = extraClasses == null ? 0 : extraClasses.length;
94         final Class<?>[] excs = new Class<?>[input.length + extraLen];
95         System.arraycopy(input, 0, excs, 0, input.length);
96         if (extraLen > 0) {
97             System.arraycopy(extraClasses, 0, excs, input.length, extraLen);
98         }
99         return excs;
100     }
101 
102     /**
103      * Creates a copy of the specified Exception Class array combined with any additional Exception classes.
104      * @param type the basic array type
105      * @param extraClasses additional classes, if any
106      * @return the merged array
107      */
createExceptions(final EXCS type, final Class<?> ... extraClasses)108     public static Class<?>[] createExceptions(final EXCS type, final Class<?> ... extraClasses) {
109         switch (type) {
110         case EXCS_CLASS_AND_INTERFACE_RESOLUTION:
111             return mergeExceptions(EXCS_CLASS_AND_INTERFACE_RESOLUTION, extraClasses);
112         case EXCS_ARRAY_EXCEPTION:
113             return mergeExceptions(EXCS_ARRAY_EXCEPTION, extraClasses);
114         case EXCS_FIELD_AND_METHOD_RESOLUTION:
115             return mergeExceptions(EXCS_FIELD_AND_METHOD_RESOLUTION, extraClasses);
116         case EXCS_INTERFACE_METHOD_RESOLUTION:
117             return mergeExceptions(EXCS_INTERFACE_METHOD_RESOLUTION, extraClasses);
118         case EXCS_STRING_RESOLUTION:
119             return mergeExceptions(EXCS_STRING_RESOLUTION, extraClasses);
120         default:
121             throw new AssertionError("Cannot happen; unexpected enum value: " + type);
122         }
123     }
124 
125 
126 }
127