PropertyException.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.exceptions;
19 
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 
23 /**
24  @author Andres Almiray
25  @since 2.0.0
26  */
27 public class PropertyException extends GriffonException {
28     private static final long serialVersionUID = 6721682115856360089L;
29     private static final String CAUSE = "cause";
30     private static final String BEAN = "bean";
31     private static final String PROPERTY_NAME = "propertyName";
32 
33     public PropertyException(@Nonnull Object bean, @Nonnull String propertyName, @Nullable Object value) {
34         super(formatArgs(bean, propertyName, value));
35     }
36 
37     public PropertyException(@Nonnull Object bean, @Nonnull String propertyName, @Nullable Object value, @Nonnull Throwable cause) {
38         super(formatArgs(bean, propertyName, value), checkNonNull(cause, CAUSE));
39     }
40 
41     public PropertyException(@Nonnull Object bean, @Nonnull String propertyName) {
42         super(formatArgs(bean, propertyName));
43     }
44 
45     public PropertyException(@Nonnull Object bean, @Nonnull String propertyName, @Nonnull Throwable cause) {
46         super(formatArgs(bean, propertyName), checkNonNull(cause, CAUSE));
47     }
48 
49     public PropertyException(@Nonnull Class<?> klass, @Nonnull String propertyName) {
50         super(formatArgs(klass, propertyName));
51     }
52 
53     public PropertyException(@Nonnull Class<?> klass, @Nonnull String propertyName, @Nonnull Throwable cause) {
54         super(formatArgs(klass, propertyName), checkNonNull(cause, CAUSE));
55     }
56 
57     @Nonnull
58     private static String formatArgs(@Nonnull Object bean, @Nonnull String propertyName, @Nullable Object value) {
59         checkNonNull(bean, BEAN);
60         checkNonBlank(propertyName, PROPERTY_NAME);
61         return "Cannot set property " + propertyName + " with value " + value + " on " + bean;
62     }
63 
64     @Nonnull
65     private static String formatArgs(@Nonnull Object bean, @Nonnull String propertyName) {
66         checkNonNull(bean, BEAN);
67         checkNonBlank(propertyName, PROPERTY_NAME);
68         return "Cannot get property " + propertyName + " from " + bean;
69     }
70 
71     @Nonnull
72     private static String formatArgs(@Nonnull Class<?> klass, @Nonnull String propertyName) {
73         checkNonNull(klass, "klass");
74         checkNonBlank(propertyName, PROPERTY_NAME);
75         return klass + " does not have a property named " + propertyName;
76     }
77 }