View Javadoc
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  package org.apache.juneau.commons.reflect;
18  
19  import static org.apache.juneau.commons.utils.Utils.*;
20  
21  import java.lang.reflect.*;
22  import java.text.*;
23  
24  /**
25   * A {@link RuntimeException} wrapper around common reflection exceptions.
26   *
27   * <p>
28   * This exception is used to wrap checked exceptions that commonly occur when using Java reflection APIs,
29   * converting them into unchecked exceptions for easier handling in bean-processing code.
30   *
31   * <h5 class='section'>Wrapped Exceptions:</h5>
32   * <ul>
33   * 	<li>{@link InstantiationException}
34   * 	<li>{@link IllegalAccessException}
35   * 	<li>{@link IllegalArgumentException}
36   * 	<li>{@link InvocationTargetException}
37   * 	<li>{@link NoSuchMethodException}
38   * 	<li>{@link SecurityException}
39   * </ul>
40   *
41   * <p>
42   * The exception message can optionally include the class name of the bean that caused the exception,
43   * making it easier to identify the source of reflection errors in complex bean hierarchies.
44   *
45   * <h5 class='section'>Example:</h5>
46   * <p class='bjava'>
47   * 	<jk>try</jk> {
48   * 		Constructor&lt;?&gt; <jv>c</jv> = MyBean.<jk>class</jk>.getConstructor();
49   * 		<jv>c</jv>.newInstance();
50   * 	} <jk>catch</jk> (Exception <jv>e</jv>) {
51   * 		<jk>throw new</jk> BeanRuntimeException(<jv>e</jv>, MyBean.<jk>class</jk>, <js>"Failed to instantiate bean"</js>);
52   * 	}
53   * </p>
54   */
55  public class BeanRuntimeException extends RuntimeException {
56  
57  	private static final long serialVersionUID = 1L;
58  
59  	private static String getMessage(Throwable cause, Class<?> c, String msg, Object...args) {
60  		if (nn(msg))
61  			return (c == null ? "" : cn(c) + ": ") + f(msg, args);
62  		if (nn(cause))
63  			return (c == null ? "" : cn(c) + ": ") + cause.getMessage();
64  		return null;
65  	}
66  
67  	/**
68  	 * Constructor.
69  	 *
70  	 * @param c The class name of the bean that caused the exception.
71  	 * @param message The error message.
72  	 * @param args Arguments passed in to the {@code String.format()} method.
73  	 */
74  	public BeanRuntimeException(Class<?> c, String message, Object...args) {
75  		this(null, c, message, args);
76  	}
77  
78  	/**
79  	 * Constructor.
80  	 *
81  	 * @param message The error message.
82  	 */
83  	public BeanRuntimeException(String message) {
84  		this((Throwable)null, null, message);
85  	}
86  
87  	/**
88  	 * Constructor.
89  	 *
90  	 * @param message The error message.
91  	 * @param args Arguments passed in to the {@code String.format()} method.
92  	 */
93  	public BeanRuntimeException(String message, Object...args) {
94  		this(null, null, message, args);
95  	}
96  
97  	/**
98  	 * Constructor.
99  	 *
100 	 * @param cause The initial cause of the exception.
101 	 */
102 	public BeanRuntimeException(Throwable cause) {
103 		this(cause, null, null);
104 	}
105 
106 	/**
107 	 * Constructor.
108 	 *
109 	 * @param cause The cause of this exception.
110 	 * @param c The class name of the bean that caused the exception.
111 	 * @param message The {@link MessageFormat}-style message.
112 	 * @param args Optional {@link MessageFormat}-style arguments.
113 	 */
114 	public BeanRuntimeException(Throwable cause, Class<?> c, String message, Object...args) {
115 		super(getMessage(cause, c, message, args), cause);
116 	}
117 }