001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau; 018 019import static org.apache.juneau.common.utils.StringUtils.*; 020 021import java.text.*; 022 023import org.apache.juneau.common.utils.*; 024import org.apache.juneau.internal.*; 025 026/** 027 * Subclass of runtime exceptions that take in a message and zero or more arguments. 028 * 029 * <h5 class='section'>See Also:</h5><ul> 030 031 * </ul> 032 * 033 * @serial exclude 034 */ 035public class BasicRuntimeException extends RuntimeException { 036 037 //----------------------------------------------------------------------------------------------------------------- 038 // Static 039 //----------------------------------------------------------------------------------------------------------------- 040 041 private static final long serialVersionUID = 1L; 042 043 //----------------------------------------------------------------------------------------------------------------- 044 // Instance 045 //----------------------------------------------------------------------------------------------------------------- 046 047 boolean unmodifiable; 048 String message; 049 050 /** 051 * Constructor. 052 * 053 * @param cause The cause of this exception. 054 * @param message The {@link MessageFormat}-style message. 055 * @param args Optional {@link MessageFormat}-style arguments. 056 */ 057 public BasicRuntimeException(Throwable cause, String message, Object...args) { 058 super(format(message, args), cause); 059 } 060 061 /** 062 * Constructor. 063 * 064 * @param message The {@link MessageFormat}-style message. 065 * @param args Optional {@link MessageFormat}-style arguments. 066 */ 067 public BasicRuntimeException(String message, Object...args) { 068 super(format(message, args)); 069 } 070 071 /** 072 * Constructor. 073 * 074 * @param cause The cause of this exception. 075 */ 076 public BasicRuntimeException(Throwable cause) { 077 super(cause); 078 } 079 080 //----------------------------------------------------------------------------------------------------------------- 081 // Properties 082 //----------------------------------------------------------------------------------------------------------------- 083 084 /** 085 * Specifies whether this bean should be unmodifiable. 086 * <p> 087 * When enabled, attempting to set any properties on this bean will cause an {@link UnsupportedOperationException}. 088 * 089 * @return This object. 090 */ 091 protected BasicRuntimeException setUnmodifiable() { 092 unmodifiable = true; 093 return this; 094 } 095 096 /** 097 * Returns <jk>true</jk> if this bean is unmodifiable. 098 * 099 * @return <jk>true</jk> if this bean is unmodifiable. 100 */ 101 public boolean isUnmodifiable() { 102 return unmodifiable; 103 } 104 105 /** 106 * Throws an {@link UnsupportedOperationException} if the unmodifiable flag is set on this bean. 107 */ 108 protected final void assertModifiable() { 109 if (unmodifiable) 110 throw new UnsupportedOperationException("Bean is read-only"); 111 } 112 113 /** 114 * Same as {@link #getCause()} but searches the throwable chain for an exception of the specified type. 115 * 116 * @param c The throwable type to search for. 117 * @param <T> The throwable type to search for. 118 * @return The exception, or <jk>null</jk> if not found. 119 */ 120 public <T extends Throwable> T getCause(Class<T> c) { 121 return ThrowableUtils.getCause(c, this); 122 } 123 124 /** 125 * Sets the detail message on this exception. 126 * 127 * @param message The message. 128 * @param args The message args. 129 * @return This object. 130 */ 131 public BasicRuntimeException setMessage(String message, Object...args) { 132 assertModifiable(); 133 this.message = format(message, args); 134 return this; 135 } 136 137 @Override /* Throwable */ 138 public String getMessage() { 139 if (message != null) 140 return message; 141 String m = super.getMessage(); 142 if (m == null && getCause() != null) 143 m = getCause().getMessage(); 144 return m; 145 } 146 147 @Override /* Throwable */ 148 public synchronized Throwable fillInStackTrace() { 149 assertModifiable(); 150 return super.fillInStackTrace(); 151 } 152 153 @Override /* Throwable */ 154 public synchronized Throwable initCause(Throwable cause) { 155 assertModifiable(); 156 return super.initCause(cause); 157 } 158 159 @Override /* Throwable */ 160 public void setStackTrace(StackTraceElement[] stackTrace) { 161 assertModifiable(); 162 super.setStackTrace(stackTrace); 163 } 164 165 /** 166 * Returns the caused-by exception if there is one. 167 * 168 * @return The caused-by exception if there is one, or this exception if there isn't. 169 */ 170 public Throwable unwrap() { 171 Throwable t = getCause(); 172 return t == null ? this : t; 173 } 174}