001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://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,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans;
018
019import org.springframework.lang.Nullable;
020
021/**
022 * Exception thrown on an attempt to set the value of a property that
023 * is not writable (typically because there is no setter method).
024 *
025 * @author Rod Johnson
026 * @author Alef Arendsen
027 * @author Arjen Poutsma
028 */
029@SuppressWarnings("serial")
030public class NotWritablePropertyException extends InvalidPropertyException {
031
032        @Nullable
033        private final String[] possibleMatches;
034
035
036        /**
037         * Create a new NotWritablePropertyException.
038         * @param beanClass the offending bean class
039         * @param propertyName the offending property name
040         */
041        public NotWritablePropertyException(Class<?> beanClass, String propertyName) {
042                super(beanClass, propertyName,
043                                "Bean property '" + propertyName + "' is not writable or has an invalid setter method: " +
044                                "Does the return type of the getter match the parameter type of the setter?");
045                this.possibleMatches = null;
046        }
047
048        /**
049         * Create a new NotWritablePropertyException.
050         * @param beanClass the offending bean class
051         * @param propertyName the offending property name
052         * @param msg the detail message
053         */
054        public NotWritablePropertyException(Class<?> beanClass, String propertyName, String msg) {
055                super(beanClass, propertyName, msg);
056                this.possibleMatches = null;
057        }
058
059        /**
060         * Create a new NotWritablePropertyException.
061         * @param beanClass the offending bean class
062         * @param propertyName the offending property name
063         * @param msg the detail message
064         * @param cause the root cause
065         */
066        public NotWritablePropertyException(Class<?> beanClass, String propertyName, String msg, Throwable cause) {
067                super(beanClass, propertyName, msg, cause);
068                this.possibleMatches = null;
069        }
070
071        /**
072         * Create a new NotWritablePropertyException.
073         * @param beanClass the offending bean class
074         * @param propertyName the offending property name
075         * @param msg the detail message
076         * @param possibleMatches suggestions for actual bean property names
077         * that closely match the invalid property name
078         */
079        public NotWritablePropertyException(Class<?> beanClass, String propertyName, String msg, String[] possibleMatches) {
080                super(beanClass, propertyName, msg);
081                this.possibleMatches = possibleMatches;
082        }
083
084
085        /**
086         * Return suggestions for actual bean property names that closely match
087         * the invalid property name, if any.
088         */
089        @Nullable
090        public String[] getPossibleMatches() {
091                return this.possibleMatches;
092        }
093
094}