source: src/main/java/agents/org/apache/commons/lang/IllegalClassException.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 3.6 KB
Line 
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 */
17package agents.org.apache.commons.lang;
18
19/**
20 * <p>Thrown when an object is an instance of an unexpected type (a class or interface).
21 * This exception supplements the standard <code>IllegalArgumentException</code>
22 * by providing a more semantically rich description of the problem.</p>
23 *
24 * <p><code>IllegalClassException</code> represents the case where a method takes
25 * in a genericly typed parameter like Object (typically because it has to due to some
26 * other interface it implements), but this implementation only actually accepts a specific
27 * type, for example String. This exception would be used in place of
28 * <code>IllegalArgumentException</code>, yet it still extends it.</p>
29 *
30 * <pre>
31 * public void foo(Object obj) {
32 * if (obj instanceof String == false) {
33 * throw new IllegalClassException(String.class, obj);
34 * }
35 * // do something with the string
36 * }
37 * </pre>
38 *
39 * @author Apache Software Foundation
40 * @author Matthew Hawthorne
41 * @author Gary Gregory
42 * @since 2.0
43 * @version $Id: IllegalClassException.java 905636 2010-02-02 14:03:32Z niallp $
44 */
45public class IllegalClassException extends IllegalArgumentException {
46
47 /**
48 * Required for serialization support.
49 *
50 * @see java.io.Serializable
51 */
52 private static final long serialVersionUID = 8063272569377254819L;
53
54 /**
55 * <p>Instantiates with the expected type, and actual object.</p>
56 *
57 * @param expected the expected type
58 * @param actual the actual object
59 * @since 2.1
60 */
61 public IllegalClassException(Class expected, Object actual) {
62 super(
63 "Expected: "
64 + safeGetClassName(expected)
65 + ", actual: "
66 + (actual == null ? "null" : actual.getClass().getName()));
67 }
68
69 /**
70 * <p>Instantiates with the expected and actual types.</p>
71 *
72 * @param expected the expected type
73 * @param actual the actual type
74 */
75 public IllegalClassException(Class expected, Class actual) {
76 super(
77 "Expected: "
78 + safeGetClassName(expected)
79 + ", actual: "
80 + safeGetClassName(actual));
81 }
82
83 /**
84 * <p>Instantiates with the specified message.</p>
85 *
86 * @param message the exception message
87 */
88 public IllegalClassException(String message) {
89 super(message);
90 }
91
92 /**
93 * <p>Returns the class name or <code>null</code> if the class is
94 * <code>null</code>.</p>
95 *
96 * @param cls a <code>Class</code>
97 * @return the name of <code>cls</code>, or <code>null</code> if if <code>cls</code> is <code>null</code>.
98 */
99 private static final String safeGetClassName(Class cls) {
100 return cls == null ? null : cls.getName();
101 }
102
103}
Note: See TracBrowser for help on using the repository browser.