001: package org.apache.velocity.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.lang.reflect.Constructor;
023: import java.lang.reflect.Method;
024:
025: /**
026: * Use this to create a new Exception. This will run under JDK 1.3 or greater.
027: * However, it running under JDK 1.4 it will set the cause.
028: *
029: * @author <a href="mailto:isidore@setgame.com">Llewellyn Falco</a>
030: */
031: public class ExceptionUtils {
032: private static boolean causesAllowed = true;
033:
034: /**
035: * Create a new RuntimeException, setting the cause if possible.
036: * @param message
037: * @param cause
038: * @return A runtime exception object.
039: */
040: public static RuntimeException createRuntimeException(
041: String message, Throwable cause) {
042: return (RuntimeException) createWithCause(
043: RuntimeException.class, message, cause);
044: }
045:
046: /**
047: * Create a new Exception, setting the cause if possible.
048: * @param clazz
049: * @param message
050: * @param cause
051: * @return A Throwable.
052: */
053: public static Throwable createWithCause(Class clazz,
054: String message, Throwable cause) {
055: Throwable re = null;
056: if (causesAllowed) {
057: try {
058: Constructor constructor = clazz
059: .getConstructor(new Class[] { String.class,
060: Throwable.class });
061: re = (Throwable) constructor.newInstance(new Object[] {
062: message, cause });
063: } catch (RuntimeException e) {
064: throw e;
065: } catch (Exception e) {
066: causesAllowed = false;
067: }
068: }
069: if (re == null) {
070: try {
071: Constructor constructor = clazz
072: .getConstructor(new Class[] { String.class });
073: re = (Throwable) constructor
074: .newInstance(new Object[] { message
075: + " caused by " + cause });
076: } catch (RuntimeException e) {
077: throw e;
078: } catch (Exception e) {
079: throw new RuntimeException("Error caused " + e); // should be impossible
080: }
081: }
082: return re;
083: }
084:
085: /**
086: * Set the cause of the Exception. Will detect if this is not allowed.
087: * @param onObject
088: * @param cause
089: */
090: public static void setCause(Throwable onObject, Throwable cause) {
091: if (causesAllowed) {
092: try {
093: Method method = onObject.getClass().getMethod(
094: "initCause", new Class[] { Throwable.class });
095: method.invoke(onObject, new Object[] { cause });
096: } catch (RuntimeException e) {
097: throw e;
098: } catch (Exception e) {
099: causesAllowed = false;
100: }
101: }
102: }
103: }
|