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: */
017: package org.apache.commons.lang;
018:
019: /**
020: * <p>Thrown when an object is an instance of an unexpected type (a class or interface).
021: * This exception supplements the standard <code>IllegalArgumentException</code>
022: * by providing a more semantically rich description of the problem.</p>
023: *
024: * <p><code>IllegalClassException</code> represents the case where a method takes
025: * in a genericly typed parameter like Object (typically because it has to due to some
026: * other interface it implements), but this implementation only actually accepts a specific
027: * type, for example String. This exception would be used in place of
028: * <code>IllegalArgumentException</code>, yet it still extends it.</p>
029: *
030: * <pre>
031: * public void foo(Object obj) {
032: * if (obj instanceof String == false) {
033: * throw new IllegalClassException(String.class, obj);
034: * }
035: * // do something with the string
036: * }
037: * </pre>
038: *
039: * @author Matthew Hawthorne
040: * @author Gary Gregory
041: * @author Stephen Colebourne
042: * @since 2.0
043: * @version $Id: IllegalClassException.java 437554 2006-08-28 06:21:41Z bayard $
044: */
045: public class IllegalClassException extends IllegalArgumentException {
046:
047: /**
048: * Required for serialization support.
049: *
050: * @see java.io.Serializable
051: */
052: private static final long serialVersionUID = 8063272569377254819L;
053:
054: /**
055: * <p>Instantiates with the expected type, and actual object.</p>
056: *
057: * @param expected the expected type
058: * @param actual the actual object
059: * @since 2.1
060: */
061: public IllegalClassException(Class expected, Object actual) {
062: super ("Expected: "
063: + safeGetClassName(expected)
064: + ", actual: "
065: + (actual == null ? "null" : actual.getClass()
066: .getName()));
067: }
068:
069: /**
070: * <p>Instantiates with the expected and actual types.</p>
071: *
072: * @param expected the expected type
073: * @param actual the actual type
074: */
075: public IllegalClassException(Class expected, Class actual) {
076: super ("Expected: " + safeGetClassName(expected) + ", actual: "
077: + safeGetClassName(actual));
078: }
079:
080: /**
081: * <p>Instantiates with the specified message.</p>
082: *
083: * @param message the exception message
084: */
085: public IllegalClassException(String message) {
086: super (message);
087: }
088:
089: /**
090: * <p>Returns the class name or <code>null</code> if the class is
091: * <code>null</code>.</p>
092: *
093: * @param cls a <code>Class</code>
094: * @return the name of <code>cls</code>, or <code>null</code> if if <code>cls</code> is <code>null</code>.
095: */
096: private static final String safeGetClassName(Class cls) {
097: return cls == null ? null : cls.getName();
098: }
099:
100: }
|