KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > IllegalClassException


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 org.apache.commons.lang;
17
18 /**
19  * <p>Thrown when an object is an instance of an unexpected type (a class or interface).
20  * This exception supplements the standard <code>IllegalArgumentException</code>
21  * by providing a more semantically rich description of the problem.</p>
22  *
23  * <p><code>IllegalClassException</code> represents the case where a method takes
24  * in a genericly typed parameter like Object (typically because it has to due to some
25  * other interface it implements), but this implementation only actually accepts a specific
26  * type, for example String. This exception would be used in place of
27  * <code>IllegalArgumentException</code>, yet it still extends it.</p>
28  *
29  * <pre>
30  * public void foo(Object obj) {
31  * if (obj instanceof String == false) {
32  * throw new IllegalClassException(String.class, obj);
33  * }
34  * // do something with the string
35  * }
36  * </pre>
37  *
38  * @author Matthew Hawthorne
39  * @author Gary Gregory
40  * @author Stephen Colebourne
41  * @since 2.0
42  * @version $Id: IllegalClassException.java 161243 2005-04-14 04:30:28Z ggregory $
43  */

44 public class IllegalClassException extends IllegalArgumentException JavaDoc {
45
46     /**
47      * <p>Instantiates with the expected type, and actual object.</p>
48      *
49      * @param expected the expected type
50      * @param actual the actual object
51      * @since 2.1
52      */

53     public IllegalClassException(Class JavaDoc expected, Object JavaDoc actual) {
54         super(
55             "Expected: "
56                 + safeGetClassName(expected)
57                 + ", actual: "
58                 + (actual == null ? "null" : actual.getClass().getName()));
59     }
60
61     /**
62      * <p>Instantiates with the expected and actual types.</p>
63      *
64      * @param expected the expected type
65      * @param actual the actual type
66      */

67     public IllegalClassException(Class JavaDoc expected, Class JavaDoc actual) {
68         super(
69             "Expected: "
70                 + safeGetClassName(expected)
71                 + ", actual: "
72                 + safeGetClassName(actual));
73     }
74
75     /**
76      * <p>Instantiates with the specified message.</p>
77      *
78      * @param message the exception message
79      */

80     public IllegalClassException(String JavaDoc message) {
81         super(message);
82     }
83
84     /**
85      * <p>Returns the class name or <code>null</code> if the class is
86      * <code>null</code>.</p>
87      *
88      * @param cls a <code>Class</code>
89      * @return the name of <code>cls</code>, or <code>null</code> if if <code>cls</code> is <code>null</code>.
90      */

91     private static final String JavaDoc safeGetClassName(Class JavaDoc cls) {
92         return cls == null ? null : cls.getName();
93     }
94
95 }
96
Popular Tags