|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 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 | + |
| 17 | +package org.springframework.cloud.stream.config; |
| 18 | + |
| 19 | +import java.beans.PropertyDescriptor; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Modifier; |
| 22 | + |
| 23 | +import org.springframework.beans.BeanUtils; |
| 24 | +import org.springframework.beans.BeansException; |
| 25 | +import org.springframework.beans.FatalBeanException; |
| 26 | +import org.springframework.cloud.stream.binder.ConsumerProperties; |
| 27 | +import org.springframework.cloud.stream.binder.ProducerProperties; |
| 28 | +import org.springframework.util.ClassUtils; |
| 29 | +import org.springframework.util.ObjectUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * NOT INTENDED FOR PUBLIC USE! Was primarily created to address GH-1359. |
| 33 | + * |
| 34 | + * @see BinderProperties |
| 35 | + * @see ProducerProperties |
| 36 | + * @see ConsumerProperties |
| 37 | + * |
| 38 | + * @author Oleg Zhurakousky |
| 39 | + */ |
| 40 | +public interface MergableProperties { |
| 41 | + |
| 42 | + /** |
| 43 | + * A variation of {@link BeanUtils#copyProperties(Object, Object)} specifically designed to copy properties using the following rule: |
| 44 | + * |
| 45 | + * - If source property is null then override with the same from mergable. |
| 46 | + * - If source property is an array and it is empty then override with same from mergable. |
| 47 | + * - If source property is mergable then merge. |
| 48 | + */ |
| 49 | + default void merge(MergableProperties mergable) { |
| 50 | + for (PropertyDescriptor targetPd : BeanUtils.getPropertyDescriptors(mergable.getClass())) { |
| 51 | + Method writeMethod = targetPd.getWriteMethod(); |
| 52 | + if (writeMethod != null) { |
| 53 | + PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(this.getClass(), targetPd.getName()); |
| 54 | + if (sourcePd != null) { |
| 55 | + Method readMethod = sourcePd.getReadMethod(); |
| 56 | + if (readMethod != null && |
| 57 | + ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { |
| 58 | + try { |
| 59 | + if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { |
| 60 | + readMethod.setAccessible(true); |
| 61 | + } |
| 62 | + Object value = readMethod.invoke(this); |
| 63 | + if (value != null) { |
| 64 | + if (value instanceof MergableProperties) { |
| 65 | + ((MergableProperties)value).merge((MergableProperties)readMethod.invoke(mergable)); |
| 66 | + } |
| 67 | + else { |
| 68 | + Object v = readMethod.invoke(mergable); |
| 69 | + if (v == null || (ObjectUtils.isArray(v) && ObjectUtils.isEmpty(v))) { |
| 70 | + if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { |
| 71 | + writeMethod.setAccessible(true); |
| 72 | + } |
| 73 | + writeMethod.invoke(mergable, value); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + catch (Throwable ex) { |
| 79 | + throw new FatalBeanException( |
| 80 | + "Could not copy property '" + targetPd.getName() + "' from source to target", ex); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + default void copyProperties(Object source, Object target) throws BeansException { |
| 89 | + |
| 90 | + } |
| 91 | +} |
0 commit comments