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