FieldInjectionPoint.java
01 /*
02  * Copyright 2008-2017 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.codehaus.griffon.runtime.core.configuration;
17 
18 import griffon.exceptions.InstanceMethodInvocationException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 
22 import javax.annotation.Nonnull;
23 import java.lang.reflect.Field;
24 
25 import static griffon.core.GriffonExceptionHandler.sanitize;
26 import static griffon.util.GriffonClassUtils.invokeExactInstanceMethod;
27 import static griffon.util.GriffonNameUtils.getSetterName;
28 import static java.util.Objects.requireNonNull;
29 
30 /**
31  @author Andres Almiray
32  @since 2.11.0
33  */
34 public class FieldInjectionPoint extends InjectionPoint {
35     private static final Logger LOG = LoggerFactory.getLogger(FieldInjectionPoint.class);
36 
37     private final Field field;
38 
39     public FieldInjectionPoint(@Nonnull Field field, @Nonnull String configuration, @Nonnull String key, @Nonnull String format) {
40         super(configuration, key, format);
41         this.field = requireNonNull(field, "Argument 'field' must not be null");
42     }
43 
44     @Nonnull
45     public Field getField() {
46         return field;
47     }
48 
49     public void setValue(@Nonnull Object instance, Object value) {
50         requireNonNull(instance, "Argument 'instance' must not be null");
51         String setter = getSetterName(field.getName());
52         try {
53             invokeExactInstanceMethod(instance, setter, value);
54         catch (InstanceMethodInvocationException imie) {
55             try {
56                 field.setAccessible(true);
57                 field.set(instance, value);
58             catch (IllegalAccessException e) {
59                 LOG.warn("Cannot set value on field {} of instance {}", getConfiguration(), instance, sanitize(e));
60             }
61         }
62     }
63 
64     @Nonnull
65     public Class<?> getType() {
66         return field.getType();
67     }
68 
69     @Override
70     public String toString() {
71         final StringBuilder sb = new StringBuilder("FieldInjectionPoint{");
72         sb.append("field=").append(field);
73         sb.append(", configuration='").append(getConfiguration()).append('\'');
74         sb.append(", key='").append(getKey()).append('\'');
75         sb.append(", format='").append(getFormat()).append('\'');
76         sb.append('}');
77         return sb.toString();
78     }
79 }