001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.application;
017    
018    /**
019     * Indicates that the initialization of an object has not set a required
020     * property.
021     *
022     * @author Kevin Stembridge
023     * @since 0.3
024     *
025     */
026    public class PropertyNotSetException extends ConfigurationException {
027    
028            private static final long serialVersionUID = 6848949416219396182L;
029    
030            /**
031             * Throws an instance of this exception if the given {@code propertyValue}
032             * is null.
033             *
034             * @param propertyValue The value of the property.
035             * @param propertyName The name of the property.
036             * @param beanClass The class on which the property is supposed to be set.
037             *
038             * @throws PropertyNotSetException if {@code propertyValue} is null.
039             */
040            public static void throwIfNull(Object propertyValue, String propertyName, Class beanClass) {
041    
042                    if (propertyValue == null) {
043                            throw new PropertyNotSetException(beanClass, propertyName);
044                    }
045    
046            }
047    
048            private final Class beanClass;
049    
050            private final String propertyName;
051    
052            /**
053             * Creates a new {@code PropertyNotSetException} with the specified bean
054             * class and property name.
055             *
056             * @param beanClass The class of the JavaBean that has an uninitialized
057             * property.
058             * @param propertyName The name of the property that has not been set.
059             */
060            public PropertyNotSetException(Class beanClass, String propertyName) {
061                    this("The [" + propertyName + "] property of class [" + beanClass + "] has not been initialized.", beanClass,
062                                    propertyName);
063    
064            }
065    
066            private PropertyNotSetException(String message, Class beanClass, String propertyName) {
067                    super(message);
068                    this.beanClass = beanClass;
069                    this.propertyName = propertyName;
070    
071            }
072    
073            /**
074             * Returns the class of the JavaBean that has the uninitialized property.
075             * @return Returns the value of the beanClass field.
076             */
077            public Class getBeanClass() {
078                    return this.beanClass;
079            }
080    
081            /**
082             * Returns the name of the property that has not been set.
083             * @return Returns the value of the propertyName field.
084             */
085            public String getPropertyName() {
086                    return this.propertyName;
087            }
088    
089    }