FieldInjectionPoint.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 org.codehaus.griffon.runtime.core.configuration;
19 
20 import griffon.exceptions.InstanceMethodInvocationException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 
24 import javax.annotation.Nonnull;
25 import java.beans.PropertyEditor;
26 import java.lang.reflect.Field;
27 
28 import static griffon.core.GriffonExceptionHandler.sanitize;
29 import static griffon.util.GriffonClassUtils.invokeExactInstanceMethod;
30 import static griffon.util.GriffonNameUtils.getSetterName;
31 import static java.util.Objects.requireNonNull;
32 
33 /**
34  @author Andres Almiray
35  @since 2.11.0
36  */
37 public class FieldInjectionPoint extends InjectionPoint {
38     private static final Logger LOG = LoggerFactory.getLogger(FieldInjectionPoint.class);
39 
40     private final Field field;
41 
42     public FieldInjectionPoint(@Nonnull Field field, @Nonnull String configuration, @Nonnull String key, @Nonnull String format, @Nonnull Class<? extends PropertyEditor> editor) {
43         super(configuration, key, format, editor);
44         this.field = requireNonNull(field, "Argument 'field' must not be null");
45     }
46 
47     @Nonnull
48     public Field getField() {
49         return field;
50     }
51 
52     public void setValue(@Nonnull Object instance, Object value) {
53         requireNonNull(instance, "Argument 'instance' must not be null");
54         String setter = getSetterName(field.getName());
55         try {
56             invokeExactInstanceMethod(instance, setter, value);
57         catch (InstanceMethodInvocationException imie) {
58             try {
59                 field.setAccessible(true);
60                 field.set(instance, value);
61             catch (IllegalAccessException e) {
62                 LOG.warn("Cannot set value on field {} of instance {}", getConfiguration(), instance, sanitize(e));
63             }
64         }
65     }
66 
67     @Nonnull
68     public Class<?> getType() {
69         return field.getType();
70     }
71 
72     @Override
73     public String toString() {
74         final StringBuilder sb = new StringBuilder("FieldInjectionPoint{");
75         sb.append("field=").append(field);
76         sb.append(", configuration='").append(getConfiguration()).append('\'');
77         sb.append(", key='").append(getKey()).append('\'');
78         sb.append(", format='").append(getFormat()).append('\'');
79         sb.append(", editor='").append(getEditor()).append('\'');
80         sb.append('}');
81         return sb.toString();
82     }
83 }