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.1.0
26 */
27 public class FieldException extends GriffonException {
28 private static final long serialVersionUID = 4319304904847269368L;
29 private static final String CAUSE = "cause";
30 private static final String BEAN = "bean";
31 private static final String FIELD_NAME = "fieldName";
32
33 public FieldException(@Nonnull Object bean, @Nonnull String fieldName, @Nullable Object value) {
34 super(formatArgs(bean, fieldName, value));
35 }
36
37 public FieldException(@Nonnull Object bean, @Nonnull String fieldName, @Nullable Object value, @Nonnull Throwable cause) {
38 super(formatArgs(bean, fieldName, value), checkNonNull(cause, CAUSE));
39 }
40
41 public FieldException(@Nonnull Object bean, @Nonnull String fieldName) {
42 super(formatArgs(bean, fieldName));
43 }
44
45 public FieldException(@Nonnull Object bean, @Nonnull String fieldName, @Nonnull Throwable cause) {
46 super(formatArgs(bean, fieldName), checkNonNull(cause, CAUSE));
47 }
48
49 public FieldException(@Nonnull Class<?> klass, @Nonnull String fieldName) {
50 super(formatArgs(klass, fieldName));
51 }
52
53 public FieldException(@Nonnull Class<?> klass, @Nonnull String fieldName, @Nonnull Throwable cause) {
54 super(formatArgs(klass, fieldName), checkNonNull(cause, CAUSE));
55 }
56
57 @Nonnull
58 private static String formatArgs(@Nonnull Object bean, @Nonnull String fieldName, @Nullable Object value) {
59 checkNonNull(bean, BEAN);
60 checkNonBlank(fieldName, FIELD_NAME);
61 return "Cannot set field " + fieldName + " with value " + value + " on " + bean;
62 }
63
64 @Nonnull
65 private static String formatArgs(@Nonnull Object bean, @Nonnull String fieldName) {
66 checkNonNull(bean, BEAN);
67 checkNonBlank(fieldName, FIELD_NAME);
68 return "Cannot get field " + fieldName + " from " + bean;
69 }
70
71 @Nonnull
72 private static String formatArgs(@Nonnull Class<?> klass, @Nonnull String fieldName) {
73 checkNonNull(klass, "klass");
74 checkNonBlank(fieldName, FIELD_NAME);
75 return klass + " does not have a field named " + fieldName;
76 }
77 }
|