com.onegini.sdk.model.config.v2.MergeUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of idp-sdk Show documentation
Show all versions of idp-sdk Show documentation
Java SDK to connect to the Onegini platform
/*
* Copyright 2013-2020 Onegini b.v.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onegini.sdk.model.config.v2;
import static lombok.AccessLevel.PRIVATE;
import static org.springframework.beans.BeanUtils.getPropertyDescriptor;
import static org.springframework.beans.BeanUtils.getPropertyDescriptors;
import static org.springframework.beans.BeanUtils.isSimpleProperty;
import java.beans.FeatureDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import com.onegini.sdk.exception.MergeException;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@NoArgsConstructor(access = PRIVATE)
@Slf4j
public class MergeUtil {
/**
* Merges the non-null values into the mergeDestination object. Supports nested objects
*
* @param mergeFrom - the object whose non-null values will overwrite the calling class.
* @param mergeDestination - the object where the values will be merged into.
* @param - The type of the two classes to merge
* @return the merged object
*/
public static T merge(final T mergeFrom, final T mergeDestination) {
Assert.notNull(mergeFrom, "mergeFrom must not be null");
if(mergeDestination == null) {
return mergeFrom;
}
final Class> mergeToClass = mergeDestination.getClass();
final PropertyDescriptor[] mergeDestinationPds = getPropertyDescriptors(mergeToClass);
//Get a list of getters that are returning null, we don't want to overwrite things to null on the destination object
final Collection ignoreList = getNullPropertyNames(mergeFrom);
for (PropertyDescriptor mergeDestinationPd : mergeDestinationPds) {
final Method mergeDestinationSetter = mergeDestinationPd.getWriteMethod();
if (mergeDestinationSetter != null && !ignoreList.contains(mergeDestinationPd.getName())) {
final PropertyDescriptor mergeFromPd = getPropertyDescriptor(mergeToClass, mergeDestinationPd.getName());
final Method mergeFromGetter = mergeFromPd != null ? mergeFromPd.getReadMethod() : null;
if (mergeFromGetter != null && ClassUtils.isAssignable(mergeDestinationSetter.getParameterTypes()[0], mergeFromGetter.getReturnType())) {
mergeFromToDestination(mergeFrom, mergeDestination, mergeFromPd, mergeDestinationPd);
}
}
}
//For 2 top level maps as they dont have the same kind of property descriptors
if (mergeFrom instanceof Map) {
return (T) mergeMap(((Map