View Javadoc

1   /*******************************************************************************
2    * Copyright 2013 André Rouél and Dominik Seichter
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  package net.sf.qualitycheck.exception;
17  
18  import javax.annotation.Nullable;
19  
20  /**
21   * Thrown to indicate that a class cannot be instantiated.
22   * 
23   * @author André Rouél
24   * @author Dominik Seichter
25   */
26  public class RuntimeInstantiationException extends RuntimeException {
27  
28  	private static final long serialVersionUID = 7304261330061136504L;
29  
30  	/**
31  	 * Default message to indicate that the a given class cannot be instantiated.
32  	 */
33  	protected static final String DEFAULT_MESSAGE = "The passed class cannot be instantiated.";
34  
35  	/**
36  	 * Message to indicate that the the given class with <em>name</em> cannot be instantiated.
37  	 */
38  	protected static final String MESSAGE_WITH_NAME = "The passed class '%s' cannot be instantiated.";
39  
40  	/**
41  	 * Determines the message to be used, depending on the passed argument name. If if the given argument name is
42  	 * {@code null} or empty {@code DEFAULT_MESSAGE} will be returned, otherwise a formatted {@code MESSAGE_WITH_NAME}
43  	 * with the passed name.
44  	 * 
45  	 * @param className
46  	 *            the name of the passed {@link Class}
47  	 * @return {@code DEFAULT_MESSAGE} if the given argument name is {@code null} or empty, otherwise a formatted
48  	 *         {@code MESSAGE_WITH_NAME}
49  	 */
50  	private static String determineMessage(@Nullable final String className) {
51  		return className != null && !className.isEmpty() ? format(className) : DEFAULT_MESSAGE;
52  	}
53  
54  	/**
55  	 * Returns the formatted string {@link RuntimeInstantiationException#MESSAGE_WITH_NAME} with the given
56  	 * {@code argumentName}.
57  	 * 
58  	 * @param argumentName
59  	 *            the name of the passed argument
60  	 * @return a formatted string of message with the given argument name
61  	 */
62  	private static String format(@Nullable final String argumentName) {
63  		return String.format(MESSAGE_WITH_NAME, argumentName);
64  	}
65  
66  	/**
67  	 * Constructs an {@code IllegalNullArgumentException} with the default message
68  	 * {@link RuntimeInstantiationException#DEFAULT_MESSAGE}.
69  	 */
70  	public RuntimeInstantiationException() {
71  		super(DEFAULT_MESSAGE);
72  	}
73  
74  	/**
75  	 * Constructs an {@code RuntimeInstantiationException} with the message
76  	 * {@link RuntimeInstantiationException#MESSAGE_WITH_NAME} including the given name of the argument as string
77  	 * representation.
78  	 * 
79  	 * @param className
80  	 *            the name of the {@link Class}
81  	 */
82  	public RuntimeInstantiationException(@Nullable final String className) {
83  		super(determineMessage(className));
84  	}
85  
86  	/**
87  	 * Constructs a new exception with the message {@link RuntimeInstantiationException#MESSAGE_WITH_NAME} including the
88  	 * given name as string representation and cause.
89  	 * 
90  	 * @param className
91  	 *            the name of the {@link Class}
92  	 * @param cause
93  	 *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
94  	 *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
95  	 */
96  	public RuntimeInstantiationException(@Nullable final String className, @Nullable final Throwable cause) {
97  		super(determineMessage(className), cause);
98  	}
99  
100 	/**
101 	 * Constructs a new exception with the default message {@link RuntimeInstantiationException#DEFAULT_MESSAGE}.
102 	 * 
103 	 * @param cause
104 	 *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
105 	 *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
106 	 */
107 	public RuntimeInstantiationException(@Nullable final Throwable cause) {
108 		super(DEFAULT_MESSAGE, cause);
109 	}
110 
111 }