001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.beans;
032:
033: import java.beans.PropertyDescriptor;
034:
035: /**
036: * A runtime exception that describes read and write access problems when
037: * getting/setting a Java Bean property.
038: *
039: * @author Karsten Lentzsch
040: * @version $Revision: 1.4 $
041: * @see com.jgoodies.binding.beans.PropertyAdapter
042: */
043: public final class PropertyAccessException extends PropertyException {
044:
045: /**
046: * Constructs a new exception instance with the specified detail message
047: * and cause.
048: *
049: * @param message the detail message (which is saved for later retrieval
050: * by the {@link #getMessage()} method).
051: * @param cause the cause (which is saved for later retrieval by the
052: * {@link #getCause()} method). (A <code>null</code> value is permitted,
053: * and indicates that the cause is nonexistent or unknown.)
054: */
055: public PropertyAccessException(String message, Throwable cause) {
056: super (message, cause);
057: }
058:
059: /**
060: * Creates and returns a new PropertyAccessException instance for a failed
061: * read access for the specified bean, property descriptor and cause.
062: *
063: * @param bean the target bean
064: * @param propertyDescriptor describes the bean's property
065: * @param cause the Throwable that caused this exception
066: * @return an exception that describes a read access problem
067: */
068: public static PropertyAccessException createReadAccessException(
069: Object bean, PropertyDescriptor propertyDescriptor,
070: Throwable cause) {
071:
072: String beanType = bean == null ? null : bean.getClass()
073: .getName();
074: String message = "Failed to read an adapted Java Bean property."
075: + "\ncause="
076: + cause
077: + "\nbean="
078: + bean
079: + "\nbean type="
080: + beanType
081: + "\nproperty name="
082: + propertyDescriptor.getName()
083: + "\nproperty type="
084: + propertyDescriptor.getPropertyType().getName()
085: + "\nproperty reader="
086: + propertyDescriptor.getReadMethod();
087:
088: return new PropertyAccessException(message, cause);
089: }
090:
091: /**
092: * Creates and returns a new PropertyAccessException instance for a failed
093: * write access for the specified bean, value, property descriptor and
094: * cause.
095: *
096: * @param bean the target bean
097: * @param value the value that could not be set
098: * @param propertyDescriptor describes the bean's property
099: * @param cause the Throwable that caused this exception
100: * @return an exception that describes a write access problem
101: */
102: public static PropertyAccessException createWriteAccessException(
103: Object bean, Object value,
104: PropertyDescriptor propertyDescriptor, Throwable cause) {
105:
106: String beanType = bean == null ? null : bean.getClass()
107: .getName();
108: String valueType = value == null ? null : value.getClass()
109: .getName();
110: String message = "Failed to set an adapted Java Bean property."
111: + "\ncause=" + cause + "\nbean=" + bean
112: + "\nbean type=" + beanType + "\nvalue=" + value
113: + "\nvalue type=" + valueType + "\nproperty name="
114: + propertyDescriptor.getName() + "\nproperty type="
115: + propertyDescriptor.getPropertyType().getName()
116: + "\nproperty setter="
117: + propertyDescriptor.getWriteMethod();
118:
119: return new PropertyAccessException(message, cause);
120: }
121:
122: }
|