001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel; 020 021import org.apache.commons.lang3.ArrayUtils; 022 023/** 024 * Exception constants. 025 * 026 * @since 6.0 (intended to replace the InstructionConstant interface) 027 */ 028public final class ExceptionConst { 029 030 /** 031 * Enum corresponding to the various Exception Class arrays, used by 032 * {@link ExceptionConst#createExceptions(EXCS, Class...)}. 033 */ 034 public enum EXCS { 035 036 /** Exception class and interface resolution. */ 037 EXCS_CLASS_AND_INTERFACE_RESOLUTION, 038 039 /** Exception field and method resolution. */ 040 EXCS_FIELD_AND_METHOD_RESOLUTION, 041 042 /** Exception interface method resolution. */ 043 EXCS_INTERFACE_METHOD_RESOLUTION, 044 045 /** Exception string resolution. */ 046 EXCS_STRING_RESOLUTION, 047 048 /** Exception array exception. */ 049 EXCS_ARRAY_EXCEPTION, 050 } 051 052 /** Private constructor - utility class. */ 053 public ExceptionConst() { 054 } 055 056 /** 057 * The mother of all exceptions 058 */ 059 public static final Class<Throwable> THROWABLE = Throwable.class; 060 061 /** 062 * Super class of any run-time exception 063 */ 064 public static final Class<RuntimeException> RUNTIME_EXCEPTION = RuntimeException.class; 065 066 /** 067 * Super class of any linking exception (aka Linkage Error) 068 */ 069 public static final Class<LinkageError> LINKING_EXCEPTION = LinkageError.class; 070 071 /** 072 * Linking Exceptions. 073 */ 074 075 /** Exception class: ClassCircularityError. */ 076 public static final Class<ClassCircularityError> CLASS_CIRCULARITY_ERROR = ClassCircularityError.class; 077 078 /** Exception class: ClassFormatError. */ 079 public static final Class<ClassFormatError> CLASS_FORMAT_ERROR = ClassFormatError.class; 080 081 /** Exception class: ExceptionInInitializerError. */ 082 public static final Class<ExceptionInInitializerError> EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class; 083 084 /** Exception class: IncompatibleClassChangeError. */ 085 public static final Class<IncompatibleClassChangeError> INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class; 086 087 /** Exception class: AbstractMethodError. */ 088 public static final Class<AbstractMethodError> ABSTRACT_METHOD_ERROR = AbstractMethodError.class; 089 090 /** Exception class: IllegalAccessError. */ 091 public static final Class<IllegalAccessError> ILLEGAL_ACCESS_ERROR = IllegalAccessError.class; 092 093 /** Exception class: InstantiationError. */ 094 public static final Class<InstantiationError> INSTANTIATION_ERROR = InstantiationError.class; 095 096 /** Exception class: NoSuchFieldError. */ 097 public static final Class<NoSuchFieldError> NO_SUCH_FIELD_ERROR = NoSuchFieldError.class; 098 099 /** 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}